# How to access objects fields nested value in runtime field painless (or expression) scripts?

**URL:** <https://discuss.elastic.co/t/how-to-access-objects-fields-nested-value-in-runtime-field-painless-or-expression-scripts/302699>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 19, 2022, 9:13am UTC](https://discuss.elastic.co/t/how-to-access-objects-fields-nested-value-in-runtime-field-painless-or-expression-scripts/302699 "2022-04-19T09:13:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_Ostadi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammad_ostadi/32/104539_2.png) [@Mohammad\_Ostadi](https://discuss.elastic.co/u/Mohammad_Ostadi)\
**Post date:** [April 19, 2022, 9:13am UTC](https://discuss.elastic.co/t/how-to-access-objects-fields-nested-value-in-runtime-field-painless-or-expression-scripts/302699/1 "2022-04-19T09:13:18Z")

</div>

I have a document like this:

```auto
POST test/_doc
{
  "name":"Ali",
  "age":20,
  "grades":[
    {
    "title":"math",
    "score":1
    },
    {
    "title":"english",
    "score":2
    },
    {
    "title":"physic",
    "score":3
    }
  ]
}

```

and I want to have a runtime field with avg of each doc grade scores. then use that run time field in my queries. but I cant access to score in grades objects and there is nothing about accessing objects fields in scripting documentations.

i try these but all produce errors and none of them works:

```auto
doc['grades'][0]['score'].value
doc['grades.0.score'].value
doc['grades'].0.score
doc['grades'][0].score
field('grades.0.score').get(null)
for(i in field('grades').get([])){
  sum += i.score;
 sum += i['score']; // or this
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 20, 2022, 9:25am UTC](https://discuss.elastic.co/t/how-to-access-objects-fields-nested-value-in-runtime-field-painless-or-expression-scripts/302699/2 "2022-04-20T09:25:33Z")

</div>

Hey,

how about this?

```auto
POST test/_doc/1
{
  "name":"Ali",
  "age":20,
  "grades":[
    {
    "title":"math",
    "score":1
    },
    {
    "title":"english",
    "score":2
    },
    {
    "title":"physic",
    "score":3
    }
  ]
}

GET test/_mapping

GET test/_search
{
  "runtime_mappings": {
    "average": {
      "type": "double",
      "script": {
        "source": """
        emit(doc['grades.score'].sum()/doc['grades.score'].size());
        """ 
      }
    }
  },
  "aggs": {
    "average": {
      "stats": {
        "field": "average"
      }
    }
  }
}

```

---

<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 18, 2022, 9:25am UTC](https://discuss.elastic.co/t/how-to-access-objects-fields-nested-value-in-runtime-field-painless-or-expression-scripts/302699/3 "2022-05-18T09:25:53Z")

</div>

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