Java Code Examples for org.apache.lucene.search.highlight.Fragmenter

The following examples show how to use org.apache.lucene.search.highlight.Fragmenter. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: RegexFragmenter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Fragmenter getFragmenter(String fieldName, SolrParams params )
{ 
  numRequests.inc();
  params = SolrParams.wrapDefaults(params, defaults);

  int fragsize  = params.getFieldInt(   fieldName, HighlightParams.FRAGSIZE,  LuceneRegexFragmenter.DEFAULT_FRAGMENT_SIZE );
  int increment = params.getFieldInt(   fieldName, HighlightParams.INCREMENT, LuceneRegexFragmenter.DEFAULT_INCREMENT_GAP );
  float slop    = params.getFieldFloat( fieldName, HighlightParams.SLOP,      LuceneRegexFragmenter.DEFAULT_SLOP );
  int maxchars  = params.getFieldInt(   fieldName, HighlightParams.MAX_RE_CHARS, LuceneRegexFragmenter.DEFAULT_MAX_ANALYZED_CHARS );
  String rawpat = params.getFieldParam( fieldName, HighlightParams.PATTERN,   LuceneRegexFragmenter.DEFAULT_PATTERN_RAW );

  Pattern p = rawpat == defaultPatternRaw ? defaultPattern : Pattern.compile(rawpat);

  if( fragsize <= 0 ) {
    return new NullFragmenter();
  }
  
  return new LuceneRegexFragmenter( fragsize, increment, slop, maxchars, p );
}
 
Example 2
Source Project: lumongo   Source File: LumongoSegment.java    License: Apache License 2.0 6 votes vote down vote up
private List<LumongoHighlighter> getHighlighterList(List<HighlightRequest> highlightRequests, Query q) {

		if (highlightRequests.isEmpty()) {
			return Collections.emptyList();
		}

		List<LumongoHighlighter> highlighterList = new ArrayList<>();

		for (HighlightRequest highlight : highlightRequests) {
			QueryScorer queryScorer = new QueryScorer(q, highlight.getField());
			queryScorer.setExpandMultiTermQuery(true);
			Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer, highlight.getFragmentLength());
			SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(highlight.getPreTag(), highlight.getPostTag());
			LumongoHighlighter highlighter = new LumongoHighlighter(simpleHTMLFormatter, queryScorer, highlight);
			highlighter.setTextFragmenter(fragmenter);
			highlighterList.add(highlighter);
		}
		return highlighterList;
	}
 
Example 3
Source Project: cjs_ssms   Source File: SearcherTest.java    License: 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 Project: lucene-solr   Source File: GapFragmenter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Fragmenter getFragmenter(String fieldName, SolrParams params )
{
  numRequests.inc();
  params = SolrParams.wrapDefaults(params, defaults);
  
  int fragsize = params.getFieldInt( fieldName, HighlightParams.FRAGSIZE, 100 );
  return (fragsize <= 0) ? new NullFragmenter() : new LuceneGapFragmenter(fragsize);
}
 
Example 5
Source Project: lucene-solr   Source File: DefaultSolrHighlighter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Return a {@link org.apache.lucene.search.highlight.Fragmenter} appropriate for this field. If a fragmenter
 * has not been configured for this field, fall back to the configured
 * default or the solr default ({@link GapFragmenter}).
 *
 * @param fieldName The name of the field
 * @param params    The params controlling Highlighting
 * @return An appropriate {@link org.apache.lucene.search.highlight.Fragmenter}.
 */
protected Fragmenter getFragmenter(String fieldName, SolrParams params) {
  String fmt = params.getFieldParam(fieldName, HighlightParams.FRAGMENTER);
  SolrFragmenter frag = fragmenters.get(fmt);
  if (frag == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown fragmenter: " + fmt);
  }
  return frag.getFragmenter(fieldName, params);
}
 
Example 6
Source Project: lucene-solr   Source File: SolrFragmenter.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Return a {@link org.apache.lucene.search.highlight.Fragmenter} appropriate for this field. 
 * 
 * @param fieldName The name of the field
 * @param params The params controlling Highlighting
 * @return An appropriate {@link org.apache.lucene.search.highlight.Fragmenter}.
 */
public Fragmenter getFragmenter(String fieldName, SolrParams params);