How I can update data using “update_by_query” in Elasticsearch 2.x?

ⅰ亾dé卋堺 提交于 2021-01-28 19:08:42

问题


I need multi update documents in elastic by some query. I found one solution. Like this:

{
    "query": {
        "term": {
            "name": "some name"
        }
    },
    "script": {
        "inline": "ctx._source.some_field = \"value\""
    }
}

But it's not exactly what I need. Because one request will update 10, or 20 fields, for example. So it's uncomfortable to generate "script" string for many fields. Ideally, I need something like this:

{
    "query": {
        "term": {
            "name": "some name"
        }
    },
    "doc": {
        "field1": "value1",
        "field2": "value2",
        "field3": "value3",
        "field4": "value4",
        "field5": "value5",
        ...
        "fieldN": "valueN",
    }
}

In order to implement something like the above, I would appreciate an alternative


回答1:


You can use a script like this one:

  "script": {
    "inline": "fieldsAndValues.each{ k, v -> ctx._source[k] = \"${v}\" }",
    "lang": "groovy",
    "params": {
      "fieldsAndValues": {
        "field1": "value1",
        "field2": "value2",
        "field3": "value3"
      }
    }
  }


来源:https://stackoverflow.com/questions/38345906/how-i-can-update-data-using-update-by-query-in-elasticsearch-2-x

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