# How convert update\_by\_query with params as array to Java Rest client SDK

**URL:** <https://discuss.elastic.co/t/how-convert-update-by-query-with-params-as-array-to-java-rest-client-sdk/301870>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 7, 2022, 1:05pm UTC](https://discuss.elastic.co/t/how-convert-update-by-query-with-params-as-array-to-java-rest-client-sdk/301870 "2022-04-07T13:05:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitaly1233](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vitaly1233/32/80196_2.png) [@vitaly1233](https://discuss.elastic.co/u/vitaly1233)\
**Post date:** [April 7, 2022, 1:05pm UTC](https://discuss.elastic.co/t/how-convert-update-by-query-with-params-as-array-to-java-rest-client-sdk/301870/1 "2022-04-07T13:05:55Z")

</div>

Hi,  
Working with version 7.15  
Trying to do "update all by query " using script.  
The main idea to iterate the params in array and update as in the query below to all matched documents.

The script:  
POST : [http://11.1.1.1:9200/index/\_update\_by\_query?refresh&conflicts=proceed&max\_docs=10](http://11.1.1.1:9200/index/_update_by_query?refresh&conflicts=proceed&max_docs=10)

```auto
      "query": {
        "bool": {
          "must": {
            "bool": {
              "should": [
                {
                  "match": {
                    "_es_attributes": "classifier.old1"
                  }
                },
                {
                  "match": {
                    "_es_attributes": "classifier.old2"
                  }
                }
              ]
            }
          }
        }
      },
      "script": {
        "source": "for (int i=0 ; i < params.src.size() ; i++) { if (ctx._source._es_attributes.contains(params.src[i].oldtag)) { ctx._source._es_attributes[ctx._source._es_attributes.indexOf(params.src[i].oldtag)] = params.src[i].newtag;}}",
        "lang": "painless",
        "params": {
          "src": [
            {
              "oldtag": "classifier.Email.old1",
              "newtag": "classifier.Email.new1"
            },
    				 {
              "oldtag": "classifier.Email.old2",
              "newtag": "classifier.Email.new2"
            }
          ]
        }
      }
    }

```

What i've trying so far

public class Diff {  
String key;  
Object oldval;  
Object newval;

```
	public Diff(String key, Object oldval, Object newval) {
		this.key = key;
		this.oldval = oldval;
		this.newval = newval;
	}
}

```

```auto

    String script = "for (int i=0 ; i < params.src.size() ; i++) { if (ctx._source._es_attributes.contains(params.src[i].oldval)) { ctx._source._es_attributes[ctx._source._es_attributes.indexOf(params.src[i].oldval)] = params.src[i].newval;}}";
    
    			BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    			diffList.forEach(a -> {
    				queryBuilder.should(QueryBuilders.matchQuery("_es_attributes", a.getOldval()));
    			});
    
    params = Collections.singletonMap("src", gson.toJsonTree(diffList).toString())
    
    
    UpdateByQueryRequest request =
    				new UpdateByQueryRequest(indexName);
    		request.setRefresh(true);
    		request.setConflicts("proceed");
    		request.setQuery(queryBuilder);
    		request.setScript(
    				new Script(
    						ScriptType.INLINE, "painless",
    						script,
    						params));
    		BulkByScrollResponse response = elasticsearchRestClient.updateByQuery(request,
    				getCustomTimeoutOptions(2 * indexBulkTimeout));

```

But I got from ES error:

ElasticsearchStatusException[Elasticsearch exception [type=script\_exception, reason=runtime error]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal\_argument\_exception, reason=dynamic method [java.lang.String, size/0] not found]

What i'm missing

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 7, 2022, 2:33pm UTC](https://discuss.elastic.co/t/how-convert-update-by-query-with-params-as-array-to-java-rest-client-sdk/301870/2 "2022-04-07T14:33:38Z")

</div>

I do not understand why your java objects in the `src` map are query builders? In the JSON you provide those are a list

> "params": {  
> "src": [  
> {  
> "oldtag": "classifier.Email.old1",  
> "newtag": "classifier.Email.new1"  
> },  
> {  
> "oldtag": "classifier.Email.old2",  
> "newtag": "classifier.Email.new2"  
> }  
> ]  
> }

How about (lazy newer java syntax)

```auto
src = List.of(
  Map.of("oldTag", "classifier.Email.old1", "newtag", "classifier.Email.new1"),
  Map.of("oldTag", "classifier.Email.old2", "newtag", "classifier.Email.new2")
);

```

and then use this?

---

<div class="post-metadata">

**Author:** ![vitaly1233](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vitaly1233/32/80196_2.png) [@vitaly1233](https://discuss.elastic.co/u/vitaly1233)\
**Post date:** [April 7, 2022, 5:02pm UTC](https://discuss.elastic.co/t/how-convert-update-by-query-with-params-as-array-to-java-rest-client-sdk/301870/3 "2022-04-07T17:02:45Z")

</div>

Thanks for reply ,

```auto
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    			diffList.forEach(a -> {
    				queryBuilder.should(QueryBuilders.matchQuery("_es_attributes", a.getOldval()));
    			}); 

diffList is just pojo list of diffs (old vs new...) im iterating over it to build the builder.
and pass to setQuery param

    request.setQuery(queryBuilder);

The optional Params that don't works are build with map as example

map("src", ... "<here i did array to json>") // it doesn't work in java .. why ? 

I need the "src" key because the script starts with params.src.size() and it uses it ....
```

---

<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:** [May 5, 2022, 5:03pm UTC](https://discuss.elastic.co/t/how-convert-update-by-query-with-params-as-array-to-java-rest-client-sdk/301870/4 "2022-05-05T17:03:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
