How to best do a time of day type search?

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.

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).

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.

1 Like