1

In Java I have entity with nested objects:

public class MyData {
    @Id
    public UUID id;
    @Field(type = FieldType.Nested)
    public List<Entry> entries = new ArrayList<>(); // Entry is the class with two fields: String and int
}

I want to update entries adding new 'Entry' instance.

            Script script = new Script(
                    Script.DEFAULT_SCRIPT_TYPE,
                    "painless",
                    "ctx._source.entries += params.entry",
                    new HashMap<>(){{put("entry", value}}
            );

If value is json, then i'm getting error:

Cannot cast java.lang.String to java.util.ArrayList

If value is the object itself: new Entry("abc", 0) then i'm getting error:

java.lang.IllegalArgumentException: cannot write xcontent for unknown value of type class com.x.dto.Entry. And it doesn't accept Map either.

I understand this should be super-simple, but haven't found how to instantiate required object.

1 Answers1

0

Can be done this way:

            Script script = new Script(
                    Script.DEFAULT_SCRIPT_TYPE,
                    "painless",
                    "ctx._source.entries.add(['id': params.id, 'count':0])",
                    new HashMap<>(){{put("id", myValueHere}}
            );