概述
继续跟中华石杉老师学习ES,第37篇
课程地址: https://www.roncoo.com/view/55
官方说明
Date Histogram Aggregation: 戳这里
在 白话Elasticsearch36-深入聚合数据分析之案例实战Histogram Aggregation按区间统计中 我们使用histogram来划分bucket,分组操作,即按照某个值指定的interval,划分一个一个的bucket 。
这里我们俩看下date histogram,即按照我们指定的某个date类型的日期field,以及日期interval,按照一定的日期间隔,去划分bucket
举个例子:
date interval = 1m,1个月
2017-01-01~2017-01-31,就是一个bucket
2017-02-01~2017-02-28,就是一个bucket
…
…
…
然后会去扫描每个数据的date field,判断date落在哪个bucket中,就将其放入那个bucket
2017-01-05,就将其放入2017-01-01~2017-01-31,就是一个bucket
min_doc_count:设置为0 ,就是即使某个日期interval,2017-01-01~2017-01-31中,一条数据都没有,那么这个区间也是要返回的,不然默认是会过滤掉这个区间的 。 如果不希望展示没有销量的月份,min_doc_count:设置为1即可,即至少要有1条数据。
extended_bounds,min,max:划分bucket的时候,会限定在这个起始日期,和截止日期内
实例: 统计每月电视销量,没有销量的月份也要统计
原始数据:
DSL:
GET /tvs/sales/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold_date", "interval": "month", "format": "yyyy-MM-dd", "min_doc_count" : 0, "extended_bounds" : { "min" : "2016-01-01", "max" : "2017-12-31" } } } } }
返回:
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "sales": { "buckets": [ { "key_as_string": "2016-01-01", "key": 1451606400000, "doc_count": 0 }, { "key_as_string": "2016-02-01", "key": 1454284800000, "doc_count": 0 }, { "key_as_string": "2016-03-01", "key": 1456790400000, "doc_count": 0 }, { "key_as_string": "2016-04-01", "key": 1459468800000, "doc_count": 0 }, { "key_as_string": "2016-05-01", "key": 1462060800000, "doc_count": 1 }, { "key_as_string": "2016-06-01", "key": 1464739200000, "doc_count": 0 }, { "key_as_string": "2016-07-01", "key": 1467331200000, "doc_count": 1 }, { "key_as_string": "2016-08-01", "key": 1470009600000, "doc_count": 1 }, { "key_as_string": "2016-09-01", "key": 1472688000000, "doc_count": 0 }, { "key_as_string": "2016-10-01", "key": 1475280000000, "doc_count": 1 }, { "key_as_string": "2016-11-01", "key": 1477958400000, "doc_count": 2 }, { "key_as_string": "2016-12-01", "key": 1480550400000, "doc_count": 0 }, { "key_as_string": "2017-01-01", "key": 1483228800000, "doc_count": 1 }, { "key_as_string": "2017-02-01", "key": 1485907200000, "doc_count": 1 }, { "key_as_string": "2017-03-01", "key": 1488326400000, "doc_count": 0 }, { "key_as_string": "2017-04-01", "key": 1491004800000, "doc_count": 0 }, { "key_as_string": "2017-05-01", "key": 1493596800000, "doc_count": 0 }, { "key_as_string": "2017-06-01", "key": 1496275200000, "doc_count": 0 }, { "key_as_string": "2017-07-01", "key": 1498867200000, "doc_count": 0 }, { "key_as_string": "2017-08-01", "key": 1501545600000, "doc_count": 0 }, { "key_as_string": "2017-09-01", "key": 1504224000000, "doc_count": 0 }, { "key_as_string": "2017-10-01", "key": 1506816000000, "doc_count": 0 }, { "key_as_string": "2017-11-01", "key": 1509494400000, "doc_count": 0 }, { "key_as_string": "2017-12-01", "key": 1512086400000, "doc_count": 0 } ] } } }
"min_doc_count" : 1 的情况