# So much pain on ES, query on date returns wrong results

**URL:** <https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215>\
**Category:** Elasticsearch\
**Created:** [February 16, 2018, 4:03pm UTC](https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215 "2018-02-16T16:03:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derrick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/derrick/32/49944_2.png) [@Derrick](https://discuss.elastic.co/u/Derrick)\
**Post date:** [February 16, 2018, 4:03pm UTC](https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215/1 "2018-02-16T16:03:20Z")

</div>

Hi support,

I am doing range query on date field, but it returns no expected result, any idea?

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "booking.dealerIDUpdate": {
              "gte": "2017-11-29T00:00:00.000-0000",
              "lte": "2017-11-29T23:59:59.000-0500",
              "include_lower": true,
              "include_upper": true,
              "boost": 1,
              "time_zone": "-05:00"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 16, 2018, 4:53pm UTC](https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215/2 "2018-02-16T16:53:57Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

Please edit your post.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![Derrick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/derrick/32/49944_2.png) [@Derrick](https://discuss.elastic.co/u/Derrick)\
**Post date:** [February 16, 2018, 5:32pm UTC](https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215/3 "2018-02-16T17:32:25Z")

</div>

I am using QueryBuilders to create query, I dont' k

```auto
queryBuilder.filter(QueryBuilders.rangeQuery("booking.dealerIDUpdate").from(start).to(end).timeZone("America/New_York"));

```

how to set timezone? with this code, it always returns some data with 2017-11-28T19:30:00

saved date in ES 5.6, it will be returned by above query, reason?

```auto
"booking": {
"dealerIDUpdate": "2017-11-28T19:36:00.000-0500"
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 16, 2018, 6:07pm UTC](https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215/4 "2018-02-16T18:07:38Z")

</div>

I can't tell without a full example.

Anyway, few things to know.  
If you don't define a TZ when indexing a date like `2017-11-28T19:30:00` it is supposed to be a UTC based date. That might explain what you are seeing.

So I'd probably either set the right TZ when indexing and index something like `2017-11-28T19:30:00-0500`. Or I'd not use TZ at all in the range query.

---

<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, 2018, 6:08pm UTC](https://discuss.elastic.co/t/so-much-pain-on-es-query-on-date-returns-wrong-results/120215/5 "2018-03-16T18:08:00Z")

</div>

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