# How to prevent query execution within certain hours of the day?

**URL:** <https://discuss.elastic.co/t/how-to-prevent-query-execution-within-certain-hours-of-the-day/264456>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 16, 2021, 4:23pm UTC](https://discuss.elastic.co/t/how-to-prevent-query-execution-within-certain-hours-of-the-day/264456 "2021-02-16T16:23:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattinVS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattinvs/32/83861_2.png) [@mattinVS](https://discuss.elastic.co/u/mattinVS)\
**Post date:** [February 16, 2021, 4:23pm UTC](https://discuss.elastic.co/t/how-to-prevent-query-execution-within-certain-hours-of-the-day/264456/1 "2021-02-16T16:23:49Z")

</div>

Hi,

I'd like to prevent the execution of a query within certain hours of the day.  
The solution that comes into my mind is to use a painless script filter for this like

```auto
 "query": {
    "bool": {
      "filter": [
        {
          ... more filters for the real query here ...
        },
        {
       "script": {
          "script": {
            "source": """
  int hour = LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()),ZoneId.of('Europe/Zurich')).getHour();
  return params.hour_start <= hour && hour <= params.hour_end;
            """,
            "lang": "painless",
            "params": {
              "hour_start": 2,
              "hour_end": 5
            }
          }
        }
        }
      ]
    }
  },

```

But I fear that this generates unwanted load (due to the creation of objects that are only needed for making sure that the query gets executed in non-office-hours).

Any ideas how to improve this?

Best regards from sunny Black Forest, Germany,  
Mattin

---

<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:** [March 16, 2021, 4:23pm UTC](https://discuss.elastic.co/t/how-to-prevent-query-execution-within-certain-hours-of-the-day/264456/2 "2021-03-16T16:23:55Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
