# Range query not giving right result for lte

**URL:** <https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [June 9, 2021, 3:20pm UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460 "2021-06-09T15:20:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumyas](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@soumyas](https://discuss.elastic.co/u/soumyas)\
**Post date:** [June 9, 2021, 3:20pm UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460/1 "2021-06-09T15:20:43Z")

</div>

The result includes 23/03 in the result and miss to show 17/04. Where in data is present in the system. Any help is appreciated.

```
    "range" : {
      "timestamp" : {
        "from" : "2021-03-23",
        "to" : "2021-04-17",
        "include_lower":true,
        "include_upper":true,
        "boost":1.0
      }
    }
```

---

<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:** [June 9, 2021, 4:02pm UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460/2 "2021-06-09T16:02:58Z")

</div>

I think that `2021-04-17` is actually parsed as `2021-04-17T00:00:00`.  
When you include it, it does not mean that any second after `00:00:00` will be matching.

You should either do:

```auto
    "range" : {
      "timestamp" : {
        "from" : "2021-03-23",
        "to" : "2021-04-17T23:59:59.999",
        "include_lower":true,
        "include_upper":true,
        "boost":1.0
      }
    }

```

But I definitely prefer:

```auto
    "range" : {
      "timestamp" : {
        "from" : "2021-03-23",
        "to" : "2021-04-18"
      }
    }

```

---

<div class="post-metadata">

**Author:** ![soumyas](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@soumyas](https://discuss.elastic.co/u/soumyas)\
**Post date:** [June 10, 2021, 8:17am UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460/3 "2021-06-10T08:17:25Z")

</div>

Can you suggest how do we implement in Java using QueryBuilders.rangeQuery.

---

<div class="post-metadata">

**Author:** ![soumyas](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@soumyas](https://discuss.elastic.co/u/soumyas)\
**Post date:** [June 10, 2021, 8:18am UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460/4 "2021-06-10T08:18:13Z")

</div>

Thanks @dadoonet🙂  
I will try with the suggested change.

---

<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:** [June 10, 2021, 8:45am UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460/5 "2021-06-10T08:45:27Z")

</div>

Here is one:

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/937685b0c9639cb336134e59aea3a710d7654f1e/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientTest.java#L394-L408>

---

<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 8, 2021, 8:45am UTC](https://discuss.elastic.co/t/range-query-not-giving-right-result-for-lte/275460/6 "2021-07-08T08:45:45Z")

</div>

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