An index optimization operation allows you to reduce the number of segments in an index, thereby increasing search performance in the index. Each indexing operation adds at least one new segment to an index, so an index to which documents are often added should be optimized from time to time.
The optimization also physically removes deleted documents from the index. Deleting documents does not rewrite the index: deleted documents are excluded from search immediately, but their data remain in the index files until the index is optimized.
The memory consumption of the optimization does not depend on the index size: a very large index is optimized into several segments of limited size, so the SegmentCount property of Index.IndexInfo may be greater than 1 after optimizing such an index.
To perform this operation, call the parameterless Optimize method or the overload that takes the MergeOptions instance as a parameter. In the MergeOptions class, there is a property for specifying a cancellation object and a property for specifying whether to perform the operation asynchronously. By default, the operation is performed synchronously.
The following example demonstrates how to perform the index optimization.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder1=@"c:\MyDocuments1\";stringdocumentsFolder2=@"c:\MyDocuments2\";stringdocumentsFolder3=@"c:\MyDocuments3\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);index.Add(documentsFolder1);// Indexing documents from the specified folderindex.Add(documentsFolder2);// Each call to Add creates at least one new segment in the indexindex.Add(documentsFolder3);MergeOptionsoptions=newMergeOptions();options.Cancellation=newCancellation();// Creating cancellation object to be able to cancel the operationoptions.Cancellation.CancelAfter(100000);// Setting maximum duration of the operation to 100 seconds// Merging segments of the indexindex.Optimize(options);Console.WriteLine("Segment count: "+index.IndexInfo.SegmentCount);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: