开发者

How to convert the elastic search json query into equivalent Java API?

开发者 https://www.devze.com 2022-12-07 21:52 出处:网络
I have the below elastic search JSON Query and want to convert it into equivalent Java API. How can I convert this with Elastic Search Java API?

I have the below elastic search JSON Query and want to convert it into equivalent Java API. How can I convert this with Elastic Search Java API?

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

I had tried using MaxAggregationBuilder and MinAggregationBuilder, but in that case I had to do two seperate API calls , one for Max and the another o开发者_如何学Pythonne for Min.

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

How can I do this in one API call itself?


Those two statements are not two API calls, they are just call statements of a builder that builds up the query that you're going to send in one API call:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消