Elasticsearch Scripting: updating array value

﹥>﹥吖頭↗ 提交于 2019-12-05 00:16:55

问题


This is my document

{
    "name": "andrew",
    "b": [{"x":"c1", "y": 0}, {"x":"c2", "y": 0}]
}

I want to find element in the array field "b" and update the entire object. I tried this script but it does not update. Any ideas?

{
    "script": "for (item in ctx._source.b) { if (item['x'] == x_id) { item = newobj; } };",
    "params": {
        "x_id": "c1",
        "newobj" : {"x":"c1", "y": 4222}
    },
    "lang":"groovy"
}

回答1:


Use this instead:

{
  "script": "for (int i=0;i<ctx._source.b.size();i++) { item=ctx._source.b[i]; if (item['x'] == x_id) { ctx._source.b[i] = newobj} };",
  "params": {
    "x_id": "c1",
    "newobj": {
      "x": "c1",
      "y": 4222
    }
  },
  "lang": "groovy"
}


来源:https://stackoverflow.com/questions/37712228/elasticsearch-scripting-updating-array-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!