Update API script - create array of structures

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:

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

Later I want to add additional structures:

ctx._source.myStructArr.add(struct_n);

but I receive the following error:

"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?

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:

ctx._source.myStructArr = [ struct_1 ];

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