Update index

This page contains a description of updating indexed documents, as well as updating an index version.

Update indexed documents

The update operation is used to reindex documents that have been changed, deleted or added to indexed folders. Changing a filter specified by the document_filter property of the IndexSettings class can also lead to a change in the list of indexed documents.

The update operation also indexes again the documents whose previous indexing was not completed. For example, a document whose indexing was interrupted by cancellation is marked with the indexed_with_error flag and is indexed again by the next update. Note that only documents indexed from files can be indexed again: documents added from streams, structures or extracted data have to be added again. In the same way, the update indexes again the files of documents deleted when a damaged index is repaired (see Checking index integrity).

When updating, the same options can be specified in the instance of the UpdateOptions class, which are set in the IndexingOptions class to specify indexing options. See the Indexing options page.

The following example demonstrates how to update an index using 2 threads.

from groupdocs.search import Index
from groupdocs.search.options import UpdateOptions

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

# Indexing documents from the specified folder
index.add("./documents")

# Change, delete, add documents in the document folder
# ...

options = UpdateOptions()
options.threads = 2  # Setting the number of indexing threads
index.update(options)  # Updating the index

print("Indexed documents:", len(index.get_indexed_documents()))

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

Indexed documents: 2

Download full output

Update index version

When a new version of the GroupDocs.Search library changes the format for storing the index on disk, an index created by a previous version has to be updated to the new format with the IndexUpdater class. Version 26.10 introduces a new index format: an index of a previous version, including versions 26.7 and 26.8, cannot be opened until its version is updated, and the Index constructor raises RuntimeError for it.

The update_version method updates an index as follows:

  • An index created by versions 22.7–26.8 is converted to the new format without re-indexing, so the source documents are not required.
  • An index created by versions 18.5–22.6 is updated by indexing the documents again, so the documents must be available at the paths from which they were indexed.
  • The source index is never modified. The updated index is written to another folder, which must not be the source folder, a folder that contains it or a folder inside it. The content of the target folder is deleted before the updated index is written.
  • The method returns a value of the VersionUpdateResult enumeration: UPDATED, ALREADY_UP_TO_DATE (the index already has the current version) or UNSUPPORTED (the folder does not contain an index that can be updated).

The is_latest_version method checks whether a folder contains an index of the current version, and the can_update_version method checks whether a folder contains an index of a previous version that can be updated. After a successful update, the folder containing the old version of the index may be deleted. The full rules, including the exceptions and the settings of an updated index, are described on the Index format and compatibility page.

The following example demonstrates updating a previous version of an index.

from groupdocs.search import Index, IndexUpdater
from groupdocs.search.common import VersionUpdateResult

source_index_folder = "./old-index"
target_index_folder = "./new_index"

updater = IndexUpdater()

if updater.is_latest_version(source_index_folder):
    # The index has the current version and can be opened without updating
    index = Index(source_index_folder)
    print("The index has the current version")
elif updater.can_update_version(source_index_folder):
    # The index of the old version does not change
    result = updater.update_version(source_index_folder, target_index_folder)
    if result == VersionUpdateResult.UPDATED:
        # Opening the updated index
        index = Index(target_index_folder)
        print("The index is updated: " + str(len(index.get_indexed_documents())) + " documents")
The index is updated: 4 documents

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.