# Elasticsearch - Sort of Date fields using epoch\_millis

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-of-date-fields-using-epoch-millis/159213>\
**Category:** Elasticsearch\
**Created:** [December 3, 2018, 4:07pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-of-date-fields-using-epoch-millis/159213 "2018-12-03T16:07:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [December 3, 2018, 4:07pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-of-date-fields-using-epoch-millis/159213/1 "2018-12-03T16:07:59Z")

</div>

Hi all,  
I have stored date fields in Epoch\_millis format but as a string  
eg. {  
"date" : "1543257000000"  
}

Will this affect my sort query?  
"sort" : [  
{ "Date" : {"order" : "desc"}}

```
]
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 3, 2018, 5:00pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-of-date-fields-using-epoch-millis/159213/2 "2018-12-03T17:00:31Z")

</div>

it depends if you have dates with different length (like around 1970) and negative dates, as ascii sorting would be a bit different than time based sorting in that case.

Have you considered using the [reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-reindex.html) to fix this (along with your ingestion pipeline)?

--Alex

---

<div class="post-metadata">

**Author:** ![Nikesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikesh/32/86579_2.png) [@Nikesh](https://discuss.elastic.co/u/Nikesh)\
**Post date:** [December 4, 2018, 8:18am UTC](https://discuss.elastic.co/t/elasticsearch-sort-of-date-fields-using-epoch-millis/159213/3 "2018-12-04T08:18:18Z")

</div>

Thanks for your time and answer.  
But won't elasticsearch coerce it by default? [Coerce](https://www.elastic.co/guide/en/elasticsearch/reference/current/coerce.html#coerce) functionality is applied by default and this should work fine while sorting.

---

<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:** [January 1, 2019, 8:18am UTC](https://discuss.elastic.co/t/elasticsearch-sort-of-date-fields-using-epoch-millis/159213/4 "2019-01-01T08:18:36Z")

</div>

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