# How to get hourly based document count

**URL:** <https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043>\
**Category:** Elasticsearch\
**Created:** [June 28, 2019, 12:37pm UTC](https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043 "2019-06-28T12:37:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [June 28, 2019, 12:37pm UTC](https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043/1 "2019-06-28T12:37:00Z")

</div>

I have sample documents like below.  
{  
"timestamp": "2019-06-19T13:39:31Z"  
}

I need to get documents to count by hourly like (00:00 - 03:00, 03:00-06:00 ..)

Thanks, Advance

---

<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:** [June 28, 2019, 1:02pm UTC](https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043/2 "2019-06-28T13:02:43Z")

</div>

The preferred way would be to index the hour of the day in addition to the timestamp. Then it's pretty easy to do the aggregation.

---

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [June 28, 2019, 1:11pm UTC](https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043/3 "2019-06-28T13:11:15Z")

</div>

Thanks, @dadoonet for your reply.  
But I can't create the separate index.  
I was trying to use the following query. but not getting expected result.

```
GET /fluent/_search
{
  "size": 0,
  "aggs": {
    
    "time_of_day":{
      "date_range": {
        "field": "timestamp",
        "format": "strict_hour_minute", 
        "ranges": [
          {
            "from": "01:00",
            "to": "10:00"
          }
        ]
      }
    }
  }
}

```

Output:  
{  
"took": 5981,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 1744686,  
"max\_score": 0,  
"hits":   
},  
"aggregations": {  
"time\_of\_day": {  
"buckets": [  
{  
"key": "01:00-10:00",  
"from": 3600000,  
"from\_as\_string": "01:00",  
"to": 36000000,  
"to\_as\_string": "10:00",  
"doc\_count": 0  
}  
]  
}  
}  
}

---

<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:** [June 28, 2019, 5:26pm UTC](https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043/4 "2019-06-28T17:26:44Z")

</div>

If you don't want to create a new field, then you can try to use [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). Note that depending on the volume you have, 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:** [July 26, 2019, 5:26pm UTC](https://discuss.elastic.co/t/how-to-get-hourly-based-document-count/188043/5 "2019-07-26T17:26:49Z")

</div>

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