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