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


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)


If you want number based sorting, take a look at multi fields 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.

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?

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.

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