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 segment_count property of Index.index_info may be greater than 1 after optimizing such an index.
To perform this operation, call the optimize method without arguments or with the MergeOptions instance as an argument. 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.
fromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportCancellationfromgroupdocs.search.optionsimportMergeOptions# Creating an index in the specified folderindex=Index("./index")index.add("./documents")# Indexing documents from the specified folderindex.add("./documents2")# Each call to add creates at least one new segment in the indexindex.add("./documents3")options=MergeOptions()options.cancellation=Cancellation()# Creating cancellation object to be able to cancel the operationoptions.cancellation.cancel_after(100000)# Setting maximum duration of the operation to 100 seconds# Merging segments of the indexindex.optimize(options)print("Segment count: "+str(index.index_info.segment_count))
Along with the full-featured library, we provide a free online document search app. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our Free Online Document Search App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.