# Elastic search range query not working

**URL:** <https://discuss.elastic.co/t/elastic-search-range-query-not-working/199831>\
**Category:** Elasticsearch\
**Created:** [September 17, 2019, 1:35pm UTC](https://discuss.elastic.co/t/elastic-search-range-query-not-working/199831 "2019-09-17T13:35:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prasanth\_MP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prasanth_mp/32/52805_2.png) [@Prasanth\_MP](https://discuss.elastic.co/u/Prasanth_MP)\
**Post date:** [September 17, 2019, 1:35pm UTC](https://discuss.elastic.co/t/elastic-search-range-query-not-working/199831/1 "2019-09-17T13:35:45Z")

</div>

Hi Currently I am having an elastic search index and i am trying to perform a date range query on the documents within that index using java high level rest client library . However it doesn't work as expected. so the following is my index .I have to perform a range query on the field @timestamp.

```auto
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 229081,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mep-reports-2019.09.11",
        "_type" : "doc",
        "_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c-353899837500",
        "_score" : 1.0,
        "_source" : {
          "account_id" : "270d13e6-2f4f-4d51-99d5-92ffba5f0cb6",
          "inventory" : "SMS",
          "flight_name" : "test flight 001",
          "status" : "ENROUTE",
          "msg_text" : "Test !!!!!!!!!!!!!!1 elastic search:is.gd/YfzotY",
          "flight_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c",
          "submission_ts" : "1568197286",
          "recipient" : "353899837500",
          "o_error" : null,
          "nof_segments" : "-1",
          "campaign_id" : "0fae8662-bee9-46ac-9b3e-062f4ba55966",
          "campaign_name" : "Index search petri11",
          "@version" : "1",
          "sender" : "800111",
          "delivery_ts" : "0",
          "@timestamp" : "2019-09-11T10:21:26.000Z"
        }
      },
      {
        "_index" : "mep-reports-2019.09.11",
        "_type" : "doc",
        "_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c-353899837503",
        "_score" : 1.0,
        "_source" : {
          "account_id" : "270d13e6-2f4f-4d51-99d5-92ffba5f0cb6",
          "inventory" : "SMS",
          "flight_name" : "test flight 001",
          "status" : "ENROUTE",
          "msg_text" : "Test !!!!!!!!!!!!!!1 elastic searchh",
          "flight_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c",
          "submission_ts" : "1568197286",
          "recipient" : "353899837503",
          "o_error" : null,
          "nof_segments" : "-1",
          "campaign_id" : "0fae8662-bee9-46ac-9b3e-062f4ba55966",
          "campaign_name" : "Index search petri11",
          "@version" : "1",
          "sender" : "800111",
          "delivery_ts" : "0",
          "@timestamp" : "2019-09-11T10:21:26.000Z"
        }
      }
}

```

the followign is my java code that tries to do a range query on the @timestamp and it doesnt work . I am only pasting a part of the range query snippet below.

```auto
SearchRequestBuilder range(ZonedDateTime startDate, ZonedDateTime endDate) {

    System.out.println(startDate); // 2019-08-31T23:00Z
    System.out.println(endDate); // 2019-09-17T13:25:18.154Z

    RangeQueryBuilder startRangeQueryBuilder = QueryBuilders.rangeQuery("@timestamp").gte(startDate);
    startRangeQueryBuilder.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    boolQueryBuilder.should(startRangeQueryBuilder);

    RangeQueryBuilder endRangeQueryBuilder = QueryBuilders.rangeQuery("@timestamp").lte(endDate);
    boolQueryBuilder.should(endRangeQueryBuilder);
    endRangeQueryBuilder.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    return this;
}

```

any idea how can i fix my range query to in order to work it correctly

thank you so much

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 17, 2019, 3:35pm UTC](https://discuss.elastic.co/t/elastic-search-range-query-not-working/199831/2 "2019-09-17T15:35:41Z")

</div>

hey,

please take your time to reread how `should` clauses work within a bool query. In this particular case a should results in an OR, which means you are searching for documents older than X or younger Y.

First you should combine this to one range query

```auto
QueryBuilders.rangeQuery("@timestamp").gte(startDate).lte(endDate);

```

Second you should put that single rangeQuery into the `filter` part of a boolean query.

--Alex

---

<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:** [October 15, 2019, 3:45pm UTC](https://discuss.elastic.co/t/elastic-search-range-query-not-working/199831/3 "2019-10-15T15:45:55Z")

</div>

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