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.
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 :
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 :
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 :
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.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 ??