var sourceIndexes = new string[] { "index1", "index2", "index3" }; var targetIndexDir = @"C:\index\combinedIndex"; var indexWriters = sourceIndexes.Select(indexDir => new IndexWriter(FSDirectory.Open(indexDir), new StandardAnalyzer(LuceneVersion.LUCENE_48), IndexWriter.MaxFieldLength.UNLIMITED)) .ToList(); var targetIndexWriter = new IndexWriter(FSDirectory.Open(targetIndexDir), new StandardAnalyzer(LuceneVersion.LUCENE_48), IndexWriter.MaxFieldLength.UNLIMITED); targetIndexWriter.AddIndexes(indexWriters.Select(indexWriter => indexWriter.Directory)); foreach (var indexWriter in indexWriters) { indexWriter.Dispose(); } targetIndexWriter.Dispose();
var sourceIndexDir = @"C:\index\sourceIndex"; var targetIndexDir = @"C:\index\targetIndex"; var sourceIndexReader = DirectoryReader.Open(FSDirectory.Open(sourceIndexDir)); var sourceSearcher = new IndexSearcher(sourceIndexReader); var targetIndexWriter = new IndexWriter(FSDirectory.Open(targetIndexDir), new StandardAnalyzer(LuceneVersion.LUCENE_48), IndexWriter.MaxFieldLength.UNLIMITED); targetIndexWriter.AddIndexes(sourceSearcher.IndexReader); sourceIndexReader.Dispose(); targetIndexWriter.Dispose();This code demonstrates how to add an existing index (sourceIndex) into a new index (targetIndex) using AddIndexes method. It first opens the sourceIndex for searching, creates a new IndexSearcher instance, and passes its IndexReader to AddIndexes method of targetIndexWriter. Finally, both IndexReader and IndexWriter instances are disposed. Both examples use the Lucene.Net.Index.IndexWriter class and IndexWriter.AddIndexes method from Lucene.Net package library.
After this completes, the index is optimized.
The provided IndexReaders are not closed.
NOTE: while this is running, any attempts to add or delete documents (with another thread) will be paused until this method completes.
See AddIndexesNoOptimize(Directory[]) for details on transactional semantics, temporary free space required in the Directory, and non-CFS segments on an Exception.
NOTE: if this method hits an OutOfMemoryError you should immediately close the writer. See above for details.
public AddIndexes ( ) : void | ||
return | void |