# Not able to search with date range filter

**URL:** <https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088>\
**Category:** Elasticsearch\
**Created:** [October 16, 2023, 9:55am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088 "2023-10-16T09:55:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 9:55am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/1 "2023-10-16T09:55:15Z")

</div>

I had integrated Elasticsearch in my ruby on rails project with chewy gem.  
All the searches are working fine except date range filter I tried every approach but not getting any error or response is always blank array that means query is correct and it's not even working for exact search.

---

<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:** [October 16, 2023, 10:29am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/2 "2023-10-16T10:29:53Z")

</div>

What is the mapping for the field you are date range filtering on? What does your date range filter/clause look like? Which version of Elasticsearch are you using?

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:37am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/3 "2023-10-16T10:37:45Z")

</div>

Elasticsearch gem version:- 7.2.0

Indexing of created at field in users index is:-  
field :created\_at, type: 'date', format: "yyyy-MM-dd HH:mm:ss||strict\_date\_optional\_time ||epoch\_millis"

---

<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:** [October 16, 2023, 10:44am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/4 "2023-10-16T10:44:17Z")

</div>

Please get the mapping from the [get field mapping API](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/indices-get-field-mapping.html) so we know what is actually in Elasticsearch.

What does the query look like?

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:46am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/5 "2023-10-16T10:46:33Z")

</div>

I'm using chewy gem so do I need to run raw query also ?

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:49am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/6 "2023-10-16T10:49:30Z")

</div>

I tried curl request and through chewy gem both are not responding.

1. curl -XPOST "[http://3.143.106.232:9202/users/\_search](http://3.143.106.232:9202/users/_search)" -H 'Content-Type: application/json' -d'{"query": {"bool": {"term": {"created\_at": "2023-01-02T06:10:15.000Z"}}}}'

2. curl -XPOST "[http://3.143.106.232:9202/users/\_search](http://3.143.106.232:9202/users/_search)" -H 'Content-Type: application/json' -d'{"query": {"range": {"created\_at": {"gte": "2021-09-16T14:06:02", "lt": "2023-09-16T14:06:02"}}}}'

3. with chewy gem:-  
from\_date = '2023-09-11T00:00:00Z' # ISO 8601 format with time and timezone  
to\_date = '2023-10-11T23:59:59Z' # ISO 8601 format with time and timezone  
def search\_with\_date\_range(from\_date, to\_date)

# Use Chewy's DSL to construct the search request

```
query = { bool: { filter: [{ range: { 'created_at': { gte: from_date, lte: to_date } } }] } }

```

# Execute the search

UsersIndex.query(query)  
end

---

<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:** [October 16, 2023, 10:49am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/7 "2023-10-16T10:49:44Z")

</div>

Run a query directly against Elasticsearch. Use curl or some other tool, e,g Kibana dev tools console.

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:51am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/8 "2023-10-16T10:51:20Z")

</div>

I just share two curl commands with you is there any issue in those ?

---

<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:** [October 16, 2023, 10:52am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/9 "2023-10-16T10:52:11Z")

</div>

What is the mapping of the field?

> [@bhumika](#):
>
> both are not responding.

Are you getting no response at all or an empty response?

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:52am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/10 "2023-10-16T10:52:58Z")

</div>

I'm getting empty response.

---

<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:** [October 16, 2023, 10:53am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/11 "2023-10-16T10:53:36Z")

</div>

Please show me the mapping of the field as well as a document in the index that you expect to find.

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:54am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/12 "2023-10-16T10:54:32Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> document in the index that you expect to find.

{"\_index":"users","\_id":"1139","\_score":1.0,"\_source":{"[first\_name":"bchjdshs","last\_name":"vrerh","state":"pending","phone\_number":"","username":"unntvvvvv","email":"mukul.jaiswa@dsdf.com](mailto:first_name%22:%22bchjdshs%22,%22last_name%22:%22vrerh%22,%22state%22:%22pending%22,%22phone_number%22:%22%22,%22username%22:%22unntvvvvv%22,%22email%22:%22mukul.jaiswa@dsdf.com)","uid":"ID64A5F99228","role":"member","platform":null,"referral\_id":null,"id":1139,"country":null,"level":0,"users\_count":0,"created\_at":"2023-01-02T06:10:15.000Z"}}

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:55am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/13 "2023-10-16T10:55:36Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> Please show me the mapping of the field

