This page contains a description of how to get a list of indexed documents from an index, and how to get the text of indexed documents in HTML or plain text format.
Getting indexed documents
To get a list of indexed documents from an index, use the get_indexed_documents method of the Index class. Documents with the extensions ZIP, PST, OST can also contain internal documents. To get a list of internal documents, use the get_indexed_document_items method of the Index class. For ZIP archives, this way you can access documents of arbitrary nesting depth. An example of obtaining a list of documents from an index is presented below.
fromgroupdocs.searchimportIndex# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Getting list of indexed documentsdocuments=index.get_indexed_documents()fordocumentindocuments:print(document.file_path)items=index.get_indexed_document_items(document)# Getting list of document itemsforiteminitems:print("\t"+item.inner_path)
To get a single indexed document, use the get_indexed_document method. It takes the file path of a document indexed from a file (as returned by the DocumentInfo.file_path property, which is a full path) or the key of a document indexed from a stream or a structure, and returns None if there is no such document in the index.
importosfromgroupdocs.searchimportIndex# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Getting an indexed document by its file pathdocument=index.get_indexed_document(os.path.abspath("./documents/einstein-biography.txt"))ifdocumentisnotNone:print(document.file_path+" ("+str(document.file_type)+")")
The text of an indexed document can be extracted from an index if the option to save the text of documents in the index was enabled. If this option was not enabled when creating an index, then when the get_document_text method of the Index class is called, the text of the document will be extracted from the document again. Details about saving the text of documents in an index can be found on the page Storing text of indexed documents.
The generated text of the document is passed to an instance of a class derived from the abstract class OutputAdapter. Details on the output adapters are presented on the page Output adapters.
After generating the text of a document into a file, this file can be opened by an Internet browser. The following example shows how to extract document text from an index.
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportFileOutputAdapterfromgroupdocs.search.optionsimportOutputFormat# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Getting list of indexed documentsdocuments=index.get_indexed_documents()# Getting a document textiflen(documents)>0:os.makedirs("./output",exist_ok=True)output_adapter=FileOutputAdapter(OutputFormat.HTML,"./output/Text.html")index.get_document_text(documents[0],output_adapter)print("The text of",os.path.basename(documents[0].file_path),"is saved to ./output/Text.html")
To extract the text of a document from an index, there is also an overload of the method that takes an instance of the TextOptions class as a parameter. In this class, the following options can be specified:
custom_extractor is a custom extractor used during indexing, it is necessary if the text of the document was not saved in the index;
additional_fields are additional document fields added during document indexing which are also necessary if the document text was not saved in the index;
cancellation is an object used to cancel the operation;
generate_head is a flag to specify whether the Head tag is generated in the output HTML; the default value is True;
metadata_indexing_options is an object for specifying metadata indexing options. This property and the other properties inherited from the BaseOptions class (for example, encoding, auto_detect_encoding and ocr_indexing_options) specify how the text of the document is extracted; they are necessary if the document text was not saved in the index.
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: