在弹性搜索中使用嵌套数组更新字段

use*_*300 15 arrays nested elasticsearch

我试图用数组更新文档中的字段.我想在"产品"字段中添加一个数组.我试过这个:

POST /index/type/1/_update
{
    "doc" :{
       "products": [
         {
           "name": "A",
           "count": 1
         },
         {
           "name": "B",
           "count": 2
         },
         {
           "name": "c",
           "count": 3
         }
       ]
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试运行代码时得到的错误响应:

{
   "error": {
      "root_cause": [
         {
            "type": "mapper_parsing_exception",
            "reason": "failed to parse [products]"
         }
      ],
      "type": "mapper_parsing_exception",
      "reason": "failed to parse [products]",
      "caused_by": {
         "type": "illegal_state_exception",
         "reason": "Can't get text on a START_OBJECT at 1:2073"
      }
   },
   "status": 400
}
Run Code Online (Sandbox Code Playgroud)

谁知道我做错了什么?

msh*_*eeb 21

消息"无法在START_OBJECT上获取文本"意味着Elasticsearch期望输入"string"类型但您尝试将对象作为输入.

如果你检查Kibana,你会发现那里存在"产品"字段,并被定义为字符串.但是,由于您正在输入字典列表,因此字段"产品"应该从头开始定义为对象(最好使用动态字段).一个例子是(参见https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html上的完整示例)

    "products": { 
      "dynamic": true,
      "properties": {}
    }
Run Code Online (Sandbox Code Playgroud)

但是,由于您已经拥有索引,因此无法更改映射,因此您需要删除索引,事先执行映射,然后执行更新.