【ElasticSearch】聚合
【ElasticSearch】聚合
==========================================================
1、基本聚合
2、统计null值
==========================================================
1、基本聚合
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0); TermsAggregationBuilder aggregation = AggregationBuilders.terms("group").field("createBy").size(5000); searchSourceBuilder.aggregation(aggregation); MissingAggregationBuilder missing = AggregationBuilders.missing("nullValue").field("createBy"); searchSourceBuilder.aggregation(missing);
{ "size": 0, "query": { "bool": { "must": [ { "term": { "status": { "value": 3, "boost": 1 } } }, { "range": { "createTime": { "from": "2020-12-01 00:00:00", "to": "2021-05-11 23:59:59", "include_lower": true, "include_upper": false, "boost": 1 } } } ] } }, "aggregations": { "createByGroup": { // 聚合名称 "terms": { "field": "createBy", // 聚合字段 "size": 5000, "min_doc_count": 1, "shard_min_doc_count": 0, "show_term_doc_count_error": false, "order": [ // 聚合排序 { "_count": "desc" }, { "_key": "asc" } ] } }, "nullCreateBy": {// 统计值为null的聚合 "missing": { "field": "createBy"// null值字段 } } } }
2、统计null值
MissingAggregationBuilder missing = AggregationBuilders.missing("nullValue").field("createBy");
searchSourceBuilder.aggregation(missing);
"aggregations": { "nullValue": { "missing": { "field": "createBy" } } }
聚合排序
{ "size": 0, "query": { "bool": { "must": [ { "term": { "position": { "value": "1227880568159477762" } } }, { "range": { "date_time": { "from": "2020-03-21 17:26:00", "to": "2020-03-22 17:26:59", "include_lower": true, "include_upper": true, "boost": 1 } } } ] } }, "sort": [ { "date_time": { "order": "desc" } } ], "aggs": { "group_minute": { "date_histogram": { "field": "date_time", "interval": "minute", "format": "yyyy-MM-dd HH:mm", "order": { "_key": "desc" } }, "aggs": { "avg_value": { "avg": { "field": "value" } } } } } }