# How to best do a time of day type search?

**URL:** <https://discuss.elastic.co/t/how-to-best-do-a-time-of-day-type-search/6949>\
**Category:** Elasticsearch\
**Created:** [March 8, 2012, 3:58pm UTC](https://discuss.elastic.co/t/how-to-best-do-a-time-of-day-type-search/6949 "2012-03-08T15:58:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)\
**Post date:** [March 8, 2012, 3:58pm UTC](https://discuss.elastic.co/t/how-to-best-do-a-time-of-day-type-search/6949/1 "2012-03-08T15:58:07Z")

</div>

I have documents with a classic (millisecond) timestamp. I need to find  
documents where the timestamp is a certain hour of the day (regardless of  
the actual date). I know that one way to implement this would be to  
decompose the timestamp at indexing time (e.g. into individual year, month,  
day, hour, minute fields) and to apply filters on those fields as needed. I  
was wondering whether there is another, more elegant way at query time to  
achieve the same.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 8, 2012, 10:32pm UTC](https://discuss.elastic.co/t/how-to-best-do-a-time-of-day-type-search/6949/2 "2012-03-08T22:32:03Z")

</div>

You can use a script filter use doc['timestamp'].date.hourOfDay to get for example the hour of day (see more here [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html)).

Note though, that script filter simply runs the script against the matching doc, and check if it matches or not. The data is not "indexed" compared to indexing an additional field that would include the hour of day for example and then doing checks on it.

On Thursday, March 8, 2012 at 5:58 PM, Jan Fiedler wrote:

> I have documents with a classic (millisecond) timestamp. I need to find documents where the timestamp is a certain hour of the day (regardless of the actual date). I know that one way to implement this would be to decompose the timestamp at indexing time (e.g. into individual year, month, day, hour, minute fields) and to apply filters on those fields as needed. I was wondering whether there is another, more elegant way at query time to achieve the same.

---

<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 6, 2017, 3:36am UTC](https://discuss.elastic.co/t/how-to-best-do-a-time-of-day-type-search/6949/3 "2017-07-06T03:36:36Z")

</div>


