How to use Lucene expressions within a query to filter a search by hour


#1

I have a bunch of indexed documents, each of which have a startTime field which is a date. I want to query for all documents after 9 PM, regardless of what day they are on. In my production setup, I have groovy scripts disabled for security reasons, so I'm looking to get through this using Lucene expressions. This was my original attempt, which used groovy scripts...

{
  "size": 50,
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "doc['startTime'].getHourOfDay() > 21"
        }
      }
    }
  }
}

How do I craft a similar query using Lucene expressions? I can see that we should be able to access getHourOfDay() on documents, but I can't find any solid examples of filtering by hour without using groovy scripts.


(Nik Everett) #2

You are much better off doing this as a bool of range queries, I think. Script filters will have to visit each document and look up the startTime from doc values. A range query can use the index so it only has to visit the queries that have a chance of matching.

Look out for time zones, btw.


(system) #3