public IndexReader Create() { var directory = _directoryFactory.Create(); if (IndexReader.IndexExists(directory)) { return(IndexReader.Open(directory, true)); } //create an index and then open using (var writer = _writerFactory.Create()) { writer.Commit(); } return(IndexReader.Open(_directoryFactory.Create(), true)); }
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.Entity.IsFirstRun) { writer.AddDocument(doc); } 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); } else { writer.AddDocument(doc); } } } writer.Commit(); writer.Optimize(); } } }