# Date Histogram - group by hour interval across multiple days

**URL:** https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850
**Category:** Elasticsearch
**Created:** [March 24, 2017, 6:46am UTC](https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850 "2017-03-24T06:46:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![prashant.bajpai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prashant.bajpai/32/16844_2.png) [@prashant.bajpai](https://discuss.elastic.co/u/prashant.bajpai)
#### Post date: [March 24, 2017, 6:46am UTC](https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850/1 "2017-03-24T06:46:49Z")

</div>

Today I had a task where I have to aggregate the data bucketed by 1 hour interval. So I used Date\_Histogram aggregation in elastic search. Below is the query:

```
    GET test-2017.02.01/_search
    {
      "size" : 0,
    
      "aggs": {
        "range_aggs": {
          "date_histogram": {
            "field": "@timestamp",
            "interval": "hour",
            "format": "yyyy-MM-dd HH:mm"
          }
        }
      }
      
    }

```

I got the below result:

```
        "aggregations": {
        "range_aggs": {
          "buckets": [
            {
              "key_as_string": "2017-02-01 12:00",
              "key": 1485950400000,
              "doc_count": 4027
            },
            {
              "key_as_string": "2017-02-01 13:00",
              "key": 1485954000000,
              "doc_count": 0
            }
          ]
        }
      }

```

Every is good till now as I have run this query for one day, but when I run the query for multiple days in that case, I am getting the keys per day.  
My question is - How can I get the data for the hour intervals(ex- 9am to 10am, 10am to 11am, ...etc) across all the days ?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 24, 2017, 7:16am UTC](https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850/2 "2017-03-24T07:16:24Z")

</div>

Add this field in your model, like hour\_of\_the\_day.  
Then it will be easy to create a terms agg on it

---

<div class="post-metadata">

### Author: ![prashant.bajpai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prashant.bajpai/32/16844_2.png) [@prashant.bajpai](https://discuss.elastic.co/u/prashant.bajpai)
#### Post date: [March 24, 2017, 8:08am UTC](https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850/3 "2017-03-24T08:08:58Z")

</div>

@dadoonet Thank for your reply, Is this the only way ? , If I don't want to make any change in elastic db, in that case can we have any solution ?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 24, 2017, 8:24am UTC](https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850/4 "2017-03-24T08:24:15Z")

</div>

Terms agg with script [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script)

But that will be probably slow.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 21, 2017, 8:24am UTC](https://discuss.elastic.co/t/date-histogram-group-by-hour-interval-across-multiple-days/79850/5 "2017-04-21T08:24:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
