# ElasticSearch Query Range with timezone is not working

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877>\
**Category:** Elasticsearch\
**Created:** [October 3, 2020, 4:16pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877 "2020-10-03T16:16:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)\
**Post date:** [October 3, 2020, 4:16pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/1 "2020-10-03T16:16:56Z")

</div>

Hi, I am currently facing a weird issue, when I am include timezone, the elastic search is returning zero hits:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

Whilst if i remove the timezone, it works:

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "business_logstash_wbsp",
        "_type" : "_doc",
        "_id" : "OEg573QBzOy_U32-2Hq1",
        "_score" : 1.0,
        "_source" : {
          "wbsp" : "smit 12345 1005000000000002"
        }
      },
      {
        "_index" : "business_logstash_wbsp",
        "_type" : "_doc",
        "_id" : "E0g573QBzOy_U32-GHor",
        "_score" : 1.0,
        "_source" : {
          "wbsp" : "smit 12345 1005000000000002"
        }
      },
      {
        "_index" : "business_logstash_wbsp",
        "_type" : "_doc",
        "_id" : "KUg573QBzOy_U32-iHoa",
        "_score" : 1.0,
        "_source" : {
          "wbsp" : "smit 12345 1005000000000002"
        }
      }
    ]
  }
}

```

Here is the sample query:

```auto
GET /business_logstash_wbsp/_search
{
    "_source": "wbsp",
   "query":{
      "range":{
         "@timestamp":{
            "gte":"2020-10-01T00:01:00",
            "lt":"2020-10-03T16:00:00"
         }
      }
   }
}

```

Ref: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html)

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [October 3, 2020, 7:04pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/2 "2020-10-03T19:04:19Z")

</div>

Can you add a sample of the data that you think should be returned true along with the mapping?

---

<div class="post-metadata">

**Author:** ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)\
**Post date:** [October 4, 2020, 4:45am UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/3 "2020-10-04T04:45:03Z")

</div>

Noted, I have made the changes to the question. Also note, the data returnined is the expected result. In the above sample, I have modified the date based to UTC (manually).

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [October 4, 2020, 12:26pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/4 "2020-10-04T12:26:00Z")

</div>

> [@mastersmit](#):
>
> `"wbsp" : "smit 12345 1005000000000002"`

What type of field is `wbsp` and is this the data for it? (string)?

---

<div class="post-metadata">

**Author:** ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)\
**Post date:** [October 5, 2020, 4:24pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/5 "2020-10-05T16:24:19Z")

</div>

Yes, it is of type "string"

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [October 5, 2020, 5:57pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/6 "2020-10-05T17:57:53Z")

</div>

I believe timezone doesn't work on strings. Can't find the reference yet. Will update the post when I do. This would be due to it requiring to do math to calculate the date and you can't do that with a string.

This would mean you would need to convert to date or int if you wish to perform range queries using timezones.

---

<div class="post-metadata">

**Author:** ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)\
**Post date:** [October 6, 2020, 1:03pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/7 "2020-10-06T13:03:30Z")

</div>

Noted, However I am filitering on value of metadata @timestamp right, may i know how is "wbsp" field affecting the result.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [October 6, 2020, 1:06pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/8 "2020-10-06T13:06:17Z")

</div>

> [@mastersmit](#):
>
> ```auto
> "@timestamp":{
> "gte":"2020-10-01T00:01:00",
> "lt":"2020-10-03T16:00:00"
> }
> 
> ```

I misread it. Apologies.

Can you post the query you are using to try the timezone?

---

<div class="post-metadata">

**Author:** ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)\
**Post date:** [October 6, 2020, 1:32pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/9 "2020-10-06T13:32:50Z")

</div>

No worries.

Ref: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#range-query-time-zone](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#range-query-time-zone)

```auto
GET /business_logstash_wbsp/_search
{
   "_source": "wbsp",
   "query":{
        "range": {
          "@timestamp": {
            "time_zone": "+08:00",        
            "gte": "2020-10-01T00:01:00", 
            "lte": "2020-10-03T16:00:00"                  
          }
        }
      }
    }

```

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [October 6, 2020, 2:07pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/10 "2020-10-06T14:07:46Z")

</div>

Everything looks correct. I am assuming you verified that after adjusting the timezone there are records that would be returned within that range?

---

<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:** [November 3, 2020, 2:07pm UTC](https://discuss.elastic.co/t/elasticsearch-query-range-with-timezone-is-not-working/250877/11 "2020-11-03T14:07:54Z")

</div>

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