常用的聚合函数

一、histogram

1、histogram:直方图、柱状图

2、

 1 GET product/_search
 2 {
 3   "size": 0, 
 4   "aggs": {
 5     "price_histogram": {
 6       "histogram": {
 7         "field": "price",
 8         "interval": 1000,     间隔
 9         "keyed": true,        查询结果新增key值
10         "min_doc_count": 1,   如果该值为0,查询结果区间无值直方图中不显示该区间;如果设置为1,查询结果区间无值的范围也会显示出来
11         "missing": 1999       如果price字段值为空,则用1999进行补充
12       }
13     }
14   }
15 }
#日期直方图 
1 GET product/_search
 2 {
 3   "size": 0,
 4   "aggs": {
 5     "my_date_histogram": {
 6       "date_histogram": {
 7         "field": "createtime",
 8         "calendar_interval": "month",
 9         "format": "yyyy-MM",
10         "extended_bounds": {        如果es现有数据范围不在这个范围内可以使用此参数,不在范围内的也会显示,只不过count为0
11           "min": "2020-01",
12           "max": "2020-12"
13         },
14         "min_doc_count": 0,         与extended_bounds参数一起使用时,此参数必须设置为0;当设置为1时与上一参数冲突
15         "order": {
16           "_count": "desc"
17         }
18       },
19       "aggs": {
20         "sum_agg": {
21           "sum": {
22             "field": "price"
23           }
24         },
25         "my_cumulative_sum": {      
26           "cumulative_sum": {       累加,该时间段之前所有金额的累加
27             "buckets_path": "sum_agg"
28           }
29         }
30       }
31     }
32   }
33 }

二、百分位统计

 1 GET product/_search
 2 {
 3   "size": 0, 
 4   "aggs": {
 5     "price_percentiles": {
 6       "percentiles": {
 7         "field": "price",
 8         "percents": [
 9           1,        百分之一的价格在xxx下
10           5,        百分之五的价格在xxx下
11           25,       。。。。
12           50,
13           75,
14           95,
15           99
16         ]
17       }
18     }
19   }
20 }

 

posted @ 2022-02-11 21:54  showMeTheCodes  阅读(135)  评论(0编辑  收藏  举报