# Convert a string field to date in order to sort it

**URL:** <https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791>\
**Category:** Elasticsearch\
**Created:** [July 26, 2018, 2:14pm UTC](https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791 "2018-07-26T14:14:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)\
**Post date:** [July 26, 2018, 2:14pm UTC](https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791/1 "2018-07-26T14:14:18Z")

</div>

Hello!

I want to sort a field that happens to be a string. but the actual value of that string is date.  
Is there any way to convert this field to date?

GET /index/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"product": "blah"  
}  
}  
]  
}  
},  
"sort": {  
"string\_date": {  
"order": "desc"  
}  
}  
}

Thank you!

/Angelos

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 26, 2018, 2:27pm UTC](https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791/2 "2018-07-26T14:27:04Z")

</div>

Use a date datatype in your mapping.

---

<div class="post-metadata">

**Author:** ![Angelos](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Angelos](https://discuss.elastic.co/u/Angelos)\
**Post date:** [July 26, 2018, 2:44pm UTC](https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791/3 "2018-07-26T14:44:28Z")

</div>

Can I update the datatype?  
For example from:

"string\_date": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 100  
}

to:

"string\_date": {  
"type": "date"  
}

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 26, 2018, 3:06pm UTC](https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791/4 "2018-07-26T15:06:43Z")

</div>

No. You need to reindex.

---

<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:** [August 23, 2018, 3:06pm UTC](https://discuss.elastic.co/t/convert-a-string-field-to-date-in-order-to-sort-it/141791/5 "2018-08-23T15:06:48Z")

</div>

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