如何通过Java API更新Elasticsearch 2.2脚本中的嵌套对象 [英] How to update nested objects in Elasticsearch 2.2 script via Java API
问题描述
我试图通过Java API更新Elasticsearch中的嵌套对象,使用类似于此处。问题是如何将json传递到脚本中。如果我只是盲目地将json连接到脚本字符串,建议这里,Groovy不编译。如果直接将其作为参数传递,则只需将其解析为字符串即可。如果我尝试使用JsonSlurper,就像:
String script =ctx._source.pete = new groovy.json.JsonSlurper ).parseText(JSON);
映射< String,Object> params = ImmutableMap.of(json,json);
返回新脚本(脚本,ScriptService.ScriptType.INLINE,null,params);
我得到一个编译异常:无法解析类groovy.json.JsonSlurper
JsonSlurper方法的另一个问题似乎是Elasticsearch团队基本上有一个
谢谢你们在Elasticsearch的帮助我这个。答案是将JSON转换为 Map
,然后将 Map
作为参数传递:
String script =ctx._source.pete = jsonMap;
地图<? ,>?; jsonMap = new ObjectMapper()。readValue(json,HashMap.class);
映射< String,Object> params = ImmutableMap.of(jsonMap,jsonMap);
返回新脚本(脚本,ScriptService.ScriptType.INLINE,null,params);
我使用 org.codehaus.jackson.map.ObjectMapper
执行从JSON转换到地图
。
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);
I get a compilation exception: unable to resolve class groovy.json.JsonSlurper
A further problem with the JsonSlurper approach seems to be that the Elasticsearch team has basically disabled it in 2.2.
Does anyone know how to pass the json in correctly via the Java API?
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
.
这篇关于如何通过Java API更新Elasticsearch 2.2脚本中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!