Elasticsearch count in groups by date range

大城市里の小女人 提交于 2019-12-08 03:59:49

问题


I have documents like this:

{
body: 'some text',
read_date: '2017-12-22T10:19:40.223000'
}

Is there a way to query count of documents published in last 10 days group by date? For example:

2017-12-22, 150  
2017-12-21, 79  
2017-12-20, 111  
2017-12-19, 27  
2017-12-18, 100  

回答1:


Yes, you can easily achieve that using a date_histogram aggregation, like this:

{
  "query": {
    "range": {
      "read_date": {
        "gte": "now-10d"
      }
    }
  },
  "aggs": {
    "byday": {
      "date_histogram": {
        "field": "read_date",
        "interval": "day"
      }
    }
  }
}


来源:https://stackoverflow.com/questions/47938580/elasticsearch-count-in-groups-by-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!