Search code examples
elasticsearchelasticsearch-queryelasticsearch-java-apiresthighlevelclient

Elastic Search update by query not reflecting correctly updated number of documents


I am running an update by query call using the java rest client: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-update-by-query.html

This is my code: (extracted only the relevant part and updated some fields in the painless script)

UpdateByQueryRequest queryRequest = new UpdateByQueryRequest(indexName);
Script script = new Script(ScriptType.INLINE, "painless", "if (ctx._source.someKey == someVal) {ctx._source.someKey = someOtherVal;}",Collections.emptyMap());
queryRequest.setScript(script);
BulkByScrollResponse bulkResponse = aesClient.updateByQuery(queryRequest, RequestOptions.DEFAULT);

The above code works fine, with the relevant documents getting updated. However Whenever I execute the following:

long updatedDocs = bulkResponse.getUpdated();

The value stored in updatedDocs doesn't reflect the actual number of documents getting updated. The value stored in it is the number of documents that it processed.

According to the documentation , it should return the number of documents updated.

Please correct me if i am wrong. Is there a fix for this?


Solution

  • You should modify your script to :

    if (ctx._source.someKey == someVal) {ctx._source.someKey = someOtherVal;} else { ctx.op='noop'}
    

    The ctx.op='noop' omits that document from its updates as specified in the update_by_query documentation

    This no operation will be reported in :

    long noops = bulkResponse.getNoops();