# How to aggregate data by hour of day regardless of day

**URL:** https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127
**Category:** Elasticsearch
**Created:** [April 2, 2016, 9:40am UTC](https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127 "2016-04-02T09:40:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![adityaishu24](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@adityaishu24](https://discuss.elastic.co/u/adityaishu24)
#### Post date: [April 2, 2016, 9:40am UTC](https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127/1 "2016-04-02T09:40:46Z")

</div>

My query is that I am not able to aggregate my data on date field as per hour of day , I want the aggregation for every hour regardless of day .

```
{
"size": 0,
"aggs": {
"hour": {
"date_histogram": {
"field": "date",
"interval": "hour",
"min_doc_count": 0,
"format": "hh"
}

```

this gives for each hour for each day which is wrong can anyone help??

---

<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: [April 2, 2016, 9:47am UTC](https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127/2 "2016-04-02T09:47:35Z")

</div>

Please format your code. It's ugly.

That's how it works.

I would index the hour of the day as a field within the doc and aggregate on this.

You could may be [Scripted fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html) to extract the hour of the day but I think it will be less efficient.

Example: [https://www.elastic.co/blog/kibana-4-beta-3-now-more-filtery](https://www.elastic.co/blog/kibana-4-beta-3-now-more-filtery)

---

<div class="post-metadata">

### Author: ![adityaishu24](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@adityaishu24](https://discuss.elastic.co/u/adityaishu24)
#### Post date: [April 2, 2016, 10:00am UTC](https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127/3 "2016-04-02T10:00:36Z")

</div>

yes rather than scripts its better to make hour of day as field it will work but can't we do it directly on the date field , because for hour of day I have to create another property in mappings file and I am looking for doing it on the same date field , is there a way we can

---

<div class="post-metadata">

### Author: ![adityaishu24](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@adityaishu24](https://discuss.elastic.co/u/adityaishu24)
#### Post date: [April 2, 2016, 10:34am UTC](https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127/4 "2016-04-02T10:34:35Z")

</div>

can you help me to write a file script of the inline script for hour of day as I am completely new to elasticsearch

---

<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 5, 2017, 11:03pm UTC](https://discuss.elastic.co/t/how-to-aggregate-data-by-hour-of-day-regardless-of-day/46127/5 "2017-07-05T23:03:01Z")

</div>


