Sort on multiple fields Not working

Hi, I'm trying to sort on multiple fields like -

  1. sort on field1 first
  2. if there is a tie on field1, sort based on field 2.

POST sort_logic/_doc
{
"field1" : 4,
"field2" : "4"

}

POST sort_logic/_doc
{
"field1" : 4,
"field2" : "8"
}
POST sort_logic/_doc
{
"field1" : 4,
"field2" : "10"
}

GET sort_logic/_search?size=5
{
"sort" : [
{"field1": {"order": "desc"},
"field2.keyword": {"order": "desc"}
}
]
}

Sorting output - [4,"8"],[4, "4"], [4, "10"]
Expected sorting - [4, "10"], [4, "8"], [4, "4"]

The keyword subfield is a string so values are sorted as strings, not integers. When you sort strings you compare a character at a time, so "8" > "4" > "10", so the sorting output you are seeing is the expected one.

Thanks for you response. This makes sense now.
Is there a way to sort multiple fields(combination of keyword and integers) in such a way that keyword is cast to integer (or any other way that you suggest) while sorting and achieve the desired output.

[4, "10"], [4, "8"], [4, "4"]

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