Example usage for org.apache.lucene.search.highlight Highlighter getBestFragment

List of usage examples for org.apache.lucene.search.highlight Highlighter getBestFragment

Introduction

In this page you can find the example usage for org.apache.lucene.search.highlight Highlighter getBestFragment.

Prototype

public final String getBestFragment(Analyzer analyzer, String fieldName, String text)
        throws IOException, InvalidTokenOffsetsException 

Source Link

Document

Highlights chosen terms in a text, extracting the most relevant section.

Usage

From source file:uk.ac.ebi.biostudies.efo.EFOExpandedHighlighter.java

License:Apache License

private String doHighlightQuery(Query query, String fieldName, String text, String openMark, String closeMark,
        boolean fragmentOnly) {
    try {/*from w  ww .  j a  v a 2 s. c o m*/
        SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(openMark, closeMark);
        QueryScorer scorer = new QueryScorer(query, fieldName, indexConfig.getDefaultField());
        Highlighter highlighter = new Highlighter(htmlFormatter, scorer);
        highlighter.setTextFragmenter(
                fragmentOnly ? new SimpleSpanFragmenter(scorer, indexConfig.getSearchSnippetFragmentSize())
                        : new NullFragmenter());
        String str = highlighter.getBestFragment(new ExperimentTextAnalyzer(),
                "".equals(fieldName) ? indexConfig.getDefaultField() : fieldName, text);
        return str;
    } catch (Exception x) {
        logger.error("Caught an exception:", x);
    }
    return text;

}