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: the serialize method returns bytes, which can be stored or sent to another process or server, and ExtractedData.deserialize restores the data from them.
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 index_type property specifies the type of the index for which data are extracted (the default value is IndexType.NORMAL_INDEX); set it to the type of the index to which the data will be added. Its temp_folder property specifies the folder for temporary files.
The following example demonstrates how to perform separate data extraction and indexing.
fromgroupdocs.searchimportExtractor,Indexfromgroupdocs.search.commonimportDocument,ExtractedDatafromgroupdocs.search.optionsimportExtractionOptions,IndexingOptionsdocument_path="./sample.pdf"# Extracting data from a documentextractor=Extractor()extractor.error_occurred+=lambdasender,args:print(args.message)document=Document.create_from_file(document_path)extraction_options=ExtractionOptions()extraction_options.use_raw_text_extraction=Falseextracted_data=extractor.extract(document,extraction_options)# Serializing the dataarray=extracted_data.serialize()# Deserializing the datadeserialized_data=ExtractedData.deserialize(array)# Creating an indexindex=Index("./index")ifdeserialized_data.is_extraction_successful:# Indexing the datadata=[deserialized_data]index.add(data,IndexingOptions())# Searching in the indexresult=index.search("Einstein")print("Documents found:",result.document_count)
The following sample file is used in this example: sample.pdf
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 find more code examples in our GitHub repository:
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.