I'm trying to update a nested object in Elasticsearch via the Java API, using a technique similar to what is outlined here. The problem is how to pass the json into the script. If I just blindly concatenate the json to the script string as suggested here, the Groovy does not compile. If you pass it in directly as a parameter, it just gets parsed as a string. If I try to use JsonSlurper, like:
String script = "ctx._source.pete = new groovy.json.JsonSlurper().parseText(json)";
Map<String, Object> params = ImmutableMap.of("json", json);
return new Script(script, ScriptService.ScriptType.INLINE, null, params);
Thanks to the guys at Elasticsearch for helping me with this. The answer is to convert the JSON to a Map
and then just pass the Map
as a param:
String script = "ctx._source.pete = jsonMap";
Map<? ,?> jsonMap = new ObjectMapper().readValue(json, HashMap.class);
Map<String, Object> params = ImmutableMap.of("jsonMap", jsonMap);
return new Script(script, ScriptService.ScriptType.INLINE, null, params);
I'm using org.codehaus.jackson.map.ObjectMapper
to do the conversion from JSON to the Map
.