# Range-Query not working l

**URL:** <https://discuss.elastic.co/t/range-query-not-working-l/273496>\
**Category:** Elasticsearch\
**Created:** [May 20, 2021, 8:58am UTC](https://discuss.elastic.co/t/range-query-not-working-l/273496 "2021-05-20T08:58:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rammi22](https://avatars.discourse-cdn.com/v4/letter/r/258eb7/32.png) [@Rammi22](https://discuss.elastic.co/u/Rammi22)\
**Post date:** [May 20, 2021, 8:58am UTC](https://discuss.elastic.co/t/range-query-not-working-l/273496/1 "2021-05-20T08:58:13Z")

</div>

Hello,

I use:

- [Elasticsearch-PHP](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html)
- [ElasticsearchDSL](https://github.com/ongr-io/ElasticsearchDSL)

Configurated search query:

```auto
array(2) {
  ["index"]=>
  string(19) "my_index_*"
  ["body"]=>
  array(4) {
    ["query"]=>
    array(1) {
      ["range"]=>
      array(1) {
        ["data.main.startDateTime"]=>
        array(2) {
          ["lte"]=>
          string(25) "2008-08-23T23:05:10"
          ["gte"]=>
          string(25) "2008-08-23T23:05:08+02:00"
        }
      }
    }
    ["sort"]=>
    array(1) {
      [0]=>
      array(1) {
        ["data.main.startDateTime"]=>
        array(1) {
          ["order"]=>
          string(4) "desc"
        }
      }
    }
    ["from"]=>
    int(0)
    ["size"]=>
    int(100)
  }
}

```

Calling the `search-Request (Elasticsearch\Client->search($params)` with the parameters above returns also records out of the range. Seems the range are ignored....

**Expected** 2 hits  
**Actual** more than 10 hits

As example the query returns this record, which is out of the range

```auto
// the record
{
	"_index": "my_index_200808",
	"_type": "_doc",
	...
	"_version": 1,
	"_seq_no": 34,
	"_primary_term": 1,
	"found": true,
	"_source": {
		...,
		"data": {
			"main": {
				...
				"startDateTime": "2008-08-23T23:25:22+02:00",
				...
			},
			...
		}
	}
}

```

What is wrong with the query configuration? How to use the _range query_?

---

<div class="post-metadata">

**Author:** ![ErSumit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ersumit/32/88708_2.png) [@ErSumit](https://discuss.elastic.co/u/ErSumit)\
**Post date:** [May 20, 2021, 9:29am UTC](https://discuss.elastic.co/t/range-query-not-working-l/273496/2 "2021-05-20T09:29:29Z")

</div>

> [@Rammi22](#):
>
> `startDateTime`

You shall try

```auto
GET /_search
{
"query":
    {
    "bool":
        {
            "must": [
                  "range":
                   {
                       "startDateTime": { "gte": "now-1d", "lte": "now"
                   }
             ]
        }
    }
}

```

This shall give you day between last and until this time, since we have used now-1d to now. Try changing the time range in query.

Cheers!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 20, 2021, 9:32am UTC](https://discuss.elastic.co/t/range-query-not-working-l/273496/3 "2021-05-20T09:32:56Z")

</div>

You have specified timezone in only one of your conditions so you may be searching a window larger than you think.

---

<div class="post-metadata">

**Author:** ![Rammi22](https://avatars.discourse-cdn.com/v4/letter/r/258eb7/32.png) [@Rammi22](https://discuss.elastic.co/u/Rammi22)\
**Post date:** [May 20, 2021, 9:49am UTC](https://discuss.elastic.co/t/range-query-not-working-l/273496/4 "2021-05-20T09:49:42Z")

</div>

@ErSumit @Christian_Dahlqvist thx for response,

the query configuration was wrong, witn the fix it works:

```auto
array(2) {
  ["index"]=>
  string(19) "my_index_*"
  ["body"]=>
  array(4) {
    ["query"]=>
    array(1) {
      ["range"]=>
      array(1) {
        ["data.main.startDateTime"]=>
        array(2) {
          ["lte"]=>
          string(25) "2008-08-23T23:05:10+02:00"
          ["gte"]=>
          string(25) "2008-08-23T23:05:08+02:00"
        }
      }
    }
    ["sort"]=>
    array(1) {
      [0]=>
      array(1) {
        ["data.main.startDateTime"]=>
        array(1) {
          ["order"]=>
          string(4) "desc"
        }
      }
    }
    ["from"]=>
    int(0)
    ["size"]=>
    int(100)
  }
}
Elasticsearch\Client:1212

```

Thanks and bye

---

<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:** [June 17, 2021, 9:49am UTC](https://discuss.elastic.co/t/range-query-not-working-l/273496/5 "2021-06-17T09:49:59Z")

</div>

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