在批量索引编制过程中检测更改

时间:2018-11-20 15:28:58

标签: elasticsearch

我们正在为数据库使用Elasticsearch v5.6.12。我们使用批量REST API经常更新此内容。在某些情况下,个别请求不会更改任何内容(即Elasticsearch已经更新的文档的值)。 如何检测到这些实例?

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

1 个答案:

答案 0 :(得分:3)

在检查批量查询的结果时,可以使用noop detection

批量查询返回时,您可以遍历每个更新结果,并检查result字段的值是否为noop(与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
    }
  ]
}

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