首页 > 解决方案 > 在批量索引期间检测更改

问题描述

我们正在为我们的数据库使用 Elasticsearch v5.6.12。我们经常使用批量 REST api 更新它。有时单个请求不会改变任何东西(即 Elasticsearch 已经是最新的文档的值)。如何检测这些实例?

我看到了这个(https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html),但我不确定它是否适用于我们的情况。

标签: elasticsearch

解决方案


您可以在检查批量查询的结果时使用noop检测。

当批量查询返回时,您可以遍历每个更新结果并检查该result字段的值是否为noop(vs updated)

# Say the document is indexed
PUT test/doc/1
{
  "test": "123"
}

# Now you want to bulk update it
POST test/doc/_bulk
{"update":{"_id": "1"}}
{"doc":{"test":"123"}}        <-- this will yield `result: noop`
{"update":{"_id": "1"}}
{"doc":{"test":"1234"}}       <-- this will yield `result: updated`
{"update":{"_id": "2"}}
{"doc":{"test":"3456"}, "doc_as_upsert": true}       <-- this will yield `result: created`

结果:

{
  "took" : 6,
  "errors" : false,
  "items" : [
    {
      "update" : {
        "_index" : "test",
        "_type" : "doc",
        "_id" : "1",
        "_version" : 2,
        "result" : "noop",            <-- see "noop"
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "test",
        "_type" : "doc",
        "_id" : "1",
        "_version" : 3,
        "result" : "updated",            <-- see "updated"
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "_index" : "test",
      "_type" : "doc",
      "_id" : "2",
      "_version" : 1,
      "result" : "created",            <-- see "created"
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }
  ]
}

如您所见,当doc_as_upsert: true为 id 为 2 的文档指定时,将创建文档并且result字段值为created


推荐阅读