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

**URL:** <https://discuss.elastic.co/t/how-to-use-lucene-expressions-within-a-query-to-filter-a-search-by-hour/44612>\
**Category:** Elasticsearch\
**Created:** [March 16, 2016, 8:10pm UTC](https://discuss.elastic.co/t/how-to-use-lucene-expressions-within-a-query-to-filter-a-search-by-hour/44612 "2016-03-16T20:10:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nosocks](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nosocks](https://discuss.elastic.co/u/nosocks)\
**Post date:** [March 16, 2016, 8:10pm UTC](https://discuss.elastic.co/t/how-to-use-lucene-expressions-within-a-query-to-filter-a-search-by-hour/44612/1 "2016-03-16T20:10:01Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_lucene_expressions_scripts). 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.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 18, 2016, 3:41pm UTC](https://discuss.elastic.co/t/how-to-use-lucene-expressions-within-a-query-to-filter-a-search-by-hour/44612/2 "2016-03-18T15:41:42Z")

</div>

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.

---

<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:07pm UTC](https://discuss.elastic.co/t/how-to-use-lucene-expressions-within-a-query-to-filter-a-search-by-hour/44612/3 "2017-07-05T23:07:08Z")

</div>


