Text search in network

To run a text search, use the search_first method of the Searcher class. This method returns the first portion of results.

To continue the search, the search_next method is used, returning the second and subsequent portions of results. Continue the search while the next_chunk_search_token property of the result is not None.

Text and object forms of the search query are supported when appropriate overloads of the search_first method are used. The options for a network search are the same as those for a single index search, but a search network performs only chunk search: set the is_chunk_search option to True, otherwise the search_first method raises ValueError.

Each portion of results is returned as a NetworkSearchResult object, which has the following members:

A found document is represented by the NetworkFoundDocument class, which contains the found_document object, as in a single index search, and the index of the shard where the document was found (shard_index property).

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 performing a full-text search on the search network.

from groupdocs.search import SearchQuery
from groupdocs.search.options import SearchOptions

word = "einstein"
use_synonym_search = True

searcher = node.searcher

query = SearchQuery.create_word_query(word)
print()
print("First time search for: " + str(query))
options = SearchOptions()
options.is_chunk_search = True
options.use_synonym_search = use_synonym_search
total_occurrences = 0
documents = []

result = searcher.search_first(query, options)

documents.extend(result)
total_occurrences += result.occurrence_count
print("Documents found (node %d): %d" % (result.node_index, result.document_count))

while result.next_chunk_search_token is not None:
    print()
    print("Next time search for: " + str(query))

    result = searcher.search_next(result.next_chunk_search_token)

    documents.extend(result)
    total_occurrences += result.occurrence_count
    print("Documents found (node %d): %d" % (result.node_index, result.document_count))

print()
print("Total occurrences: %d" % total_occurrences)
for document in documents:
    print("%d: %s (%d occurrences)" % (document.shard_index,
                                       document.found_document.document_info.file_path,
                                       document.found_document.occurrence_count))

More information about search queries can be found on the Queries in text and object form page.

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.