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

使用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

相關文章
相關標籤/搜索