# Time range based on timestamp in DSL query

**URL:** <https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962>\
**Category:** Elasticsearch\
**Created:** [January 12, 2024, 1:41pm UTC](https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962 "2024-01-12T13:41:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeCalve](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@LeCalve](https://discuss.elastic.co/u/LeCalve)\
**Post date:** [January 12, 2024, 1:41pm UTC](https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962/1 "2024-01-12T13:41:05Z")

</div>

Hello, I want to make a filter based on the time of a timestamp.  
I would like to extract all timestamps which have a time \< 8 or time \> 20.  
I don't know how to make the DSL query for that 🙂 {

---

<div class="post-metadata">

**Author:** ![yago82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yago82/32/97755_2.png) [@yago82](https://discuss.elastic.co/u/yago82)\
**Post date:** [January 12, 2024, 2:06pm UTC](https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962/2 "2024-01-12T14:06:45Z")

</div>

> [@LeCalve](#):
>
> Hello, I want to make a filter based on the time of a timestamp.  
> I would like to extract all timestamps which have a time \< 8 or time \> 20.  
> I don't know how to make the DSL query for that 🙂 {

Hi,

To filter documents based on the time of a timestamp, you can use the `range` query in combination with the `date` field's `hour` date math. Here's an example of how you can do this:

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "timestamp": {
              "lt": "now/h-8h"
            }
          }
        },
        {
          "range": {
            "timestamp": {
              "gt": "now/h+20h"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

```

Regards

---

<div class="post-metadata">

**Author:** ![LeCalve](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@LeCalve](https://discuss.elastic.co/u/LeCalve)\
**Post date:** [January 12, 2024, 2:43pm UTC](https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962/3 "2024-01-12T14:43:32Z")

</div>

Sorry but il doesn't work.  
Ii would like to do something like that  
hour(timepart(timestamp)) \< 8 or hour(timepart(timestamp)) \> 20

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [January 12, 2024, 2:57pm UTC](https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962/4 "2024-01-12T14:57:31Z")

</div>

Hi @LeCalve Welcome to the community.

You are going to need to create a field that represents time of day and then filter on it

I just help someone with this. You can read what we did here

> [@Extracting time from @timestamp field using Runtime](https://discuss.elastic.co/t/extracting-time-from-timestamp-field-using-runtime/348468/1):
>
> I understand Scripted Fields have been deprecated since 7.13. The replacement is now Runtime. I have a @timestamp field [image] How can I extract out the time component into a new field @timeofday - such that when I filter by @timeofday, I can filter by time range. I was reading [Lucene Expressions Language | Elasticsearch Guide [5.0] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/modules-scripting-expression.html#_date_field_api) and But still feel abit lost on how complicated an approach I need to do. Its actually a simple mod because I'm relying on data in existing colu…

This is doing it with runtime fields. In order to use this for a DSL query You will need to [add the runtime field directly to the mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html) or you may want to actually use an ingest pipeline and create a field on ingest.

Take a read and come back. Tell us what you think.

---

<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:** [February 9, 2024, 2:57pm UTC](https://discuss.elastic.co/t/time-range-based-on-timestamp-in-dsl-query/350962/5 "2024-02-09T14:57:55Z")

</div>

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