# Sorting on field which in text data type but integer will be store

**URL:** <https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046>\
**Category:** Elasticsearch\
**Created:** [January 29, 2020, 5:05pm UTC](https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046 "2020-01-29T17:05:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopal\_Chauhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gopal_chauhan/32/51033_2.png) [@Gopal\_Chauhan](https://discuss.elastic.co/u/Gopal_Chauhan)\
**Post date:** [January 29, 2020, 5:05pm UTC](https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046/1 "2020-01-29T17:05:23Z")

</div>

Hi,

We have a field in index - TempNo which has to be text type but all values in this field are number (integer)

When i am doing sorting (desc) on this field , sort does not happen correctly. I am not getting result in desc order of TempNo.

It seems it is because of text type . How can I sort it correctly ? (type is text but sorting should happen based on Number)

Thanks,  
Gopal

---

<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:** [January 30, 2020, 2:46pm UTC](https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046/2 "2020-01-30T14:46:01Z")

</div>

If you want number based sorting, take a look at [multi fields](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/multi-fields.html) and index this field as a string as well as an integer. This way you can specify the string based for all your search operations and the integer based field for sorting.

---

<div class="post-metadata">

**Author:** ![Gopal\_Chauhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gopal_chauhan/32/51033_2.png) [@Gopal\_Chauhan](https://discuss.elastic.co/u/Gopal_Chauhan)\
**Post date:** [January 31, 2020, 4:11am UTC](https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046/3 "2020-01-31T04:11:02Z")

</div>

Hi Alex,

I tried the same option but 70001588 appear before 100416687.

I have changed the mapping like below  
"TempNo": {  
"type": "text",

```
          "fields": {
      "raw": { 
        "type": "keyword"
      }
    }

```

and using query as below

GET tempindex-20200113-1085/\_search  
{  
"from": 0,  
"query": {  
"bool": {  
"must": [  
{  
"simple\_query\_string": {  
"query": "2019"  
}  
}  
]  
}  
},  
"size": 100,  
"sort": [  
{  
"TempNo.raw": {  
"order": "desc"  
}  
}  
]  
}

Am I doing any thing wrong?

---

<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:** [January 31, 2020, 9:05am UTC](https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046/4 "2020-01-31T09:05:32Z")

</div>

please format your snippets properly. This is super hard to read.

In order to fully understand the issue, you need to provide a **minimal, but complete reproducible example** , that includes index creation, mapping, sample documents and your query, otherwise everyone is working on assumptions, how your may have configured Elasticsearch and come to wrong conclusions.

That said, a keyword field is still a string field and not an integer field, so you are still not properly sorting by a number.

---

<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:** [February 28, 2020, 9:05am UTC](https://discuss.elastic.co/t/sorting-on-field-which-in-text-data-type-but-integer-will-be-store/217046/5 "2020-02-28T09:05:42Z")

</div>

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