# Date field Sorting in ElasticSearch

**URL:** <https://discuss.elastic.co/t/date-field-sorting-in-elasticsearch/103604>\
**Category:** Elasticsearch\
**Created:** [October 11, 2017, 7:27pm UTC](https://discuss.elastic.co/t/date-field-sorting-in-elasticsearch/103604 "2017-10-11T19:27:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paddy\_Mahadeva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paddy_mahadeva/32/44225_2.png) [@Paddy\_Mahadeva](https://discuss.elastic.co/u/Paddy_Mahadeva)\
**Post date:** [October 11, 2017, 7:27pm UTC](https://discuss.elastic.co/t/date-field-sorting-in-elasticsearch/103604/1 "2017-10-11T19:27:30Z")

</div>

Hi,

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?

---

<div class="post-metadata">

**Author:** ![Paddy\_Mahadeva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paddy_mahadeva/32/44225_2.png) [@Paddy\_Mahadeva](https://discuss.elastic.co/u/Paddy_Mahadeva)\
**Post date:** [October 12, 2017, 1:11pm UTC](https://discuss.elastic.co/t/date-field-sorting-in-elasticsearch/103604/2 "2017-10-12T13:11:30Z")

</div>

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

---

<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:** [November 9, 2017, 1:19pm UTC](https://discuss.elastic.co/t/date-field-sorting-in-elasticsearch/103604/3 "2017-11-09T13:19:39Z")

</div>

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