【问题标题】:Elastic Search update by query not reflecting correctly updated number of documents查询的弹性搜索更新未反映正确更新的文档数量
【发布时间】:2020-03-31 09:10:11
【问题描述】:

我正在使用 java rest 客户端通过查询调用运行更新: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-update-by-query.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 中存储的值不反映正在更新的文档的实际数量。存储在其中的值是它处理的文档数。

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

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

【问题讨论】:

    标签: elasticsearch elasticsearch-query elasticsearch-java-api resthighlevelclient


    【解决方案1】:

    您应该将脚本修改为:

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

    ctx.op='noop' 从其更新中省略了该文档,如 update_by_query documentation 中指定的那样

    此无操作会报:

    long noops = bulkResponse.getNoops(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多