# Retrieve data having date from 1st, Jan to current date on a date field

**URL:** https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451
**Category:** Elasticsearch
**Created:** [February 14, 2023, 8:21am UTC](https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451 "2023-02-14T08:21:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kajalp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kajalp](https://discuss.elastic.co/u/kajalp)
#### Post date: [February 14, 2023, 8:21am UTC](https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451/1 "2023-02-14T08:21:25Z")

</div>

Hi All,

I have data with a date field having dates from last 3 years.  
What I want?  
I want to get all the records from Jan1st to current day at any given time over a year.

I tried below query:

```auto
GET index_name/_search
{
"query": {
    "bool": {"must": [
{"range":{"promoted":{"lte": "now", "format": "yyyy"}}}
]}
}

```

The ES doc says for a missing date component, it will take default value for day as 01 and MM as 01 hence I tried to give only the yyyy so that the day and month is taken as 01-01-yyyy. But it is not working.

> **[Range query | Elasticsearch Guide \[8.6\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#missing-date-components)**

Please let me know how it can be achieved since I am new to elastic.

---

<div class="post-metadata">

### Author: ![Ayush\_Mathur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ayush_mathur/32/77134_2.png) [@Ayush\_Mathur](https://discuss.elastic.co/u/Ayush_Mathur)
#### Post date: [February 14, 2023, 8:43am UTC](https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451/2 "2023-02-14T08:43:32Z")

</div>

Hello @kajalp , welcome to the community !  
To query for all the documents since the beginning of the year, you can use `range filter` instead of `must query`. Please note, since you are not setting `size:` attribute to your query, it will just return the top 10 results from the payload. Setting `size: 0` will allow you to return 10K top documents from the index.  
Your query should be something similar to:

```auto
GET index_name/_search 
{
	"size" : 0,
	"query" : {
		"bool" : {
			"filter" : {
				"range" : {
					"promoted" : {
						"gte" : "now-1M/M",
						"lte" : "now"
					}
				}
			}
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![kajalp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kajalp](https://discuss.elastic.co/u/kajalp)
#### Post date: [February 14, 2023, 9:32am UTC](https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451/3 "2023-02-14T09:32:28Z")

</div>

Hi Ayush,

Thankyou for response.  
Just one doubt will it give the data from jan1st to today at any given point of time in a year? Because giving "gte" : "now-1M/M" explicity says 1 month back but lets say my query runs on April then it would not provide result from jan1st.

---

<div class="post-metadata">

### Author: ![Ayush\_Mathur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ayush_mathur/32/77134_2.png) [@Ayush\_Mathur](https://discuss.elastic.co/u/Ayush_Mathur)
#### Post date: [February 14, 2023, 10:24am UTC](https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451/4 "2023-02-14T10:24:20Z")

</div>

No, the above example was just to show how it can be done using just filter and setting size parameter. For a generic search/ query where timelines are not defined when query will be executed, you can opt for Search Template (keeping `lte: now` for your particular case) and pass the date as input from when you would like to search the documents from.  
To understand more, please refer: [Search templates | Elasticsearch Guide [8.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html)

---

<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: [March 14, 2023, 10:24am UTC](https://discuss.elastic.co/t/retrieve-data-having-date-from-1st-jan-to-current-date-on-a-date-field/325451/5 "2023-03-14T10:24:38Z")

</div>

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