Array Sort Script to sort array by index

PUT /my-index-000001/_doc/1?refresh
{
   "product": "chocolate",
   "salesManager" :["a","z","r"]
}
PUT /my-index-000001/_doc/2?refresh
{
   "product": "chocolate",
   "salesManager" :["v","f","d"]
}
PUT /my-index-000001/_doc/3?refresh
{
   "product": "chocolate",
   "salesManager" :["z","a","a"]
}
POST /my-index-000001/_search
{
   "sort": {
    "_script": {
      "type": "String",
      "script": {
        "lang": "painless",
        "source": "if(doc['salesManager'].size()>0) { return doc['salesManager'][0].toLowerCase()} return 'z';",
        "params": {
          "factor": 1.1
        }
      },
      "order": "asc"
    }
  }
}

I need to sort asc the index data using salesManager 0th index sort
so the correct sort order is on doc id is 1->2->3

but I am getting 3->2->1

Accessing 0th Index in painless is giving the value after sorting that field.
Why Array of Strings are getting sorted automatically ?

Hi @Dakshay_Agarwal

Maybe someone from the Elastic team can confirm but according to this doc there is an explanation for the behavior of your query.

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