Search in sources :

Example 16 with SimpleHTMLFormatter

use of org.apache.lucene.search.highlight.SimpleHTMLFormatter in project ansj_seg by NLPchina.

the class IndexTest method toHighlighter.

/**
 * 高亮设置
 *
 * @param query
 * @param doc
 * @param field
 * @return
 */
private String toHighlighter(Analyzer analyzer, Query query, Document doc) {
    String field = "text";
    try {
        SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
        Highlighter highlighter = new Highlighter(simpleHtmlFormatter, new QueryScorer(query));
        TokenStream tokenStream1 = analyzer.tokenStream("text", new StringReader(doc.get(field)));
        String highlighterStr = highlighter.getBestFragment(tokenStream1, doc.get(field));
        return highlighterStr == null ? doc.get(field) : highlighterStr;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidTokenOffsetsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) StringReader(java.io.StringReader) IOException(java.io.IOException) SimpleHTMLFormatter(org.apache.lucene.search.highlight.SimpleHTMLFormatter) Highlighter(org.apache.lucene.search.highlight.Highlighter)

Example 17 with SimpleHTMLFormatter

use of org.apache.lucene.search.highlight.SimpleHTMLFormatter in project ansj_seg by NLPchina.

the class IndexAndTest method toHighlighter.

/**
 * 高亮设置
 *
 * @param query
 * @param doc
 * @param field
 * @return
 */
private String toHighlighter(Analyzer analyzer, Query query, Document doc) {
    String field = "text";
    try {
        SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
        Highlighter highlighter = new Highlighter(simpleHtmlFormatter, new QueryScorer(query));
        TokenStream tokenStream1 = analyzer.tokenStream("text", new StringReader(doc.get(field)));
        String highlighterStr = highlighter.getBestFragment(tokenStream1, doc.get(field));
        return highlighterStr == null ? doc.get(field) : highlighterStr;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidTokenOffsetsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) StringReader(java.io.StringReader) IOException(java.io.IOException) SimpleHTMLFormatter(org.apache.lucene.search.highlight.SimpleHTMLFormatter) Highlighter(org.apache.lucene.search.highlight.Highlighter)

Aggregations

SimpleHTMLFormatter (org.apache.lucene.search.highlight.SimpleHTMLFormatter)17 Highlighter (org.apache.lucene.search.highlight.Highlighter)16 QueryScorer (org.apache.lucene.search.highlight.QueryScorer)16 TokenStream (org.apache.lucene.analysis.TokenStream)13 IOException (java.io.IOException)12 StringReader (java.io.StringReader)10 InvalidTokenOffsetsException (org.apache.lucene.search.highlight.InvalidTokenOffsetsException)10 ParseException (org.apache.lucene.queryparser.classic.ParseException)4 Map (java.util.Map)3 Analyzer (org.apache.lucene.analysis.Analyzer)3 IndexReader (org.apache.lucene.index.IndexReader)3 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Query (org.apache.lucene.search.Query)3 SimpleFragmenter (org.apache.lucene.search.highlight.SimpleFragmenter)3 SimpleHTMLEncoder (org.apache.lucene.search.highlight.SimpleHTMLEncoder)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ArrayList (java.util.ArrayList)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Document (org.apache.lucene.document.Document)2