Paddy Mahadeva
Paddy Mahadeva

Reputation: 145

Sorting by Date field in Elasticsearch

I have an index & type in elasticsearch which has a field mapping

"data_received_on": {
"type": "date",
"format": "YYYY-MM-DD"
},

I need to get the newest 10 records and i have about 5000 records that is spread out over the last 1 month.

ie. date_received_on field has values ranging from 2017-09-01 to 2017-09-30.

But my sorting does not give me the records in the right order when I order by this date. When I order by a number or string field it works well, but the moment I use a date field, it behaves unpredictably.

my sort condition is simple

"sort" : {
"data_received_on" : {
"order" : "desc"
}
}

What do I need to do differently to get the date field based sorting right?

Upvotes: 1

Views: 4588

Answers (1)

Paddy Mahadeva
Paddy Mahadeva

Reputation: 145

I found the issue.

The reason was my date format being YYYY-MM-DD. It should have been yyyy-MM-dd. Fixing that solved the problem

Upvotes: 1

Related Questions