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 has method overloads for merging a single index and a whole index repository. 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 IndexType 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 UseStopWords, UseCharacterReplacements, ReplaceUnpairedSurrogates and SkipBlendedOnlyTerms 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 throws InvalidOperationException, whose message names the differences, and no index is changed. 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 ErrorOccurred event reports it.
The following example demonstrates the merging of two indexes.
C#
stringindexFolder1=@"c:\MyIndex1\";stringindexFolder2=@"c:\MyIndex2\";stringdocumentsFolder1=@"c:\MyDocuments1\";stringdocumentsFolder2=@"c:\MyDocuments2\";Indexindex1=newIndex(indexFolder1);// Creating index1index1.Add(documentsFolder1);// Indexing documentsIndexindex2=newIndex(indexFolder2);// Creating index2index2.Add(documentsFolder2);// Indexing documentsMergeOptionsoptions=newMergeOptions();options.Cancellation=newCancellation();// Creating cancellation object to be able to cancel the operationtry{// Merging index2 into index1// Files of index2 will not be changedindex1.Merge(index2,options);}catch(InvalidOperationExceptionexception){// The indexes divide text into terms differently; the message names the differencesConsole.WriteLine(exception.Message);}
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: