I am trying to update several documents based on a search query with ES version 2.3.4. My use case is to search for documents where two fields match certain values and then add a new field with a certain value. So let's say I want to search all employees with first name "John" and last name "Smith" and add a new field "job" to their profiles with the value "Engineer" in it. So my first question is whether it is possible to do this using the "doc" option with the update_by_query API (the same way like with the update API).
If not, and script must be used (which is the way I'm doing it now) then maybe somebody can help me getting rid of the following error:
{"error":{"root_cause":[{"type":"class_cast_exception","reason":"java.lang.String cannot be cast to java.util.Map"}],"type":"class_cast_exception","reason":"java.lang.String cannot be cast to java.util.Map"},"status":500}
The code I'm using looks as follows:
curl -XPOST -s 'http://localhost:9200/test_index/_update_by_query?conflicts=proceed' -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"match": { "first_name" : "John" }
},
{
"match": { "last_name" : "Smith" }
}
]
}
}
}
},
"script" : "ctx._source.job = \"Engineer\""
}'
When sending the same query (without the "script" field) using the count API no error is reported and the correct number of documents is retuned.