使用Elasticsearch RestHighLevelClient的汇总日期出现重复的日期如何处理?
我正在使用Elasticsearch RestHighLevelClient并尝试在下面实现sql:
选择格式(日期,'yyyy-MM-dd'),从订单组中按格式(日期,'yyyy-MM-dd')计数(*)
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
sourceBuilder.aggregation(AggregationBuilders.terms("date_count").field("time").format("yyyy-MM-dd"));
SearchRequest searchRequest = new SearchRequest("order_*");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
我得到了结果,但是有很多重复的日期结果,例如:
buckets:
bucket1:
key: 1583390009000
keyAsString: 2020-03-05
docCount: 4
bucket2:
key: 1583391748000
keyAsString: 2020-03-05
docCount: 5
我应该如何避免重复?我的意思是同一日期(例如2020-03-05)算作一个时段。
问题来源:stackoverflow
应该使用日期直方图聚合
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//sourceBuilder.aggregation(AggregationBuilders.terms("date_count").field("time").format("yyyy-MM-dd"));
sourceBuilder.aggregation(AggregationBuilders.dateHistogram("date_count").calendarInterval(DateHistogramInterval.DAY).field("time"));
SearchRequest searchRequest = new SearchRequest("order_*");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
回答来源stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。