Getting document text in network

To obtain the extracted text of indexed documents in the search network, the get_document_text method of the Searcher class is used.

The first parameter of the get_document_text method is a document represented by the NetworkDocumentInfo class. A list of all indexed documents in the search network can be obtained using the get_indexed_documents method of the Searcher class.

The second parameter of the get_document_text method is the result output adapter. In this operation, the same output adapter classes are used as those used in similar operations of a single index. For information about output adapters, see the Output adapters page. The text is available only if the text storage is enabled in the configuration of the search network; otherwise, the output adapter receives no text, and the log of the search network reports the error.

The examples on this page use the master node of a running search network. The following code creates and starts the master node as described on the Search network deployment page; the slave nodes must be started beforehand. The examples that follow continue this code in the same Python process.

from groupdocs.search.common import FileLogger
from groupdocs.search.scaling import SearchNetworkNode
from groupdocs.search.scaling.configuring import Configuration, TcpSettings

base_path = "./SearchNetwork/"
base_port = 49100

# Loading the search network configuration from a file
configuration = Configuration.load(base_path + "Configuration.xml")

# Creating, configuring and starting the master node; the timeouts are specified in milliseconds
node = SearchNetworkNode.create_master_node(
    0,
    base_path + "Node0",
    TcpSettings(base_port, 3000, 3000, 3000, 1000),
    FileLogger(base_path + "Log.txt", 4.0),
    configuration)
node.configure_all_nodes()
node.start()

The following sample file is used in this example: SearchNetwork/Configuration.xml

The following code example, which continues the previous one, demonstrates retrieving the text of a document indexed in the search network, as well as outputting the resulting data to the console.

from groupdocs.search.common import StringOutputAdapter
from groupdocs.search.options import OutputFormat

file_name = "archive-catalogue.pdf"  # The name of the document to get the text of

searcher = node.searcher

documents = []
shard_indices = node.get_shard_indices()
for shard_index in shard_indices:
    infos = searcher.get_indexed_documents(shard_index)
    documents.extend(infos)
    for info in infos:
        items = searcher.get_indexed_document_items(info)
        documents.extend(items)

for document in documents:
    if file_name in str(document.document_info):
        print()
        print(document.document_info)

        output_adapter = StringOutputAdapter(OutputFormat.PLAIN_TEXT)
        searcher.get_document_text(document, output_adapter)

        print(output_adapter.get_result())
        break

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 the full-featured Python 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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.