# Can't get nested value on script

**URL:** https://discuss.elastic.co/t/cant-get-nested-value-on-script/229251
**Category:** Elasticsearch
**Created:** [April 22, 2020, 11:56am UTC](https://discuss.elastic.co/t/cant-get-nested-value-on-script/229251 "2020-04-22T11:56:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joel.ortin](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@joel.ortin](https://discuss.elastic.co/u/joel.ortin)
#### Post date: [April 22, 2020, 11:56am UTC](https://discuss.elastic.co/t/cant-get-nested-value-on-script/229251/1 "2020-04-22T11:56:38Z")

</div>

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:

```auto
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:

```auto
{
  "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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 20, 2020, 11:56am UTC](https://discuss.elastic.co/t/cant-get-nested-value-on-script/229251/2 "2020-05-20T11:56:45Z")

</div>

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