How to best do a time of day type search?


(Jan Fiedler) #1

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.


(Shay Banon) #2

You can use a script filter use doc['timestamp'].date.hourOfDay to get for example the hour of day (see more here 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.


(system) #3