date histogram 定义
bucket,分组操作,histogram,按照某个值指定的interval,划分一个一个的bucket
date histogram,按照我们指定的某个date类型的日期field,以及日期interval,按照一定的日期间隔,去划分bucket
date histogram 使用
date interval = 1m,
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:即使某个日期interval,2017-01-01~2017-01-31中,一条数据都没有,那么这个区间也是要返回的,不然默认是会过滤掉这个区间的.(设置默认最小值)
extended_bounds,min,max:划分bucket的时候,会限定在这个起始日期,和截止日期内
date histogram 搜索测试
GET /tvs/sales/_search { "size" : 0, "aggs": { " group_by_sold_date": { "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" } } } } }
|
date histogram 结果
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { " group_by_sold_date": { "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 } ] } } } |