elasticsearch - 在批量索引期间检测更改
问题描述
我们正在为我们的数据库使用 Elasticsearch v5.6.12。我们经常使用批量 REST api 更新它。有时单个请求不会改变任何东西(即 Elasticsearch 已经是最新的文档的值)。如何检测这些实例?
我看到了这个(https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html),但我不确定它是否适用于我们的情况。
解决方案
您可以在检查批量查询的结果时使用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
推荐阅读
- spring-security - 是否可以使用自定义的 ServerAuthenticationEntryPoint 添加响应正文?
- php - PHP preg_match 不接受换行符
- visual-studio-code - 如何杀死 Ubuntu 主机上的 VSCode 远程服务
- python - XML 换行符实体和 Python 编码
- apache - 如何在 Apache 中使用代理引擎重定向到映射到其他主机的其他子域?
- r - 使用双 for 循环创建新变量的函数
- c++ - 如何使用 WinRT 创建经典的 win32 应用程序窗口?
- ios - 如何使用 swift 链接实现 GPUImage 过滤器?
- r - 箱线图的倾斜 x 轴标签
- python - 无法通过传递环境变量从 .NET 客户端运行 docker 容器