使用java操做es,添加文档时,出现以下错误:css
[{"type":"mapper_parsing_exception","reason":"object mapping for [enclosure_infor] tried to parse field [enclosure_infor] as object, but found a concrete value"}]
场景:
enclosure_infor这个字段的mapping以下,是个nested类型的:java
"enclosure_infor": {
"type": "nested",
"properties": {
"document": {
"type": "text"
},
"enclosure": {
"type": "keyword"
},
"link": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
错误缘由:
因为这个字段设计目的是为了存以下格式的数组:web
{
"_index": "policy_document",
"_type": "policy_document",
"_id": "aaatest2",
"_version": 1,
"found": true,
"_source": { "level": "国家", "enclosure_infor": [ { "document": "aaaaa", "link": "aa.link.com" }, { "document": "bbbbb", "link": "bb.link.com" } ] } }
在java中处理时,把这个字段的值转为json了:json
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map = new HashedMap(8);
String base64Str = BinUtil.fileToBase64Str(filei);
map.put("enclosure",multipartFile.getOriginalFilename());
map.put("document",base64Str);
list.add(map);
String s = JSON.toJSONString(list, prettyFormat)
这样转换后的结果就是,最终post的语句相似以下示例:数组
POST policy_document/policy_document/aaatest/_create
{
"level":"国家",
"enclosure_infor":"[{\"document\":\"document0\",\"enclosure\":\"enclosure0\"}]",
"plat_from":11,
"reference_number":"国6",
"title":"汪文档6",
"id":"3331d0d5b1354424aaa2dd10232dd563",
"_id_":"3331d0d5b1354424aaa2dd10232dd563",
"launch_date":"2018-05-02",
"launch_department":"国家统计局6"
}
注意这个字段的值,被转成这样了:bash
"enclosure_infor":"[{
\"document\":\"document0\",\"enclosure\":\"enclosure0\"}]"
报错
此时若是执行此语句就会报错以下:app
"reason":"object mapping for [] tried to parse field [] as object, but found a concrete value"
而正确的应该是这样的:svg
POST policy_document/policy_document/aaatest2/_create
{
"level":"国家",
"enclosure_infor":[
{
"document":"aaaaa","link":"aa.link.com"},
{
"document":"bbbbb","link":"bb.link.com"}]
}
解决方式
综合上面,能够看到,最终解决方式为,这个字段的值,应该是个list类型,而不是String类型,因此,把这个enclosure_infor的java类型改成List类型便可。
本文同步分享在 博客“IT云清”(CSDN)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。post