Checking index integrity

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Search MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-search -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-search/search-net-mcp:latest
Folders, licensing and more →

This page describes how GroupDocs.Search protects the data of an index on disk, how an index recovers after an interrupted operation, and how to check and repair a damaged index.

Protection of index data

Since version 26.10, the index data are protected with checksums, so damaged data are detected when they are read and are reported instead of being used. All changes to an index are applied atomically: if an operation is interrupted by an error, a power failure or termination of the process, the index remains in its previous consistent state, and the changes of the interrupted operation are not visible.

An indexing operation writes the new data to separate files first and then merges them with the index. If the operation is interrupted after its data have been written, the next time the index is opened:

  • the written data are verified and merged with the index, so the indexing operation is completed;
  • if the written data are damaged or incomplete, or, since version 26.11, cannot be merged with the index, they are discarded, so the indexing operation is rolled back and the index keeps its state before the operation. The changes of the operation are lost and have to be made again, for example, by repeating the interrupted operation. Since this happens while the index is being opened, the reason is written to the logger of the index, if it is specified in the index settings passed to the constructor; since version 26.11, it is also listed in the errors of the next indexing report.

A document whose indexing was interrupted by cancellation is marked with the IndexedWithError flag and is indexed again by the next update of the index.

Since version 26.11, the optimization, the merge of the data of an indexing operation with the index and the merge of indexes verify the checksums of the index data that they read before they complete the files that they write. An optimization or a merge of indexes that reads damaged data fails: the ErrorOccurred event reports that a file of the index is damaged, the files written by the operation are deleted, and the index keeps its state. Repair the index with the CheckSegments method (see below) and repeat the operation. An indexing operation or a merge of indexes that reads damaged posting lists of the whole index makes them again from the segments, reports it as an error that is not critical (IsCritical is false) and goes on.

Checking and repairing an index

The CheckSegments method of the Index class checks the files of an index, for example, after a disk failure or when errors about damaged index files are reported. The method verifies the checksum of every file of the index, including the term dictionary of the index, the posting lists, the files of every segment and the files of the stored document texts, and also verifies that the data of the segments match the data of the whole index.

A file that cannot be read for a reason other than damage, for example because another program (an antivirus, a backup or a synchronization program) has locked it or the access to it is denied, is not taken for a damaged file: the check stops, the reason is reported through the ErrorOccurred event, and nothing is repaired. Repeat the check when the file can be read. Since version 26.11, the check also stops in this way at a file that a newer version of the library has written.

Every damage found and every repair made is reported as an index error: through the ErrorOccurred event and to the logger of the index, if it is specified. If no errors are reported, the index is intact.

The parameter of the method specifies whether a damaged index is repaired:

  • false - the index is only checked, and nothing is changed;
  • true - the index is checked, and if damage is found, the index is repaired.

The repair keeps as many documents as possible:

  • A damaged file of the index data that is kept in memory, such as the index metadata or a dictionary, is written again.
  • A damaged term dictionary of a segment is restored from the data of the whole index, and the term dictionary and the posting lists of the whole index are restored from the data of the segments.
  • A segment whose postings are damaged, or whose term dictionary cannot be restored, is excluded from the index, and its documents are deleted.
  • A damaged stored text of a document is removed, and the document is deleted. If the table of a text file is damaged, the file is excluded together with the documents of its texts.

The repaired index is saved at once. Since version 26.11, after the events that describe the repair, the ErrorOccurred event is raised once for every document that the repair deletes, with the key of the document and the reason: The document <key> is deleted from the index: <reason>. Before, the event told only the number of the deleted documents. The paths of the indexed files and folders are kept, so the next update of the index indexes the files of the deleted documents again. Documents added from streams, structures or extracted data cannot be indexed again by an update: add them again by the keys that the events name.

Checking segments is an index operation: the index gets the CheckingSegments status while it is performed. If the index is performing another operation, the method does nothing and reports the reason through the ErrorOccurred event. Searches are not blocked by the check: they work either with the damaged or with the repaired index.

The following example demonstrates how to check an index and repair it if damage is found.

C#

string indexFolder = @"c:\MyIndex\";

// Opening an existing index
Index index = new Index(indexFolder);

// Collecting the messages about damage and repair
List<string> messages = new List<string>();
index.Events.ErrorOccurred += (sender, args) =>
{
    messages.Add(args.Message);
};

// Checking the index without changing it
index.CheckSegments(false);

if (messages.Count > 0)
{
    foreach (string message in messages)
    {
        Console.WriteLine(message);
    }

    // Repairing the index
    messages.Clear();
    index.CheckSegments(true);
    foreach (string message in messages)
    {
        Console.WriteLine(message);
    }

    // Indexing again the files of the documents deleted by the repair
    index.Update();
}

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.