# Update by query gives java error

**URL:** https://discuss.elastic.co/t/update-by-query-gives-java-error/62811
**Category:** Elasticsearch
**Created:** [October 12, 2016, 1:54pm UTC](https://discuss.elastic.co/t/update-by-query-gives-java-error/62811 "2016-10-12T13:54:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mahdy\_S](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahdy\_S](https://discuss.elastic.co/u/Mahdy_S)
#### Post date: [October 12, 2016, 1:54pm UTC](https://discuss.elastic.co/t/update-by-query-gives-java-error/62811/1 "2016-10-12T13:54:02Z")

</div>

Hello,

I am trying to update several documents based on a search query. 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.  
To do this, I am using the update by query API with the "script" option. My code looks like follows:

> curl -XPOST -s '[http://localhost:9200/test\_index/\_update\_by\_query?conflicts=proceed](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""  
> }'

Whenever I run this on ubuntu terminal I get 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}

However, sending the same query (without the "script" field) using the count API works fine and gives the correct number of documents.

My questions:

- Where is exactly the error in the code I'm using? Unfortunately the error message does not specify in which line the problem is
- Is it possible to use "doc" instead of "script" in the update by query API exactly the same way this is used in Update API?

Elasticsearch version is 2.3.4

---

<div class="post-metadata">

### Author: ![acabrol](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@acabrol](https://discuss.elastic.co/u/acabrol)
#### Post date: [October 13, 2016, 10:23pm UTC](https://discuss.elastic.co/t/update-by-query-gives-java-error/62811/2 "2016-10-13T22:23:22Z")

</div>

I have the same issue with elasticsearch 2.3.4

```
curl -XPOST 'localhost:9200/watch/_update_by_query' -d '{
    "query":{ "type" : {"value" : "doc"} },
    "script" : "if (ctx._source[\"topics\"] != null) {ctx._source.remove(\"topics\")"}
}'

```

and result:

> {"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}

topics field is a nested object (if it can help).

---

<div class="post-metadata">

### Author: ![Gautam\_Vanani](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@Gautam\_Vanani](https://discuss.elastic.co/u/Gautam_Vanani)
#### Post date: [March 30, 2017, 4:22am UTC](https://discuss.elastic.co/t/update-by-query-gives-java-error/62811/3 "2017-03-30T04:22:05Z")

</div>

I'm facing same issue while appeding data in array type using script,

> using update\_by\_query:
> 
> > POST /srk/demo\_array/\_update\_by\_query  
> > {  
> > "script" : "ctx.\_source.array += arr",  
> > "params" : {  
> > "arr" : ["a", "b"]  
> > }  
> > }

> java 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  
> > }

---

<div class="post-metadata">

### Author: ![seanziee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seanziee/32/45151_2.png) [@seanziee](https://discuss.elastic.co/u/seanziee)
#### Post date: [May 26, 2017, 10:05pm UTC](https://discuss.elastic.co/t/update-by-query-gives-java-error/62811/4 "2017-05-26T22:05:34Z")

</div>

I had this same problem but figured it out. You need to add "inline" to make it work:

```
POST /logstash-*/_update_by_query
{
  "script": {
    "inline": "ctx._source.remove(\"ENTERFIELD\")"
  },
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "ENTERFIELD"
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 9:59pm UTC](https://discuss.elastic.co/t/update-by-query-gives-java-error/62811/5 "2017-07-05T21:59:58Z")

</div>


