我们有一个很大的同义词列表。我使用手动分析器来索引搜索字段。同义词列表使用“ SynonymGraphFilterFactory ”过滤器进行注释。到目前为止一切都很好。当我在该字段上进行搜索时,我得到了匹配的结果。同义词列表如下所示:汽车、车辆
如果我在搜索中输入“汽车”,则会显示正确的结果并突出显示“汽车”一词。
当我输入“车辆”一词时,我得到了正确的结果,但没有突出显示。
我想在搜索中突出显示这两个词。“汽车”和“车辆”。这甚至可能吗?
到目前为止,我还没有找到合适的解决方案。也许有人可以在这里帮助我。
配置: Hibernate-search 6、Lucene Higlighter 8.7
代码:
要索引搜索字段,我的分析器如下所示:
context.analyzer ("myCustomAnalyzer"). custom ()
.tokenizer (StandardTokenizerFactory.class)
.tokenFilter (LowerCaseFilterFactory.class)
.tokenFilter (KeywordRepeatFilterFactory.class)
.tokenFilter (PorterStemFilterFactory.class)
.tokenFilter (TrimFilterFactory.class)
.tokenFilter (SnowballPorterFilterFactory.class) .param ("language", "German")
.tokenFilter (RemoveDuplicatesTokenFilterFactory.class)
.tokenFilter (SynonymGraphFilterFactory.class) .param ("synonyms", "synonyms / synonyms.properties")
.param ("ignoreCase", "true"). param ("expand", "true");
荧光笔方法如下所示:
private Results highlighting(final Results results, final String mySearchString) {
final SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("start", "end");
final TermQuery query = new TermQuery(
new Term("indexFieldName", mySearchString));
final QueryScorer queryScorer = new QueryScorer(query, "indexFieldName");
final Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer);
queryScorer.setExpandMultiTermQuery(true);
final Highlighter highlighter = new Highlighter(simpleHTMLFormatter, queryScorer);
highlighter.setTextFragmenter(fragmenter);
try (Analyzer analyzer = new StandardAnalyzer()) {
for (final MyEntity my : results.getMyResults()) {
for (final MySecondEntity sec : my.getMyDescriptions()) {
final String text = sec.getMyName();
try {
final TokenStream tokenStream = analyzer.tokenStream(
"indexFieldName", new StringReader(text));
final String result = highlighter.getBestFragments(
tokenStream, text,
sec.getMyName().length(), " ...");
if (!StringUtils.isBlank(result)) {
sec.setMyName(result);
}
} catch (final Exception e) {
LOG.warn(String.format(
"Failure during highlighting process for ..."...
}
}
}
}
return results;
}
谢谢您的回答