"reason":"object mapping for [] tried to parse field [] as object, but found a concrete value"

使用java操作es,添加文档时,出现如下错误:

[{"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类型的:

"enclosure_infor": {
            "type": "nested",
            "properties": {
              "document": {
                "type": "text"
              },
              "enclosure": {
                "type": "keyword"
              },
              "link": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }

错误原因:

由于这个字段设计目的是为了存如下格式的数组:

{
  "_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了:

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"
}

注意这个字段的值,被转成这样了:

"enclosure_infor":"[{\"document\":\"document0\",\"enclosure\":\"enclosure0\"}]"

报错

此时如果执行此语句就会报错如下:

"reason":"object mapping for [] tried to parse field [] as object, but found a concrete value"

而正确的应该是这样的:

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类型即可。

你可能感兴趣的:("reason":"object mapping for [] tried to parse field [] as object, but found a concrete value")