Sort Alphanumeric Strings in Ascending or Descending Order

Hi Team,

I have an index name object which has field Name whose mapping is defined as below

"name": {
"type": "keyword",
"fields": {
"keyword": {
"type": "keyword",
"normalizer": "case_insensitive_normalizer"
}
}
}

case_insensitive_normalizer is just to make strings sorted in case insensitive way. Name field has vales like item 0, item 1, item 10, item 2. If I try to sort object index by name as like name.keyword : "asc" it returns response as item 0, item 1, item 10, item 2. But correct response should be item 0, item 1, item 2, item 10. I see by default does sorting in lexicographical order, how to make numbers treated as strings an get correct sorting order?

To sort alphanumeric strings in Elasticsearch in numerical order (e.g., item 0, item 1, item 2, item 10), you can't directly achieve this with the default sorting because Elasticsearch sorts strings lexicographically. Instead, consider these two main approaches:

  1. Use a Script-based Sorting: Implement a custom script in your Elasticsearch query that extracts and converts the numerical part of your strings to integers for sorting. This method is flexible but might affect performance negatively for large datasets.
  2. Index a Separate Numeric Field: During data ingestion, extract the numeric part of your strings and store it in a separate numeric field. Then, sort using this numeric field to achieve the desired order. This method is more efficient but requires preprocessing your data.

The second method is generally preferred for better performance and scalability.

1 Like

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