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 one for Min.
MaxAggregationBuilder=AggregationBuilders.max("max").field("date");
MinAggregationBuilder=AggregationBuilders.max("min").field("date");
How can I do this in one API call itself?
CodePudding user response:
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);