# Script\_fields calcul in nested

**URL:** <https://discuss.elastic.co/t/script-fields-calcul-in-nested/172725>\
**Category:** Elasticsearch\
**Created:** [March 18, 2019, 8:35am UTC](https://discuss.elastic.co/t/script-fields-calcul-in-nested/172725 "2019-03-18T08:35:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rria](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rria/32/39671_2.png) [@rria](https://discuss.elastic.co/u/rria)\
**Post date:** [March 18, 2019, 8:35am UTC](https://discuss.elastic.co/t/script-fields-calcul-in-nested/172725/1 "2019-03-18T08:35:21Z")

</div>

I created new Index like This

```
   POST times/available
    {
      "name": "Nursery one",
      "available": [
        {
          "number_places": 1,
          "date": "2018-04-01"
        },
        {
          "number_places": 1,
          "date": "2018-04-14"
        },
        {
          "number_places": 1,
          "date": "2018-04-30"
        },
        {
          "number_places": 1,
          "date": "2018-05-01"
        },
        {
          "number_places": 1,
          "date": "2018-05-24"
        },
        {
          "number_places": 1,
          "date": "2018-06-05"
        }
      ]
    }
My Query Like this
POST times/available/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "scoreee": {
      "script": {
        "lang": "painless",
        "source": """
            int total = 200;
            for (int i = 0; i < doc['available'].length; ++i) {
              total += doc['available'];
            }
            return total;
          """
      }
    }
  }
}

```

But not working  
"failures" : [  
{  
"shard" : 0,  
"index" : "times",  
"node" : "9ykUreS0SLiGV9sRBNpZ5g",  
"reason" : {  
"type" : "script\_exception",  
"reason" : "runtime error",  
"script\_stack" : [  
"""  
total += doc['available.number\_places'][0].value;  
}

```
""",
            " ^---- HERE"
          ],
          "script" : """
            int total = 200;
            for (int i = 0; i < 20; ++i) {
              total += doc['available.number_places'][0].value;
            }
            return total;
""",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "dynamic getter [java.lang.Long, value] not found"
          }
        }
      }
    ]
```

---

<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:** [March 20, 2019, 8:41am UTC](https://discuss.elastic.co/t/script-fields-calcul-in-nested/172725/2 "2019-03-20T08:41:26Z")

</div>

Hey,

you are not taking into account that `doc['available']` already is an array, so you cannot access the `number_places` field directly. Try `doc['available'][0].number_places.value` (on top of my head, untested)

--Alex

---

<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:** [April 17, 2019, 8:41am UTC](https://discuss.elastic.co/t/script-fields-calcul-in-nested/172725/3 "2019-04-17T08:41:34Z")

</div>

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