问题
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