【问题标题】:elastic search - update specific nested object弹性搜索 - 更新特定的嵌套对象
【发布时间】:2019-11-01 12:50:08
【问题描述】:

我有酒店文件,每个文件都有嵌套类型的房间。

{
id: hotel_id,
   rooms: [
     {
        id: room_id_1,
        name: "room 1 name"
      },
      {
        id: room_id_2,
        name: "room 2 name"
      },
      ....
  ]
}

我只想更新特定房间的单个字段。我正在尝试使用更新 api,从 ID 为 1 的酒店文档中更新 ID 为 2 的房间:

POST hotels/_update/1
{
  "script" : {
    "source" : "if(ctx._source.rooms.id == 2) { ctx._source.rooms.name = params.new_name }",
    "lang" : "painless",
    "params" : {
        "new_name" : "new room name"
    }  
  } 
}

我从 ES 收到此错误“非法列表快捷方式值 [id]”。

谢谢!

【问题讨论】:

    标签: elasticsearch elasticsearch-painless


    【解决方案1】:

    你需要像这样循环rooms

    POST hotels/_update/1
    {
      "script" : {
        "source" : "for (int i=0; i < ctx._source.rooms.length; i++) {if(ctx._source.rooms[i].id == 2) { ctx._source.rooms[i].name = params.new_name; break } }",
        "lang" : "painless",
        "params" : {
            "new_name" : "new room name"
        }  
      } 
    }
    

    【讨论】:

    • 很好,工作,谢谢!如此明显.. :) 只是为了将来阅读循环内的代码,当然必须使用“i”索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    相关资源
    最近更新 更多