如何将ElasticSearchjson查询转换为等价的Java API?

tktrz96b  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(162)

我有下面的ElasticSearchJSON查询,并希望将其转换为等价的Java API。我如何用ElasticSearchJava API转换它?

{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "min": {
      "min": {
        "field": "<The date>"
      }
    },
    "max":{
      "max": {
        "field": "<The date>"
      }
    }
  }
}

我曾尝试使用MaxAggregationBuilderMinAggregationBuilder,但在这种情况下,我必须执行两个单独的API调用,一个用于Max,另一个用于Min。

MaxAggregationBuilder=AggregationBuilders.max("max").field("date");
MinAggregationBuilder=AggregationBuilders.max("min").field("date");

我如何在一个API调用中实现这一点?

gfttwv5a

gfttwv5a1#

这两个语句不是两个API调用,它们只是构建器的调用语句,构建器构建您将在一个API调用中发送的查询:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
// query part
searchSourceBuilder.query(QueryBuilders.matchAllQuery()); 

// aggregation part
searchSourceBuilder.aggregation(AggregationBuilders.max("max").field("date"));
searchSourceBuilder.aggregation(AggregationBuilders.max("min").field("date"));

// request part
SearchRequest searchRequest = new SearchRequest(); 
searchRequest.source(searchSourceBuilder); 

// API call
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

相关问题