Optimize index

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.

from groupdocs.search import Index
from groupdocs.search.common import Cancellation
from groupdocs.search.options import MergeOptions

# Creating an index in the specified folder
index = Index("./index")

index.add("./documents")  # Indexing documents from the specified folder
index.add("./documents2")  # Each call to add creates at least one new segment in the index
index.add("./documents3")

options = MergeOptions()
options.cancellation = Cancellation()  # Creating cancellation object to be able to cancel the operation
options.cancellation.cancel_after(100000)  # Setting maximum duration of the operation to 100 seconds

# Merging segments of the index
index.optimize(options)

print("Segment count: " + str(index.index_info.segment_count))

The following sample files are used in this example: documents/Einstein.txt, documents/Newton.txt

The following sample file is used in this example: documents2/Curie.txt

The following sample file is used in this example: documents3/Bohr.txt

Segment count: 1

Download full output

More resources

GitHub examples

You may find more code examples in our GitHub repository:

Free online document search App

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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.