I manage to create a working query but I'm having hard time converting it to java.
Working query: GET edgedevicedata/_search { "size": 0, "aggs": { "by_days": { "date_histogram": { "field": "@timestamp", "fixed_interval": "3d" }, "aggs": { "totalValue": { "sum": { "field": "value" } },"minValue": { "min": { "field": "value" } },"maxValue": { "max": { "field": "value" } },"avgValue": { "avg": { "field": "value" } } } } } }
My java code; `RestHighLevelClient client = new RestHighLevelClient(builder);
//range query for start and end date
QueryBuilder rangeQuery = QueryBuilders
.rangeQuery("time")
.from("0")
.to("999999999999999999")
.includeLower(false)
.includeUpper(false);
//term query for sensorId (sensor type)
QueryBuilder termsQuery = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("sensorId","1"));
//term query for deviceId
QueryBuilder termsQuery2 = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("deviceId","device1"));
//combining 3 querries
QueryBuilder qb = QueryBuilders
.boolQuery()
.must(rangeQuery)
.must(termsQuery)
.must(termsQuery2);
//creating search request
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(qb);
searchSourceBuilder.from(0);
searchSourceBuilder.size(1000);
DateHistogramAggregationBuilder dateAggregation=
AggregationBuilders.dateHistogram("date_aggregation")
.field("timestamp")
.fixedInterval(DateHistogramInterval.days(1))
.subAggregation(AggregationBuilders.max("maxValue")
.field("value"))
.subAggregation(AggregationBuilders.min("minValue")
.field("value"))
.subAggregation(AggregationBuilders.avg("avgValue")
.field("value"))
.subAggregation(AggregationBuilders.sum("totalValue")
.field("value"));
searchSourceBuilder.aggregation(dateAggregation);
searchRequest.source(searchSourceBuilder);
//setting up for response
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//getting gits
SearchHits hits = searchResponse.getHits();`
Expected output example: "aggregations" : { "by_minute" : { "buckets" : [ { "key_as_string" : "2019-12-28T00:00:00.000Z", "key" : 1577491200000, "doc_count" : 6, "totalValue" : { "value" : 125.0 }, "avgValue" : { "value" : 20.833333333333332 }, "minValue" : { "value" : 15.0 }, "maxValue" : { "value" : 29.0 } }
Output im getting: { "_index" : "edgedevicedata", "_type" : "_doc", "_id" : "rDryZ28BtB64rZAMaqqO", "_score" : 3.0, "_source" : { "sensorId" : 1, "deviceId" : "device1", "time" : 1577996937761, "value" : 29.0, "@version" : "1", "@timestamp" : "2020-01-02T20:28:57.766Z" } }
I'm sorry about the format of queries but I don't know how to make them look good.