0

我正在使用 java rest 客户端通过查询调用运行更新: https ://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-update-by-查询.html

这是我的代码:(仅提取相关部分并更新了无痛脚本中的一些字段)

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);

上面的代码工作正常,相关文档得到更新。但是,每当我执行以下操作时:

long updatedDocs = bulkResponse.getUpdated();

存储的值updatedDocs不反映更新文档的实际数量。存储在其中的值是它处理的文档数。

根据文档,它应该返回更新的文档数。

如果我错了,请纠正我。有解决办法吗?

4

1 回答 1

1

您应该将脚本修改为:

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

ctx.op='noop' 从update_by_query 文档中指定的更新中省略了该文档

此无操作将在中报告:

long noops = bulkResponse.getNoops(); 
于 2020-04-01T09:00:14.057 回答