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.
fromgroupdocs.search.commonimportFileLoggerfromgroupdocs.search.scalingimportSearchNetworkNodefromgroupdocs.search.scaling.configuringimportConfiguration,TcpSettingsbase_path="./SearchNetwork/"base_port=49100# Loading the search network configuration from a fileconfiguration=Configuration.load(base_path+"Configuration.xml")# Creating, configuring and starting the master node; the timeouts are specified in millisecondsnode=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 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.
fromgroupdocs.search.highlightersimportFragmentHighlighterfromgroupdocs.search.optionsimportHighlightOptions,OutputFormat,SearchOptionsquery="Einstein"max_fragments=5# The maximum number of fragments to output for each fieldsearcher=node.searcher# Searching in the search networksearch_options=SearchOptions()search_options.is_chunk_search=Truesearch_result=searcher.search_first(query,search_options)whilesearch_result.document_count==0andsearch_result.next_chunk_search_tokenisnotNone:search_result=searcher.search_next(search_result.next_chunk_search_token)ifsearch_result.document_count>0:# Getting the first found documentdocument=search_result.get_found_document(0)highlighter=FragmentHighlighter(OutputFormat.PLAIN_TEXT)options=HighlightOptions()options.terms_after=5options.terms_before=5options.terms_total=15searcher.highlight(document,highlighter,options)result=highlighter.get_result()print()forcontainerinresult:ifcontainer.count==0:continuefragments=container.get_fragments()print(container.field_name)count=min(len(fragments),max_fragments)forjinrange(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: