Can't get nested value on script

Hi,
I'm trying to do some sorting functionality (like multiply a calculated distance by a number). I have a nested relation where i have multiple objects with a geopoint, and I want to get the closest one and sort the root document by that. But any time I try to get the field on the script, It doesn't exist.

Elasticsearch version 7.1

Right now I'm just trying to return the closest object but it acts like there is no value (always return 0, and the result return that stores has values):

Script:

POST _scripts/distance-popularity-sort
{
  "script": {
    "lang": "painless",
    "source": "if ( doc['stores.location'].size() == 0) return 0; return doc['stores.location'].arcDistance(params.latitude, params.longitude)"
  }
}

Get query:

{
  "script_fields":{
      "sort_value":{
         "script":{
            "id":"distance-popularity-sort",
            "params":{
               "latitude":40.416690826416016,
               "location":"stores.location",
               "longitude":-3.700345516204834
            
            }         
         }, "ignore_failure":false     
      } 
  },
  "sort":[
      {
         "_script":{
            "script":{
               "id":"distance-popularity-sort",
               "params":{
                  "latitude":40.416690826416016,
                  "location":"stores.location",
                  "longitude":-3.700345516204834
               
               }
            },
            "type":"number",
            "order":"desc"      
         }    
      }
  ],
  "_source":{
      "includes":[
         "id",
         "stores"
      
      ]
   
  }
}

Thanks

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