Separate data extraction

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 →

The feature described in this article makes it possible to separate the operations of extracting data from a document and adding the extracted data to the index. The extracted data can be easily serialized and deserialized as needed.

This feature can be useful because the operation of extracting text and other data from documents can be very long for some formats. At the same time, adding data to the index is a rather lightweight operation. Therefore, separating these operations significantly reduces the time during which the index is busy performing operations, and adding more data extractors increases the overall indexing performance. Data extractors can even work on separate servers, completely freeing the indexing server from extracting operations.

The Extractor class is used to extract data from documents. Upon completion of the operation, the Extract method returns an instance of the ExtractedData class, which is used directly to add to the index.

The Extractor class has the following members in addition to the Extract method:

  • The Settings property returns an ExtractorSettings object. Its IndexType property specifies the type of the index for which data are extracted (the default value is NormalIndex); set it to the type of the index to which the data will be added. Its TempFolder property specifies the folder for temporary files, for example for the copies of the files that the extractor reads with the backup privilege (UseBackupPrivilege). Since version 26.11, the temporary folder of the system is used if the property is not set; before, such a copy failed.
  • The ErrorOccurred, PasswordRequired and ImagePreparing events, which work like the index events with the same names.

The ExtractionOptions class contains the same extraction options as the IndexingOptions class inherited from BaseOptions (see Indexing options), as well as the CustomExtractor property, which specifies a custom text extractor for the document.

Since version 26.11, a text that fails while it is read is kept up to the failure: the failure is reported by the ErrorOccurred event, and the extraction is not successful (IsExtractionSuccessful is false). Such data can still be added to the index: the document is indexed with the text read before the failure and is marked with the IndexedWithError flag. Before, the text ended without an error, and the extraction was successful.

Since version 26.11, the Deserialize method of the ExtractedData class refuses damaged data with InvalidDataException, and data that ends too early with InvalidOperationException. Before, damaged data could terminate the process.

The following example demonstrates how to perform separate data extraction and indexing.

C#

string indexFolder = @"c:\MyIndex";
string documentPath = @"c:\MyDocuments\MyDocument.pdf";

// Extracting data from a document
Extractor extractor = new Extractor();
extractor.ErrorOccurred += (sender, args) => Console.WriteLine(args.Message);
Document document = Document.CreateFromFile(documentPath);
ExtractionOptions extractionOptions = new ExtractionOptions();
extractionOptions.UseRawTextExtraction = false;
ExtractedData extractedData = extractor.Extract(document, extractionOptions);

// Serializing the data
byte[] array = extractedData.Serialize();

// Deserializing the data
ExtractedData deserializedData = ExtractedData.Deserialize(array);

// Creating an index
Index index = new Index(indexFolder);

if (deserializedData.IsExtractionSuccessful)
{
    // Indexing the data
    ExtractedData[] data = new ExtractedData[]
    {
        deserializedData
    };
    index.Add(data, new IndexingOptions());

    // Searching in the index
    SearchResult result = index.Search("Einstein");
}

Note that when indexed documents change and need to be updated in the index, the same code must be executed. That is, data must be extracted separately from the updated documents and then the extracted data must be added to the index. The Update method does not update documents added from extracted data.

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.