# How to add an element into multi level nested Arrays in Elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-add-an-element-into-multi-level-nested-arrays-in-elasticsearch/161164>\
**Category:** Elasticsearch\
**Created:** [December 17, 2018, 2:05pm UTC](https://discuss.elastic.co/t/how-to-add-an-element-into-multi-level-nested-arrays-in-elasticsearch/161164 "2018-12-17T14:05:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gereksiz\_Seyler](https://avatars.discourse-cdn.com/v4/letter/g/e5b9ba/32.png) [@Gereksiz\_Seyler](https://discuss.elastic.co/u/Gereksiz_Seyler)\
**Post date:** [December 17, 2018, 2:05pm UTC](https://discuss.elastic.co/t/how-to-add-an-element-into-multi-level-nested-arrays-in-elasticsearch/161164/1 "2018-12-17T14:05:41Z")

</div>

My data:

```
"_source": {
"owner": "102195915017611526666",
"boxes": [
{
"box_id": "999",
"items": [
    {
        "item_id": "123",
        "item_key": "x",
        "item_value": "y"
    }
]
},
{
"box_id": "888",
"items": []
}
]
}

```

And I want to add an item into items Array which one is in the second box ( `box_id:888` ) . I know how to add item into boxes array . I have researched but found nothing. I'm new in elasticsearch. Thank you for your helps

Edit: I also tried:

```auto
"script" : {
        "inline": "for (int i = 0; i<ctx._source.boxes.length; i++ ){if(ctx._source.boxes[i].box_id == '888') ctx._source.boxes[i].add(params.newitem);}",
        "params" : {
            "newitem" : {"item_key": "x", "item_value": "y", "item_id": "342246995990409"}
    }
},
"query": {
    "match": {
        "owner": "102195915017611526666"
    }
    }
}

```

But I'am getting this error:

```auto
"script_stack": [
                "ctx._source.boxes[i].add(params.newitem);}",
                " ^---- HERE"
            ],

```

I have asked many people but I couldn't get any answer from anyone. Is there any elasticsearch genius? I would be very appreciate.

---

<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:** [January 14, 2019, 2:05pm UTC](https://discuss.elastic.co/t/how-to-add-an-element-into-multi-level-nested-arrays-in-elasticsearch/161164/2 "2019-01-14T14:05:42Z")

</div>

Closed on January 14, 2019, 2:05pm UTC

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