Exemple #1
0
        public object GetMaxVersion()
        {
            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            using (var searcher = _searcherFactory.Create()) {
                var version = _context.Entity.GetVersionField();

                _context.Debug(() => $"Detecting max input version: {_context.Connection.Folder}:{version.Name}.");

                var hits = searcher.Search(new MatchAllDocsQuery(), null, 1,
                                           new Sort(new SortField(version.Name, LuceneConversion.TypeSort(version.Type), true))
                                           );

                if (hits.TotalHits > 0)
                {
                    var doc   = searcher.Doc(hits.ScoreDocs[0].Doc);
                    var value = doc.Get(version.Name);
                    _context.Debug(() => $"Found value: {value}");
                    return(version.Convert(value));
                }
            }

            _context.Debug(() => "Did not find max input version");
            return(null);
        }
        public void Write(IEnumerable <IRow> rows)
        {
            var tflKey = _output.Entity.TflKey();

            using (var searcher = _searcherFactory.Create()) {
                using (var writer = _writerFactory.Create()) {
                    foreach (var row in rows)
                    {
                        var tflId = string.Concat(_primaryKey.Select(pk => row[pk].ToString()));
                        var doc   = new Document();
                        foreach (var field in _fieldSearchTypes.Where(field => field.SearchType.Store || field.SearchType.Index))
                        {
                            doc.Add(CreateField(field, row[field.Field]));
                        }
                        doc.Add(new LuceneField("TflId", tflId, LuceneField.Store.YES, LuceneField.Index.NOT_ANALYZED_NO_NORMS));
                        if (_output.Process.Mode == "init")
                        {
                            writer.AddDocument(doc);
                            _output.Entity.Inserts += 1;
                        }
                        else
                        {
                            var term = new Term("TflId", tflId);
                            var hits = searcher.Search(new TermQuery(term), null, 1);
                            if (hits.TotalHits > 0)
                            {
                                var old = searcher.Doc(hits.ScoreDocs[0].Doc);
                                doc.RemoveField(tflKey.Alias);
                                doc.Add(new NumericField(tflKey.Alias, LuceneField.Store.YES, true).SetIntValue(Convert.ToInt32(old.Get(tflKey.Alias))));
                                writer.UpdateDocument(term, doc);
                                _output.Entity.Updates += 1;
                            }
                            else
                            {
                                writer.AddDocument(doc);
                                _output.Entity.Inserts += 1;
                            }
                        }
                    }
                    writer.Commit();
                    writer.Optimize();
                }
            }
        }
Exemple #3
0
        public IEnumerable <IRow> Read()
        {
            using (var reader = _readerFactory.Create()) {
                var numDocs  = reader.NumDocs();
                var selector = new MapFieldSelector(_fields.Select(f => f.Name).ToArray());

                using (var searcher = _searcherFactory.Create()) {
                    // read from input?  consider filters, and field names
                    if (_readFrom == ReadFrom.Input)
                    {
                        if (_context.Entity.Filter.Any())
                        {
                            var queryFields = _context.Entity.Filter.Select(f => f.Field).ToArray();
                            var query       = string.Join(" ", _context.Entity.Filter.Select(f => "(" + (string.IsNullOrEmpty(f.Expression) ? f.Field + ":" + f.Value : f.Expression) + ") " + f.Continuation.ToUpper()));
                            query = query.Remove(query.Length - 3);
                            var topFieldCollector = TopFieldCollector.Create(Sort.INDEXORDER, numDocs, false, false, false, false);

                            searcher.Search(new MultiFieldQueryParser(V, queryFields, _analyzer).Parse(query), topFieldCollector);

                            var topDocs = topFieldCollector.TopDocs();

                            if (topDocs == null)
                            {
                                yield break;
                            }

                            for (var i = 0; i < topDocs.TotalHits; i++)
                            {
                                var row = _rowFactory.Create();
                                var doc = searcher.Doc(i, selector);
                                foreach (var field in _fields)
                                {
                                    row[field] = field.Convert(doc.Get(field.Name));
                                }
                                yield return(row);
                            }
                        }
                        else
                        {
                            for (var i = 0; i < numDocs; i++)
                            {
                                if (reader.IsDeleted(i))
                                {
                                    continue;
                                }
                                var doc = reader.Document(i, selector);
                                var row = _rowFactory.Create();
                                foreach (var field in _fields)
                                {
                                    row[field] = field.Convert(doc.Get(field.Name));
                                }
                                yield return(row);
                            }
                        }
                    }
                    else // read from output? consider tfldeleted and field aliases

                    {
                        var tflDeleted = _context.Entity.TflDeleted();
                        var collector  = TopFieldCollector.Create(Sort.INDEXORDER, numDocs, false, false, false, false);
                        searcher.Search(LuceneConversion.TypeSearch(tflDeleted, tflDeleted.Alias, false), collector);

                        var topDocs = collector.TopDocs();

                        if (topDocs == null)
                        {
                            yield break;
                        }

                        for (var i = 0; i < topDocs.TotalHits; i++)
                        {
                            var row = _rowFactory.Create();
                            var doc = searcher.Doc(i, selector);
                            foreach (var field in _fields)
                            {
                                row[field] = field.Convert(doc.Get(field.Alias));
                            }
                            yield return(row);
                        }
                    }
                }
            }
        }
 public LuceneOutputProvider(OutputContext context, SearcherFactory searcherFactory)
 {
     _searcherFactory = searcherFactory;
     _context         = context;
     _searcher        = searcherFactory.Create();
 }