shridhar mangoji
shridhar mangoji

Reputation: 1

Elastic Search: Sort Numeric which is defined as String

I have scenario where I need to Sort by the specific field, which is number but is defined as String. Is there any option where we can sort the string field as a numeric without using the script tag and without performing reindex

Upvotes: 0

Views: 393

Answers (1)

Val
Val

Reputation: 217314

If you don't want to reindex, you can update-by-query the index by modifying slightly the mapping.

Given your field is defined like this:

"sort-field": {
    "type": "text"
}

You can modify it to add a numeric sub-field as follows:

PUT your-index/_mapping
{
   "properties": {
       "sort-field": {
          "type": "text",
          "fields": {               <---- add a numeric sub-field
             "numeric": {
                "type": "integer"
             }
          }
       }
   }
}

And then simply update the index (add ?wait_for_completion=false if the index is big and the update would take a substantial amount of time):

POST your-index/_update_by_query

When the update is done, you can sort by sort-field.numeric

Upvotes: 1

Related Questions