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.
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.
C#
stringbasePath=@"c:\SearchNetwork\";intbasePort=49100;// Loading the search network configuration from a fileConfigurationconfiguration=Configuration.Load(basePath+"Configuration.xml");// Creating, configuring and starting the master node; the timeouts are specified in millisecondsSearchNetworkNodenode=SearchNetworkNode.CreateMasterNode(0,basePath+"Node0",newTcpSettings(basePort,3000,3000,3000,1000),newFileLogger(basePath+"Log.txt",4.0),configuration);node.ConfigureAllNodes();node.Start();
The following code example, which continues the previous one, demonstrates highlighting search results on the search network.
C#
stringquery="Einstein";intmaxFragments=5;// The maximum number of fragments to output for each fieldSearchersearcher=node.Searcher;// Searching in the search networkNetworkSearchResultsearchResult=searcher.SearchFirst(query,newSearchOptions());if(searchResult.DocumentCount>0){// Getting the first found documentNetworkFoundDocumentdocument=searchResult.GetFoundDocument(0);FragmentHighlighterhighlighter=newFragmentHighlighter(OutputFormat.PlainText);HighlightOptionsoptions=newHighlightOptions();options.TermsAfter=5;options.TermsBefore=5;options.TermsTotal=15;searcher.Highlight(document,highlighter,options);FragmentContainer[]result=highlighter.GetResult();Console.WriteLine();for(inti=0;i<result.Length;i++){FragmentContainercontainer=result[i];if(container.Count==0)continue;string[]fragments=container.GetFragments();Console.WriteLine(container.FieldName);intcount=Math.Min(fragments.Length,maxFragments);for(intj=0;j<count;j++){Console.WriteLine("\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: