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

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:

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.

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

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:

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

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.

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

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