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 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 following code example demonstrates highlighting search results on the search network.

Searcher searcher = node.getSearcher();

FragmentHighlighter highlighter = new FragmentHighlighter(OutputFormat.PlainText);

HighlightOptions options = new HighlightOptions();
options.setTermsAfter(5);
options.setTermsBefore(5);
options.setTermsTotal(15);
searcher.highlight(document, highlighter, options);

FragmentContainer[] result = highlighter.getResult();
System.out.println();
for (FragmentContainer container : result) {
    if (container.getCount() == 0) continue;

    String[] fragments = container.getFragments();
    System.out.println(container.getFieldName());
    int count = Math.min(fragments.length, maxFragments);
    for (int j = 0; j < count; j++) {
        System.out.println("\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 full featured .NET 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.