How convert update_by_query with params as array to Java Rest client SDK

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

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

    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

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)

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?

Thanks for reply ,

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 ....

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