Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch how to support transaction involving multiple documents

i use elasticsearch,and Denormalizing Data,like

PUT /my_index/user/1
{
"name": "John Smith",
"email": "[email protected]",
"dob": "1970/10/24"
}

PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"user": {
"id": 1,
"name": "John Smith"
}
}

but the problem is that Elasticsearch does not support ACID transactions. Changes to individual documents are ACIDic, but not changes involving multiple documents.if i want to change /my_index/user/1 and /my_index/blogpost/2 user name at one transaction,if one error it will rollback, how to do that?

like image 759
王奕然 Avatar asked Jun 28 '18 01:06

王奕然


1 Answers

There are no transactions in ES and never will according to inside sources.

The best way to achieve what you want is to make your updates in bulk and then check the response of each individual responses.

POST _bulk
{"index": {"_index": "my_index", "_type": "user", "_id": "1"}}
{ "name": "John Smith", "email": "[email protected]", "dob": "1970/10/24" }
{"index": {"_index": "my_index", "_type": "blogpost", "_id": "2"}}
{ "title": "Relationships", "body": "It's complicated...", "user": { "id": 1, "name": "John Smith" }}

When your client gets the responses, it should check the items array and make sure that each item status is 200 (updated) or 201 (created). If that's the case, your bulk "transaction" was properly committed, if not, then everything with status 200 or 201 was committed otherwise the commit failed.

like image 127
Val Avatar answered Oct 02 '22 22:10

Val