ES date_histogram 聚合

如下

GET cars/index/_search
{
  "size":0,
  "aggs": {
    "sales": {
      "date_histogram": {//按照日期时间聚合分析数据
        "field": "sold",//分析的字段
        "interval": "month",//按照月份间隔
        "format": "yyyy-MM-dd",//日期格式
        "min_doc_count": 0,// 没有数据的月份返回0
        "extended_bounds":{//强制返回的日期区间,是连续的
          "min":"2014-01-01",
          "max":"2018-12-31"
        }
      }
    }
  }
}

结果如下,拿到数据后方便进行图表分析,这样区间内连续的数据都可以看得很清晰

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "sales": {
      "buckets": [
        {
          "key_as_string": "2014-01-01",
          "key": 1388534400000,
          "doc_count": 1
        },
        {
          "key_as_string": "2014-02-01",
          "key": 1391212800000,
          "doc_count": 1
        },
        {
          "key_as_string": "2014-03-01",
          "key": 1393632000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2014-04-01",
          "key": 1396310400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2014-05-01",
          "key": 1398902400000,
          "doc_count": 1
        },
        {
          "key_as_string": "2014-06-01",
          "key": 1401580800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2014-07-01",
          "key": 1404172800000,
          "doc_count": 1
        },
        {
          "key_as_string": "2014-08-01",
          "key": 1406851200000,
          "doc_count": 1
        },
        {
          "key_as_string": "2014-09-01",
          "key": 1409529600000,
          "doc_count": 0
        },
        {
          "key_as_string": "2014-10-01",
          "key": 1412121600000,
          "doc_count": 1
        },
        {
          "key_as_string": "2014-11-01",
          "key": 1414800000000,
          "doc_count": 2
        },
        {
          "key_as_string": "2014-12-01",
          "key": 1417392000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-01-01",
          "key": 1420070400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-02-01",
          "key": 1422748800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-03-01",
          "key": 1425168000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-04-01",
          "key": 1427846400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-05-01",
          "key": 1430438400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-06-01",
          "key": 1433116800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-07-01",
          "key": 1435708800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-08-01",
          "key": 1438387200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-09-01",
          "key": 1441065600000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-10-01",
          "key": 1443657600000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-11-01",
          "key": 1446336000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2015-12-01",
          "key": 1448928000000,
          "doc_count": 0
        },
        {
          "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": 0
        },
        {
          "key_as_string": "2016-06-01",
          "key": 1464739200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-07-01",
          "key": 1467331200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-08-01",
          "key": 1470009600000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-09-01",
          "key": 1472688000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-10-01",
          "key": 1475280000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-11-01",
          "key": 1477958400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2016-12-01",
          "key": 1480550400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-01-01",
          "key": 1483228800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-02-01",
          "key": 1485907200000,
          "doc_count": 0
        },
        {
          "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
        },
        {
          "key_as_string": "2018-01-01",
          "key": 1514764800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-02-01",
          "key": 1517443200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-03-01",
          "key": 1519862400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-04-01",
          "key": 1522540800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-05-01",
          "key": 1525132800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-06-01",
          "key": 1527811200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-07-01",
          "key": 1530403200000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-08-01",
          "key": 1533081600000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-09-01",
          "key": 1535760000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-10-01",
          "key": 1538352000000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-11-01",
          "key": 1541030400000,
          "doc_count": 0
        },
        {
          "key_as_string": "2018-12-01",
          "key": 1543622400000,
          "doc_count": 0
        }
      ]
    }
  }
}

 

posted @ 2019-09-27 10:27  kakaok  阅读(1522)  评论(0编辑  收藏  举报