Date type question : Get Documents by Year


(Abhishek Kumar) #1

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)


(Mark Walkom) #2

Should be easy enough, what have you tried?


(Abhishek Kumar) #3

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

(Abhishek Kumar) #5

"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"
}
}
}
}


(system) #6

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