Highlighting results in network

Search results in the search network can be highlighted in the text of the found document using the highlight method of the Searcher class. The found document is passed to the method as a NetworkFoundDocument object obtained from a search result.

The set of available classes for highlighting text, as well as the highlighting options, are identical to those used for highlighting search results in a single index. Please see also the Highlighting search results page. Highlighting needs the extracted text of the document, so the text storage must be enabled in the configuration of the search network; otherwise, the highlighter returns no fragments, 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 highlighting search results on the search network. A search network performs only chunk search, so the example sets the is_chunk_search option and takes the first found document from the first portion of results that contains documents.

from groupdocs.search.highlighters import FragmentHighlighter
from groupdocs.search.options import HighlightOptions, OutputFormat, SearchOptions

query = "Einstein"
max_fragments = 5  # The maximum number of fragments to output for each field

searcher = node.searcher

# Searching in the search network
search_options = SearchOptions()
search_options.is_chunk_search = True
search_result = searcher.search_first(query, search_options)
while search_result.document_count == 0 and search_result.next_chunk_search_token is not None:
    search_result = searcher.search_next(search_result.next_chunk_search_token)
if search_result.document_count > 0:
    # Getting the first found document
    document = search_result.get_found_document(0)

    highlighter = FragmentHighlighter(OutputFormat.PLAIN_TEXT)

    options = HighlightOptions()
    options.terms_after = 5
    options.terms_before = 5
    options.terms_total = 15
    searcher.highlight(document, highlighter, options)

    result = highlighter.get_result()
    print()
    for container in result:
        if container.count == 0:
            continue

        fragments = container.get_fragments()
        print(container.field_name)
        count = min(len(fragments), max_fragments)
        for j in range(count):
            print("\t" + fragments[j])

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.