FragmentHighlighter

Inheritance: java.lang.Object, com.groupdocs.search.common.ResultBuilderFactory, com.groupdocs.search.highlighters.Highlighter

All Implemented Interfaces: com.groupdocs.search.highlighters.IFragmentHighlighter

public class FragmentHighlighter extends Highlighter implements IFragmentHighlighter

Represents a search result highlighter that highlights search results in text fragments.

Learn more


 String indexFolder = "c:\\MyIndex\\";
 String documentsFolder = "c:\\MyDocuments\\";
 // Creating an index
 Index index = new Index(indexFolder);
 // Indexing documents from the specified folder
 index.add(documentsFolder);
 // Search for the word 'Einstein'
 SearchResult result = index.search("Einstein");
 // Assigning highlight options
 HighlightOptions options = new HighlightOptions();
 options.setTermsBefore(5);
 options.setTermsAfter(5);
 options.setTermsTotal(15);
 // Highlighting found words in the text of a document
 FoundDocument document = result.getFoundDocument(0);
 FragmentHighlighter highlighter = new FragmentHighlighter(OutputFormat.Html);
 index.highlight(document, highlighter, options);
 // Getting the result
 FragmentContainer[] fragmentContainers = highlighter.getResult();
 for (int i = 0; i < fragmentContainers.length; i++) {
     FragmentContainer container = fragmentContainers[i];
     String[] fragments = container.getFragments();
     if (fragments.length > 0) {
         System.out.println(container.getFieldName());
         System.out.println();
         for (int j = 0; j < fragments.length; j++) {
             // Printing HTML markup to console
             System.out.println(fragments[j]);
             System.out.println();
         }
     }
 }
 

Constructors

Constructor Description
FragmentHighlighter(OutputFormat outputFormat) Initializes a new instance of the FragmentHighlighter class.

Methods

Method Description
getResult() Gets an array of resulting fragment containers.

FragmentHighlighter(OutputFormat outputFormat)

public FragmentHighlighter(OutputFormat outputFormat)

Initializes a new instance of the FragmentHighlighter class.

Parameters:

Parameter Type Description
outputFormat OutputFormat The output format.

getResult()

public final FragmentContainer[] getResult()

Gets an array of resulting fragment containers.

Returns: com.groupdocs.search.common.FragmentContainer[] - An array of resulting fragment containers.