Date type question : Get Documents by Year

I have a DATE field in an index with
"type" : "date" &
"format" : "strict_date_optional_time||epoch_millis"

I need to get all the Documents in a mentioned year(say : return all documents whose date field has year 2017)

Should be easy enough, what have you tried?

Its working in case of query string :

i) http://localhost:9200/_search?q=2017

But
when i am finding the same with the attibuteName , its returning null .Dont know why?

ii)http://localhost:9200/_search?q=mydateAttributeName:2017

I cant use i) , cause the any other field in the document can also have 2017 as text.

Is it possible to get it done using Query-DSL?

  • mydateAttributeName = 2017-07-30T18:30:00.000Z

"range-query" did the job :slight_smile:

To get the records in year 2017 using query-dsl ,

{
"query": {
"range": {
"myDateAttributeName": {
"gte": "2016-12-31T00:00:00.000Z",
"lte": "2018-01-01T00:00:00.000Z"
}
}
}
}

1 Like

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