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.
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.
The following example demonstrates how to perform separate data extraction and indexing.
C#
stringindexFolder=@"c:\MyIndex";stringdocumentPath=@"c:\MyDocuments\MyDocument.pdf";// Extracting data from a documentExtractorextractor=newExtractor();extractor.ErrorOccurred+=(sender,args)=>Console.WriteLine(args.Message);Documentdocument=Document.CreateFromFile(documentPath);ExtractionOptionsextractionOptions=newExtractionOptions();extractionOptions.UseRawTextExtraction=false;ExtractedDataextractedData=extractor.Extract(document,extractionOptions);// Serializing the databyte[]array=extractedData.Serialize();// Deserializing the dataExtractedDatadeserializedData=ExtractedData.Deserialize(array);// Creating an indexIndexindex=newIndex(indexFolder);if(deserializedData.IsExtractionSuccessful){// Indexing the dataExtractedData[]data=newExtractedData[]{deserializedData};index.Add(data,newIndexingOptions());// Searching in the indexSearchResultresult=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: