Q:非洲食人族的酋长吃什么?

nested 嵌套数据新增,报mapper_parsing_exception

Elasticsearch | 作者 dongchunfu | 发布于2017年08月22日 | 阅读数:7320

mapping:
curl -XPUT 'localhos:9200/test_ool?pretty' -d'
{
"mappings":{
"ool":{
"properties":{
"id":{
"type":"long"
},
"logol":{
"type":"string"
},
"student":{
"properties":{
"id":{
"type":"long"
},
"name":{
"type":"string"
},
"books":{
"type":"nested",
"properties":{
"name":{
"type":"string"
},
"id":{
"type":"long"
}
}
}
}
}
}
}
}
}
'
index:
curl -XPUT '10.12.31.110:9200/test_ool/ool/1?pretty' -d'
{
"id":"2",
"logol":"apple",
"student":{
"id":1,
"name":"dongchunfu",
"books":[
{
"id":1,
"name":"三毛",
"size":1024
}
]
}
}
'
groovy:
curl -XPOST '10.12.31.110:9200/test_ool/ool/1/_update?pretty' -d'
{
"script" : "if(ctx._source.student.books==null){ctx._source.student.books=listValue}else{ctx._source.student.books.add(objectValue)}",
"params" : {
listValue:[{"size":2017,"name":"rabbitmq in action","id":5}],
objectValue:{"size":2017,"name":"rabbitmq in action","id":5}
}
}
'

rest test:
@Test
public void testRestClient() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("10.12.31.110", 9200, "http")).build();

Map<String, String> param = new HashMap<>();
String script = "def f = ctx._source.student; f.books.findAll{ f.books +=listValue}";

Map<String, String> params = new HashMap<>();
Map<String, String> objectParam = new HashMap<>();
objectParam.put("name", "http");
objectParam.put("id", "15");
List<Map<String, String>> listParam = new ArrayList<>();
listParam.add(objectParam);

params.put("listValue", GSON.toJson(listParam));
params.put("objectValue", GSON.toJson(objectParam));

param.put("listValue", GSON.toJson(listParam));
param.put("objectValue", GSON.toJson(objectParam));

param.put("params", GSON.toJson(params));

StringBuilder dBuilder = new StringBuilder();
dBuilder.append("%20-d%20'").append(GSON.toJson(param)).append("'");

final Script groovyScript = new Script(script, ScriptService.ScriptType.INLINE, "groovy", params);
HttpEntity entity = new StringEntity(GSON.toJson(groovyScript), ContentType.APPLICATION_JSON);

final Response post = restClient.performRequest("POST", "/test_ool/ool/1/_update" /*+ dBuilder.toString()*/,
Collections.emptyMap(), entity);

System.out.println(post);
}

exception:
org.elasticsearch.client.ResponseException: POST http://10.12.31.110:9200/test_ool/ool/1/_update: HTTP/1.1 400 Bad Request
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"object mapping for [student.books] tried to parse field [null] as object, but found a concrete value"}],"type":"mapper_parsing_exception","reason":"object mapping for [student.books] tried to parse field [null] as object, but found a concrete value"},"status":400}
已邀请:

dongchunfu - 80后IT 男

赞同来自:

直接.fieldName += 就会报错

dongchunfu - 80后IT 男

赞同来自:

已成功解决,在朋友的帮忙下,试出来的!
一下加粗字段,不要进行gson的转换操作,查阅源码 脚本的参数Map<String,? extends Object>,可将Map直接使用@test
public void testRestClient() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("10.12.31.110", 9200, "http")).build();

Map<String, String> param = new HashMap<>();
String script = "def f = ctx._source.student; f.books.findAll{ f.books +=listValue}";

Map<String, String> params = new HashMap<>();
Map<String, String> objectParam = new HashMap<>();
objectParam.put("name", "http");
objectParam.put("id", "15");
List<Map<String, String>> listParam = new ArrayList<>();
listParam.add(objectParam);

//params.put("listValue", GSON.toJson(listParam));
//params.put("objectValue", GSON.toJson(objectParam));

 
params.put("listValue", objectParam);
params.put("objectValue", listParam);


param.put("listValue", GSON.toJson(listParam));
param.put("objectValue", GSON.toJson(objectParam));

param.put("params", GSON.toJson(params));

StringBuilder dBuilder = new StringBuilder();
dBuilder.append("%20-d%20'").append(GSON.toJson(param)).append("'");

final Script groovyScript = new Script(script, ScriptService.ScriptType.INLINE, "groovy", params);
HttpEntity entity = new StringEntity(GSON.toJson(groovyScript), ContentType.APPLICATION_JSON);

final Response post = restClient.performRequest("POST", "/test_ool/ool/1/_update" /*+ dBuilder.toString()*/,
Collections.emptyMap(), entity);

System.out.println(post);
}

要回复问题请先登录注册