# Add elements in nested object with JAVA API 5.6 and painless

**URL:** <https://discuss.elastic.co/t/add-elements-in-nested-object-with-java-api-5-6-and-painless/129806>\
**Category:** Elasticsearch\
**Created:** [April 27, 2018, 9:58am UTC](https://discuss.elastic.co/t/add-elements-in-nested-object-with-java-api-5-6-and-painless/129806 "2018-04-27T09:58:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uldax](https://avatars.discourse-cdn.com/v4/letter/u/f0a364/32.png) [@Uldax](https://discuss.elastic.co/u/Uldax)\
**Post date:** [April 27, 2018, 9:58am UTC](https://discuss.elastic.co/t/add-elements-in-nested-object-with-java-api-5-6-and-painless/129806/1 "2018-04-27T09:58:49Z")

</div>

Hi everyone, I’m trying to add elements on a nested array on an existing document using painless script .

I’m using :

- elastic search server 5.6.9
- org.elasticsearch.client:elasticsearch:5.6.9
- org.elasticsearch.client:transport:5.6.9

I tried on Kibana the following request and it works.

```auto
POST user_transaction_1/transaction/100000/_update
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.transactions.addAll(params.newTransactions)",
    "params": {
        "newTransactions": [
          {"accountId":1,"amount":-49.9,"date": 15124284000}
        ]
    }
  }
}

```

But now I want to do this request using the Java API, I create the following script :

```java
Script updateScript = new Script(Script.DEFAULT_SCRIPT_TYPE,
        "painless",
        "ctx._source.transactions.add(params.newTransaction)",
        ImmutableMap.of("newTransaction", JSON));

UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(index, "transaction", key)
.setScript(updateScript)

```

If JSON var is a `String` (JSON of my transaction array) I've got :

```java
ScriptException[runtime error]; nested: NotSerializableExceptionWrapper[class_cast_exception: Cannot cast java.lang.String to java.util.Collection];
at org.elasticsearch.action.update.UpdateHelper.executeScript(UpdateHelper.java:266)

```

If JSON var is `List<String>` (Array of json element) I've got :

```java
MapperParsingException[object mapping for [transactions] tried to parse field [null] as object, but found a concrete value]

```

If JSON var is `List<Transaction>` (Array of POJO ) I've got :

```java
java.io.IOException: can not write type [class com...Transaction] at org.elasticsearch.common.io.stream.StreamOutput.writeGenericValue(StreamOutput.java:655) at org.elasticsearch.index.mapper.DocumentParser.parseObjectOrNested(DocumentParser.java:350)

```

Someone knows how to do this request using JAVA API ??

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 20, 2018, 8:21pm UTC](https://discuss.elastic.co/t/add-elements-in-nested-object-with-java-api-5-6-and-painless/129806/2 "2018-05-20T20:21:00Z")

</div>

> If JSON var is a String (JSON of my transaction array)

This doesn't work because you are calling `addAll` with a String, which takes a Collection. If you pass your data as a string, you need to parse it into a collection. Unfortunately painless does not have any built in json parsing.

> If JSON var is List (Array of json element)

I think this fails because the `transactions` object now has mixed data (list and map) which fails somewhere when trying to apply the update.

> If JSON var is List (Array of POJO )

This fails because Ealsticsearch does not know how to read/write your Transaction class.

I think the most important thing to notice is from your second example:

```auto
[object mapping for [transactions] tried to parse field [null]

```

When you are trying to update the field, it already this `transactions` is an object field. You need to set the mapping for this field at index creation to type `nested`.

---

<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:** [June 17, 2018, 8:21pm UTC](https://discuss.elastic.co/t/add-elements-in-nested-object-with-java-api-5-6-and-painless/129806/3 "2018-06-17T20:21:10Z")

</div>

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