public class HtmlFragmentHighlighter extends Highlighter
Represents a search result highlighter that highlights search results in HTML formatted text fragments.
Learn more
The example demonstrates a typical usage of the class.
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);
HtmlFragmentHighlighter highlighter = new HtmlFragmentHighlighter();
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();
}
}
}
Constructor and Description |
---|
HtmlFragmentHighlighter()
Initializes a new instance of the
HtmlFragmentHighlighter class. |
Modifier and Type | Method and Description |
---|---|
protected Object |
createResultBuilder(TextOptions options) |
FragmentContainer[] |
getResult()
Gets an array of resulting fragment containers.
|
createResultBuilder
public HtmlFragmentHighlighter()
Initializes a new instance of the HtmlFragmentHighlighter
class.
public final FragmentContainer[] getResult()
Gets an array of resulting fragment containers.
protected Object createResultBuilder(TextOptions options)
createResultBuilder
in class ResultBuilderFactory