Java Code Examples for org.apache.lucene.search.highlight.Highlighter#setTextFragmenter()

The following examples show how to use org.apache.lucene.search.highlight.Highlighter#setTextFragmenter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlfrescoSolrHighlighter.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected Highlighter getHighlighter(Query query, String requestFieldname, SolrQueryRequest request)
{
	String schemaFieldName =
			AlfrescoSolrDataModel.getInstance()
				.mapProperty(requestFieldname, FieldUse.HIGHLIGHT, request);

	Highlighter highlighter =
			new Highlighter(
					getFormatter(requestFieldname, request.getParams()),
					getEncoder(requestFieldname, request.getParams()),
					getQueryScorer(query,schemaFieldName, request));

	highlighter.setTextFragmenter(getFragmenter(requestFieldname, request.getParams()));
	return highlighter;
}
 
Example 2
Source File: HighlightCustomQueryTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * This method intended for use with
 * <code>testHighlightingWithDefaultField()</code>
 */
private String highlightField(Query query, String fieldName,
    String text) throws IOException, InvalidTokenOffsetsException {
  try (MockAnalyzer mockAnalyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE,true,
      MockTokenFilter.ENGLISH_STOPSET); TokenStream tokenStream = mockAnalyzer.tokenStream(fieldName, text)) {
    // Assuming "<B>", "</B>" used to highlight
    SimpleHTMLFormatter formatter = new SimpleHTMLFormatter();
    MyQueryScorer scorer = new MyQueryScorer(query, fieldName, FIELD_NAME);
    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.setTextFragmenter(new SimpleFragmenter(Integer.MAX_VALUE));

    String rv = highlighter.getBestFragments(tokenStream, text, 1,
        "(FIELD TEXT TRUNCATED)");
    return rv.length() == 0 ? text : rv;
  }
}
 
Example 3
Source File: SearcherTest.java    From cjs_ssms with GNU General Public License v2.0 5 votes vote down vote up
public static void search(String indexDir, String q) throws Exception {
  Directory dir = FSDirectory.open(Paths.get(indexDir));
  IndexReader reader = DirectoryReader.open(dir);
  IndexSearcher is = new IndexSearcher(reader);
  // Analyzer analyzer=new StandardAnalyzer(); // 标准分词器
  SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
  QueryParser parser = new QueryParser("desc", analyzer);
  Query query = parser.parse(q);

  long start = System.currentTimeMillis();
  TopDocs hits = is.search(query, 10);
  long end = System.currentTimeMillis();
  System.out.println("匹配 " + q + " ,总共花费" + (end - start) + "毫秒" + "查询到" + hits.totalHits + "个记录");

  QueryScorer scorer = new QueryScorer(query);
  Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
  SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
  Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer);
  highlighter.setTextFragmenter(fragmenter);
  for (ScoreDoc scoreDoc : hits.scoreDocs) {
    Document doc = is.doc(scoreDoc.doc);
    System.out.println(doc.get("city"));
    System.out.println(doc.get("desc"));
    String desc = doc.get("desc");
    if (desc != null) {
      TokenStream tokenStream = analyzer.tokenStream("desc", new StringReader(desc));
      System.out.println(highlighter.getBestFragment(tokenStream, desc));
    }
  }
  reader.close();
}
 
Example 4
Source File: DefaultSolrHighlighter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Return a phrase {@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
 * @param tokenStream document text tokenStream that implements reset() efficiently (e.g. CachingTokenFilter).
 *                    If it's used, call reset() first.
 * @throws IOException If there is a low-level I/O error.
 */
protected Highlighter getPhraseHighlighter(Query query, String fieldName, SolrQueryRequest request, TokenStream tokenStream) throws IOException {
  SolrParams params = request.getParams();
  Highlighter highlighter = new Highlighter(
          getFormatter(fieldName, params),
          getEncoder(fieldName, params),
          getSpanQueryScorer(query, fieldName, tokenStream, request));

  highlighter.setTextFragmenter(getFragmenter(fieldName, params));

  return highlighter;
}
 
Example 5
Source File: DefaultSolrHighlighter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}