I am using Elasticsearch 7.4, and have a unique requirement to perform below operation in elasticsearch through painless script:-
#1 I can perform using painless script using below to update the counter value, but how can I get the updated value in response? I want the new value ctx._source.counter in response of the POST call I made, Is there any way?
curl -XPOST -H "Content-Type:application/json" localhost:9200/myindex/_update_by_query -d "{"query":{"bool":{"filter":[{"match":{"field1":"1"}}]}},"script":{"source":"ctx._source.counter = ctx._source.counter + params.incrementValue","lang":"painless","params":{"incrementValue":"2"}}}"
What you want is different from the specs of the _update_by_query API. In the specs you can see the response only contains stats & status of the update, and not the document body (neither initial or final).
One way to accomplish your goal is:
With this, you can get compute the updated value at your end with the method of your choice. In any case it would be two API calls not just one.