# Elasticsearch date range query for range on a date-time field

**URL:** https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759
**Category:** Elasticsearch
**Created:** [March 26, 2022, 2:18pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759 "2022-03-26T14:18:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Shaiwal\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaiwal_sharma/32/97508_2.png) [@Shaiwal\_Sharma](https://discuss.elastic.co/u/Shaiwal_Sharma)
#### Post date: [March 26, 2022, 2:18pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759/1 "2022-03-26T14:18:27Z")

</div>

I have to get records (training) wherein I have a start\_date, and I have to get all records that have not yet ended. So I don't have an end\_date field, but just the start date, so end\_date is start\_date + 60 days. So what I need comprises all results that have "start\_date" less than equal to "now" and "start\_date + 60 days" greater than equal to "now" i.e. "now" is between start\_date and "start\_date + 60 days".

So here's what I am trying. But I know "on\_demand\_start\_date+60d" is incorrect. Can anyone help, how can this be done?

"must": [  
{  
"range": {  
"on\_demand\_start\_date+60d": {  
"gte": "now"  
}  
}  
},  
{  
"range": {  
"on\_demand\_start\_date": {  
"lte": "now"  
}  
}  
}  
]

Below is a sample document: `{ "_index": "cdc_sse_content", "_type": "doc", "_id": "cdc_sse_publish_1140", "_score": null, "_source": { "country": "us", "event_source_time_zone": "", "source": "CVENT", "iSAPIRecord": true, "index_type": "content", "content_title": "***Test - API March 04 - 1", "event_technology": ["Cloud & Computing", "Collaboration", "Services (CX)"], "language_code": "en", "application_name": "event-selector", "event_type": "Hybrid event", "event_region": ["North America"], "event_time_zone": "UTC", "cdc_page_url": "https://www.google.com?EID=<95929>", "event_end_date": "2022-02-05T01:00", "event_filters": "", "created_by": "shaiwsha", "live_stream_end_date": null, "marketing_owner": "tparida", "event_on-demand_details": "", "search_filter_lables": ["On-demand"], "kafka": { "consumer_group": "sseadmin", "partition": 0, "offset": 181002, "topic": "SSEAdminContentPublished", "key": null }, "updated_by": "shaiwsha", "publish_date": "2022-02-15", "event_start_date": "2022-02-04T17:00", "status": "active", "eid": "95929", "es_index_fail": false, "cdc_id": "event-selector_1140", "event_image_url": "https://www.cisco.com/c/dam/assets/events/i/event-selector/2021/rsa_global_events_page_br_dark_600x400.png", "description": "Test 1 - This is a sample Hybrid event, that should the event type,Hybrid with City and country displayed. This is a sample In-Person that should disp", "selector_id": "cdc_event-selector_publish_1140", "event_livestream_url": "", "locale": "en_us", "event_registration_url": "https://sandbox-www.cvent.com/d/p7qgmk/4W?&EID=95929", "event_live_stream_details": "", "event_city": "Santa Clara", "event_registration_and_location_details": "", "@version": "1", "event_details": "", "start_date": "2022-02-07T19:37", "registration_start_date": "2022-02-18T09:00", "event_industry": ["Manufacturing"], "event_language": ["Cantonese", "Dutch", "English"], "event_state": "California", "live_stream_start_date": null, "on_demand_url": "https://www.google.com", "on_demand": ["On-demand"], "@timestamp": "2022-03-15T07:28:08.821Z", "registration_end_date": "2022-02-20T09:00", "recent_search_published_date": "2022-03-10T14:17:30.320Z", "event_country": "USA", "featured_event": ["Featured event"], "created_date": "2022-03-10T10:08:05.801Z", "last_update_ts": "2022/3/10 14:19:5", "updated_date": "2022-03-10T14:19:05.693Z" }, "sort": [1643994000000] }`

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [March 27, 2022, 11:42pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759/2 "2022-03-27T23:42:11Z")

</div>

I think this is what you're looking for see [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math)

This says less than or equal to now plus 60 days and greater than equal to now

```auto
"range": {
"on_demand_start_date": {
"lte" : "now+60d/d",
"gte": "now"
}

```

---

<div class="post-metadata">

### Author: ![Shaiwal\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaiwal_sharma/32/97508_2.png) [@Shaiwal\_Sharma](https://discuss.elastic.co/u/Shaiwal_Sharma)
#### Post date: [March 28, 2022, 4:34am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759/3 "2022-03-28T04:34:36Z")

</div>

Hi Stephen,  
thanks for the reply, but what I am looking for is basically calculate on on\_deman\_start\_date, so like on\_demand\_start\_date is "lte" now && "on\_demand\_start\_date + 60days" is "gte" now.  
I am not sure if what you suggested and what is expected both will be the same.

Just to explain, I don't have an end date for on\_demand training, but the way it is expected is on\_demand\_end\_date = on\_demand\_start\_date + 60 days... so basically I need to restrict results based on if training has ended.  
Thanks.

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [March 28, 2022, 3:03pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759/4 "2022-03-28T15:03:37Z")

</div>

Ahh I think I see...

You could create a runtime field that represents

`on_demand_start_date_plus_60d`

and then use that in your query.

Or you could create that field when you ingest the data using it. Ingest pipeline and then use it in your query

---

<div class="post-metadata">

### Author: ![Shaiwal\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaiwal_sharma/32/97508_2.png) [@Shaiwal\_Sharma](https://discuss.elastic.co/u/Shaiwal_Sharma)
#### Post date: [March 29, 2022, 8:29am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759/5 "2022-03-29T08:29:28Z")

</div>

> [@stephenb](#):
>
> on\_demand\_start\_date\_plus\_60d

So when I run the query: GET /  
I get:  
"version": {  
"number": "5.6.16",  
"build\_hash": "3a740d1",  
"build\_date": "2019-03-13T15:33:36.565Z",  
"build\_snapshot": false,  
"lucene\_version": "6.6.1"  
}  
does runtime support this version? Also, will it impact the index anyway, because I don't want to touch the data and keep it as is? Also, do you have any samples of the runtime field? I tried doing so, but got an error.

---

<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: [April 26, 2022, 8:29am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-query-for-range-on-a-date-time-field/300759/6 "2022-04-26T08:29:34Z")

</div>

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