Lucene查询结果:获取查询

时间:2015-12-13 22:36:57

标签: lucene full-text-search

为了在Lucene查询返回的文档中显示突出显示的匹配单词,Lucene搜索结果可能包含用于将文档作为匹配我的请求的单词。

例如:

  • Lucene查询:"狗猫"

  • 结果:[" 很好"" 是朋友& #34;]

如何通过Lucene实现这一目标?我无法手动处理猫 s 或狗 s 或请求的字词与返回的字词之间的任何差异。

1 个答案:

答案 0 :(得分:1)

使用Lucene的Highlighter。像这样:

//By default, this formatter will wrap highlights with <b>, but that is configurable.
Formatter formatter = new SimpleHTMLFormatter(); 
QueryScorer scorer = new QueryScorer(query);
Highlighter highlighter = new Highlighter(formatter, queryScorer);
//You can set a fragmenter as well, by default it will split into fragments 100 chars in size, using SimpleFragmenter.

String highlightedSnippet = highlighter.getBestFragment(myAnalyzer, fieldName, fieldContent);