我使用弹性搜索和非规范化数据,比如
PUT /my_index/user/1
{
"name": "John Smith",
"email": "john@smith.com",
"dob": "1970/10/24"
}
PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"user": {
"id": 1,
"name": "John Smith"
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是 Elasticsearch 不支持 ACID 事务。对单个文档的更改是 ACIDic,但不是涉及多个文档的更改。如果我想在一次事务中更改 /my_index/user/1 和 /my_index/blogpost/2 用户名,如果出现错误将回滚,该怎么做?
ES 中没有交易,而且根据内部消息来源永远不会。
实现您想要的最佳方法是批量更新,然后检查每个响应的响应。
POST _bulk
{"index": {"_index": "my_index", "_type": "user", "_id": "1"}}
{ "name": "John Smith", "email": "john@smith.com", "dob": "1970/10/24" }
{"index": {"_index": "my_index", "_type": "blogpost", "_id": "2"}}
{ "title": "Relationships", "body": "It's complicated...", "user": { "id": 1, "name": "John Smith" }}
Run Code Online (Sandbox Code Playgroud)
当您的客户端收到响应时,它应该检查items数组并确保每个项目status是 200(更新)或 201(创建)。如果是这种情况,则您的批量“事务”已正确提交,如果没有,则提交状态为 200 或 201 的所有内容,否则提交失败。
| 归档时间: |
|
| 查看次数: |
7149 次 |
| 最近记录: |