Search in sources :

Example 11 with Highlighter

use of org.apache.lucene.search.highlight.Highlighter 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)

Example 12 with Highlighter

use of org.apache.lucene.search.highlight.Highlighter 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 13 with Highlighter

use of org.apache.lucene.search.highlight.Highlighter in project jforum2 by rafaelsteil.

the class LuceneContentCollector method retrieveRealPosts.

private List retrieveRealPosts(int[] postIds, Query query) throws IOException {
    List posts = DataAccessDriver.getInstance().newLuceneDAO().getPostsData(postIds);
    for (Iterator iter = posts.iterator(); iter.hasNext(); ) {
        Post post = (Post) iter.next();
        Scorer scorer = new QueryScorer(query);
        Highlighter highlighter = new Highlighter(scorer);
        TokenStream tokenStream = this.settings.analyzer().tokenStream(SearchFields.Indexed.CONTENTS, new StringReader(post.getText()));
        String fragment = highlighter.getBestFragment(tokenStream, post.getText());
        post.setText(fragment != null ? fragment : post.getText());
    }
    return posts;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) Post(net.jforum.entities.Post) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) Iterator(java.util.Iterator) StringReader(java.io.StringReader) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) Scorer(org.apache.lucene.search.highlight.Scorer) List(java.util.List) Highlighter(org.apache.lucene.search.highlight.Highlighter)

Example 14 with Highlighter

use of org.apache.lucene.search.highlight.Highlighter in project lucene-solr by apache.

the class TermVectorReusingLeafReader method getHighlighter.

/**
   * Return a {@link org.apache.lucene.search.highlight.Highlighter} appropriate for this field.
   * @param query The current Query
   * @param fieldName The name of the field
   * @param request The current SolrQueryRequest
   */
protected Highlighter getHighlighter(Query query, String fieldName, SolrQueryRequest request) {
    SolrParams params = request.getParams();
    Highlighter highlighter = new Highlighter(getFormatter(fieldName, params), getEncoder(fieldName, params), getQueryScorer(query, fieldName, request));
    highlighter.setTextFragmenter(getFragmenter(fieldName, params));
    return highlighter;
}
Also used : SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) Highlighter(org.apache.lucene.search.highlight.Highlighter) FastVectorHighlighter(org.apache.lucene.search.vectorhighlight.FastVectorHighlighter)

Aggregations

Highlighter (org.apache.lucene.search.highlight.Highlighter)14 TokenStream (org.apache.lucene.analysis.TokenStream)10 QueryScorer (org.apache.lucene.search.highlight.QueryScorer)10 SimpleHTMLFormatter (org.apache.lucene.search.highlight.SimpleHTMLFormatter)10 IOException (java.io.IOException)8 StringReader (java.io.StringReader)8 InvalidTokenOffsetsException (org.apache.lucene.search.highlight.InvalidTokenOffsetsException)8 FastVectorHighlighter (org.apache.lucene.search.vectorhighlight.FastVectorHighlighter)3 MapSolrParams (org.apache.solr.common.params.MapSolrParams)3 SolrParams (org.apache.solr.common.params.SolrParams)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Post (net.jforum.entities.Post)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 CachingTokenFilter (org.apache.lucene.analysis.CachingTokenFilter)1 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)1