为了在Lucene查询返回的文档中显示突出显示的匹配单词,Lucene搜索结果可能包含用于将文档作为匹配我的请求的单词。
例如:
Lucene查询:"狗猫"
结果:[" 狗很好"" 狗和猫是朋友& #34;]
如何通过Lucene实现这一目标?我无法手动处理猫 s 或狗 s 或请求的字词与返回的字词之间的任何差异。
答案 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);