ElasticSearch搜索查询,每个索引带有一个荧光笔

1cosmwyk  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(2)|浏览(133)

当从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***的调用只是覆盖了以前的荧光笔,所以这不会起作用。有什么方法可以做到这一点吗?

68bkxrlz

68bkxrlz1#

你不需要示例化一个以上的HighlightBuilder。HighlightBuilder使用模式生成器,要应用更多的点,只需在同一个HighlightBuilder示例中添加“字段”。

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 highlightBuilder = new HighlightBuilder().field("field1").field("field2").field("field3");

// Add highlighters to the search source for each index
searchSourceBuilder.highlighter(highlightBuilder);

// Set the search source on the search request
searchRequest.source(searchSourceBuilder);
disbfnqx

disbfnqx2#

最好使用HighlightBuilder和requireFieldMatch示例(在代码中使用dpend)

// 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 a highlighter for each index
HighlightBuilder highlightBuilder1 = new HighlightBuilder().field("field1");
HighlightBuilder highlightBuilder2 = new HighlightBuilder().field("field2");
HighlightBuilder highlightBuilder3 = new HighlightBuilder().field("field3");

// Create a map of index name to highlighter
Map<String, HighlightBuilder> highlighters = new HashMap<>();
highlighters.put("index1", highlightBuilder1);
highlighters.put("index2", highlightBuilder2);
highlighters.put("index3", highlightBuilder3);

// Add highlighters to the search request for each index
searchSourceBuilder.highlighter(highlighters);

// Set the search source on the search request
searchRequest.source(searchSourceBuilder);

您可以为每个索引创建一个HighlightBuilder,并使用highlighter方法将其添加到搜索请求中,该方法接受Map〈String,HighlightBuilder〉

相关问题