Attaching my index file:-

# frozen\_string\_literal: true

class UsersIndex \< Chewy::Index  
settings analysis: {  
analyzer: {  
email: {  
tokenizer: 'keyword',  
type: 'keyword'  
}  
}  
}

index\_scope User  
root date\_detection: true do  
field :first\_name  
field :last\_name  
field :state  
field :phone\_number  
field :username  
field :email, analyzer: 'email'  
field :uid, type: 'keyword'  
field :role  
field :platform  
field :referral\_id, type: 'integer'  
field :id, type: 'integer'  
field :country  
field :level, type: 'integer'  
field :users\_count, type: 'integer'  
field :created\_at, type: 'date', format: "yyyy-MM-dd HH:mm:ss||strict\_date\_optional\_time ||epoch\_millis"  
end  
end

---

<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:** [October 16, 2023, 10:56am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/14 "2023-10-16T10:56:52Z")

</div>

I want to see what the mapping in Elasticsearch is, not what your code thinks it has set it to.

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 10:59am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/15 "2023-10-16T10:59:20Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> I want to see what the mapping in Elasticsearch i

here it is:-  
{"users":{"mappings":{"dynamic":"false","properties":{"country":{"type":"text","analyzer":"keyword"},"email":{"type":"text","analyzer":"keyword"},"first\_name":{"type":"text"},"id":{"type":"integer"},"last\_name":{"type":"text"},"level":{"type":"integer"},"phone\_number":{"type":"text"},"platform":{"type":"text"},"referral\_id":{"type":"integer"},"role":{"type":"text"},"state":{"type":"text"},"uid":{"type":"keyword"},"username":{"type":"text"},"users\_count":{"type":"integer"}}}}}

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 11:00am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/16 "2023-10-16T11:00:53Z")

</div>

we don't have created\_at here in the mapping but we have this field in the response of match\_all query.

---

<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:** [October 16, 2023, 11:03am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/17 "2023-10-16T11:03:20Z")

</div>

What is the full output of `curl -XGET "http://3.143.106.232:9202/users/_mapping"` ?

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 11:06am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/18 "2023-10-16T11:06:36Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> What is the full output of `curl -XGET "http://3.143.106.232:9202/users/_mapping"` ?

this one:-  
{"users":{"mappings":{"dynamic":"false","properties":{"country":{"type":"text","analyzer":"keyword"},"email":{"type":"text","analyzer":"keyword"},"first\_name":{"type":"text"},"id":{"type":"integer"},"last\_name":{"type":"text"},"level":{"type":"integer"},"phone\_number":{"type":"text"},"platform":{"type":"text"},"referral\_id":{"type":"integer"},"role":{"type":"text"},"state":{"type":"text"},"uid":{"type":"keyword"},"username":{"type":"text"},"users\_count":{"type":"integer"}}}}}

---

<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:** [October 16, 2023, 11:10am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/19 "2023-10-16T11:10:16Z")

</div>

Well, if the `created_at` field is not in the mappings, it has not been indexed and can therefore not be searched. That would explain why you are not getting any hits. I think you need to recreate the index and ensure all fields are correctly mapped.

---

<div class="post-metadata">

**Author:** ![bhumika](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhumika](https://discuss.elastic.co/u/bhumika)\
**Post date:** [October 16, 2023, 11:10am UTC](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088/20 "2023-10-16T11:10:43Z")

</div>

this is the manual index which I had created before and then I switched to a different gem and removed this but the index is not getting changed even after indexing data after deleting the existing index.

[Next page](https://discuss.elastic.co/t/not-able-to-search-with-date-range-filter/345088.md?page=2)
