I am trying to add a list or a single object to a Array field on a Elastic Search Index, the index is as shown below:
"_index" : "class_index",
"_id" : "1234",
"_source" : {
"externalId" : 1234,
"description" : "10th grade",
"users" : [
{
"name" : useer 1,
"age" : "16",
"active" : 1
},
{
"name" : user 2,
"age" : "15",
"adtive" : 1
}
]
}
I am using Java High level Rest client to add a single object to the array field by conveting the object to a JsonNode. My Question is two part, is it possible to pass an Array of user objects instead of a single object? Also, I am running into an issue as shown below with this approach.
//usere here is a java object
Map<String, Object> paramMap = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
//user object: {"name":user name,"agee":"25","active":0}
JsonNode node = mapper.convertValue(userList.getUsers().get(0), JsonNode.class);
Map<String, Object> jsonMap = mapper.convertValue(node, Map.class);
final UpdateRequest indexRequest = new UpdateRequest(INDEX, TYPE, _id);
Script inline = new Script(ScriptType.INLINE, "painless",
"ctx._source.characters += params.user)", singletonMap("user", jsonMap));
indexRequest.script(inline);
restClient.update(indexRequest, RequestOptions.DEFAULT);
I am not sure if I am missing something here but I still get the following exception trying to update the field with a single object:
caused by: org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=illegal_argument_exception, reason=unexpected token [')'] was expecting one of [{<EOF>, ';'}].]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509) ~[elasticsearch-6.8.10.jar:6.8.10]
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420) ~[elasticsearch-6.8.10.jar:6.8.10]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:450) ~[elasticsearch-6.8.10.jar:6.8.10]
Can we achieve the goal without using a script?