Merge indexes

The merge operation is designed to combine two or more indexes into one index to accelerate the search and to simplify the work with indexes. When merging, only the index at which the merge method was called is changed. This index as a result of the operation contains all the documents that were contained in all indexes together. The second index or index repository after the merge can be deleted to free up disk space.

The MergeOptions class contains a property for specifying a cancellation object, as well as a property for specifying whether the operation will be performed asynchronously. By default, index merging operation is performed synchronously.

The Index class merges either a single index or a whole index repository into the current index: pass an Index or an IndexRepository object to the merge method. Before performing a merge, checks are made to ensure that the indexes are of the same type. Merging is performed only on those indexes that have the same index_type value as the index on which the merge method is called.

Merging compatibility

Since version 26.10, only indexes that divide the text of documents into terms in the same way can be merged:

  • Before making any changes, the merge method compares the current index with every merged index that contains documents: the use_stop_words, use_character_replacements, replace_unpaired_surrogates and skip_blended_only_terms settings, the alphabet, the tokenization dictionary, and also the stop words and the character replacements if the indexes use them. If a difference is found, the method raises RuntimeError, whose message names the differences, and no index is changed. The exception’s message attribute holds the message alone; str(exception) also contains the .NET exception type and stack trace. This also happens in the asynchronous mode.
  • The alphabet, the tokenization dictionary, the stop word dictionary and the character replacement dictionary of the current index are not changed by merging. The other dictionaries of the merged indexes (for example, synonyms and homophones) are merged into the dictionaries of the current index.
  • A merged index created by a previous version must be updated with the IndexUpdater class before merging (see Update index).
  • A merge is not performed if a merged index is performing an operation or has failed; the error_occurred event reports it.

Note that an index updated from a previous version has the replace_unpaired_surrogates and skip_blended_only_terms settings disabled, while a new index has them enabled by default. The full rules and an example of merging an updated index are presented on the Index format and compatibility page.

Merging example

The following example demonstrates the merging of two indexes.

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

index1 = Index("./index1")  # Creating index1
index1.add("./documents")  # Indexing documents

index2 = Index("./index2")  # Creating index2
index2.add("./documents2")  # Indexing documents

options = MergeOptions()
options.cancellation = Cancellation()  # Creating cancellation object to be able to cancel the operation

try:
    # Merging index2 into index1
    # Files of index2 will not be changed
    index1.merge(index2, options)
except RuntimeError as exception:
    # The indexes divide text into terms differently; the message names the differences
    print(exception.message)

print("Documents in index1:", len(index1.get_indexed_documents()))

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

Documents in index1: 3

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.