# Update API script - create array of structures

**URL:** <https://discuss.elastic.co/t/update-api-script-create-array-of-structures/204418>\
**Category:** Elasticsearch\
**Created:** [October 21, 2019, 8:50am UTC](https://discuss.elastic.co/t/update-api-script-create-array-of-structures/204418 "2019-10-21T08:50:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MMH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmh/32/54449_2.png) [@MMH](https://discuss.elastic.co/u/MMH)\
**Post date:** [October 21, 2019, 8:50am UTC](https://discuss.elastic.co/t/update-api-script-create-array-of-structures/204418/1 "2019-10-21T08:50:58Z")

</div>

I am using Elasticsearch 6.5. I am writing a script for an update API in painless. I need to add structures to an array of structures. If this field does not exist in the document (I can detect that) I am creating a new array with the first element:

```auto
ctx._source.myStructArr = new def[] {struct_1};

```

Later I want to add additional structures:

```auto
ctx._source.myStructArr.add(struct_n);

```

but I receive the following error:

```auto
"type": "illegal_argument_exception",
"reason": "dynamic method [java.lang.Object[], add/1] not found"

```

How can I add an element to this array? Or maybe I should create/initialize an array in a different way?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 22, 2019, 3:43pm UTC](https://discuss.elastic.co/t/update-api-script-create-array-of-structures/204418/2 "2019-10-22T15:43:04Z")

</div>

An array is fixed size, so what you're seeing here is that you cannot add an element to a fixed size array. If you want to add elements, create an ArrayList instead, using this syntax:

```auto
ctx._source.myStructArr = [struct_1];

```

---

<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:** [November 19, 2019, 3:43pm UTC](https://discuss.elastic.co/t/update-api-script-create-array-of-structures/204418/3 "2019-11-19T15:43:07Z")

</div>

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