概述
继续跟中华石杉老师学习ES,第36篇
课程地址: https://www.roncoo.com/view/55
官方说明
Histogram Aggregation: 戳这里
histogram:类似于terms,也是进行bucket分组操作,接收一个field,按照这个field的值的各个范围区间,进行bucket分组操作 .
"histogram":{ "field": "price", "interval": 2000 },
interval:2000,划分范围,0~2000,2000~4000,4000~6000,6000~8000,8000~10000类似的,每个范围对应一个bucket
根据price的值,比如2500,看落在哪个区间内,落在2000~4000,此时就会将这条数据放入2000~4000对应的那个bucket中
histogram也是bucket划分的一种方法,就好比terms,将field值相同的数据划分到一个bucket中
bucket有了之后,就可以对每个bucket执行avg,count,sum,max,min,等各种metric操作,聚合分析
案例
需求: 按价格区间统计电视销量和销售额
原始数据:
DSL:
GET /tvs/sales/_search { "size" : 0, "aggs":{ "price":{ "histogram":{ "field": "price", "interval": 2000 }, "aggs":{ "revenue": { "sum": { "field" : "price" } } } } } }
返回数据
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "price": { "buckets": [ { "key": 0, "doc_count": 3, "revenue": { "value": 3700 } }, { "key": 2000, "doc_count": 4, "revenue": { "value": 9500 } }, { "key": 4000, "doc_count": 0, "revenue": { "value": 0 } }, { "key": 6000, "doc_count": 0, "revenue": { "value": 0 } }, { "key": 8000, "doc_count": 1, "revenue": { "value": 8000 } } ] } } }