当从Java代码执行ElasticSearch搜索操作时,通常您会创建一个SearchRequest,其中包含一些索引作为参数,因为搜索是在多个索引中完成的。现在我希望能够为每个索引使用单独的荧光笔,例如:
// Define the indices to search on
String[] indices = {"index1", "index2", "index3"};
// Build the search request
SearchRequest searchRequest = new SearchRequest(indices);
// Build the search source
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("field", "search term"));
// Build highlighters for each index
HighlightBuilder highlightBuilder1 = new HighlightBuilder().field("field1");
HighlightBuilder highlightBuilder2 = new HighlightBuilder().field("field2");
HighlightBuilder highlightBuilder3 = new HighlightBuilder().field("field3");
// Add highlighters to the search source for each index
searchSourceBuilder.highlighter(highlightBuilder1);
searchSourceBuilder.highlighter(highlightBuilder2);
searchSourceBuilder.highlighter(highlightBuilder3);
// Set the search source on the search request
searchRequest.source(searchSourceBuilder);
但是,对***searchSourceBuilder.highlighter***的调用只是覆盖了以前的荧光笔,所以这不会起作用。有什么方法可以做到这一点吗?
2条答案
按热度按时间68bkxrlz1#
你不需要示例化一个以上的HighlightBuilder。HighlightBuilder使用模式生成器,要应用更多的点,只需在同一个HighlightBuilder示例中添加“字段”。
disbfnqx2#
最好使用HighlightBuilder和requireFieldMatch示例(在代码中使用dpend)
您可以为每个索引创建一个HighlightBuilder,并使用highlighter方法将其添加到搜索请求中,该方法接受Map〈String,HighlightBuilder〉