gpt4 book ai didi

elasticsearch - 在批量索引期间检测更改

转载 作者:行者123 更新时间:2023-12-02 22:18:48 27 4
gpt4 key购买 nike

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

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

最佳答案

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

当批量查询返回时,您可以遍历每个更新结果并检查 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
}
]
}

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

关于elasticsearch - 在批量索引期间检测更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53396315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com