# How to access doc values of a nested array inside a script?

**URL:** <https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829>\
**Category:** Elasticsearch\
**Created:** [December 23, 2015, 7:53am UTC](https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829 "2015-12-23T07:53:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunomiranda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brunomiranda/32/746_2.png) [@brunomiranda](https://discuss.elastic.co/u/brunomiranda)\
**Post date:** [December 23, 2015, 7:53am UTC](https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829/1 "2015-12-23T07:53:56Z")

</div>

I am using Elasticsearch 1.7

Given the following index, how would I select proper item in the nested array and access one of it's values? The purpose here is to use it inside the value inside a `script_score`.

```
# Create mapping
curl -XPUT localhost:9200/test/user/_mapping -d '
{
  "user" : {
    "properties" : {
      "name" : {
        "type" : "string"
      },
      "skills" : {
        "type": "nested", 
        "properties" : {
          "skill_id" : {
            "type" : "integer"
          },
          "recommendations_count" : {
            "type" : "integer"
          },
        }
      }
    }
  }
}
'

```

Let's index some data.

```
curl -XPUT localhost:9200/test/user/1 -d '
{
   "name": "John",
   "skills": [
      {
         "skill_id": 100
         "recommendations_count": 5
      },
      {
         "skill_id": 200
         "recommendations_count": 3
      }
   ]
}
'

curl -XPUT localhost:9200/test/user/1 -d '
{
   "name": "Mary",
   "skills": [
      {
         "skill_id": 100
         "recommendations_count": 9
      },
      {
         "skill_id": 200
         "recommendations_count": 0
      }
   ]
}
'

```

My query filters by skill\_id and this works well. I then want to be able to use `script_score` to boost the score of the `user` documents with a higher `recommendations_count` **for the given skill\_id.** (\<-- this is key).

```
curl -XPOST localhost:9200/test/user/_search -d '
{      
    "query":{
      "function_score":{
        "query":{
          "bool":{
            "must":{
              "nested":{
                "path":"skills",
                "query":{
                  "bool":{
                    "must":{
                      "term":{
                        "skill_id":100
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "functions":[
          {
            "script_score": {
               "script": "sqrt(1.2 * doc['skills.recommendations_count'].value)"   
            }
          }            
        ]
      }
    }
  }
} 

```

How do I access the `skills` array from within the `script`, find the 'skill\_id: 100' item in the array, and then use its `recommendations_count` value? The `script_score` above doesn't currently work (score is always 0 regardless of the data, so I assume `doc['skills.recommendations_count'].value` is not looking in the right place.

---

<div class="post-metadata">

**Author:** ![vivekshastry](https://avatars.discourse-cdn.com/v4/letter/v/eb8c5e/32.png) [@vivekshastry](https://discuss.elastic.co/u/vivekshastry)\
**Post date:** [September 22, 2016, 9:54am UTC](https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829/2 "2016-09-22T09:54:08Z")

</div>

Any luck on this issue? Facing a similar issue in terms of accessing array from with in the script.

---

<div class="post-metadata">

**Author:** ![brupm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brupm/32/746_2.png) [@brupm](https://discuss.elastic.co/u/brupm)\
**Post date:** [September 22, 2016, 6:19pm UTC](https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829/3 "2016-09-22T18:19:39Z")

</div>

No, what I did is use `field-value-factor` instead: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor) - then you can get the value from the nested attribute easily:

```
json.query do
  # Take the recommendations_count from that skill
  json.function_score do
    json.field_value_factor do
      json.field 'skills.recommendations_count'
    end
  end
end
```

---

<div class="post-metadata">

**Author:** ![vivekshastry](https://avatars.discourse-cdn.com/v4/letter/v/eb8c5e/32.png) [@vivekshastry](https://discuss.elastic.co/u/vivekshastry)\
**Post date:** [September 23, 2016, 3:38am UTC](https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829/4 "2016-09-23T03:38:35Z")

</div>

Thanks for the reply @brupm.

Could you pl give us the exact query that you used for your usecase with field\_vlaue\_factor for full clarity on syntax which is where we end up spending lot of time. We are looking for snippet of function\_score which embeds the above logic that you have given.

---

<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:** [July 5, 2017, 10:17pm UTC](https://discuss.elastic.co/t/how-to-access-doc-values-of-a-nested-array-inside-a-script/37829/5 "2017-07-05T22:17:58Z")

</div>


