user3351014
user3351014

Reputation: 185

Elasticsearch delete/update a document in index1 and index2

If I have two index, Ex: sample1 and sample2. If I delete or update a value in sample1 then the corresponding document should also deleted or updated in sample2?

Data : sample1 : {name: 'Tom', id: '1', city: 'xx', state, 'yy', country: 'zz'} sample2 : {id: '1', city: 'xx', state, 'yy', country: 'zz'} If I delete id: '1' then this document should be deleted from both the index from the server side itself. How to do this ? The problem will be if I delete the values separatley then if I end up in network issue after deleting value from one index the other index will have values how to avoid this ?

Upvotes: 1

Views: 1068

Answers (4)

Val
Val

Reputation: 217274

You can use the bulk API for doing this and you'll have better guarantees that both delete/update operations succeed or fail since everything happens in a single network call:

For deleting both documents in two different indices:

POST _bulk
{"delete": {"_index": "index1", "_type": "type1", "_id": "1"}}
{"delete": {"_index": "index2", "_type": "type2", "_id": "1"}}

For updating both documents in two different indices:

POST _bulk
{"index": {"_index": "index1", "_type": "type1", "_id": "1"}}
{"name": "Tom", id: "1", "city": "xx", "state": "yy", "country": "zz"}
{"index": {"_index": "index2", "_type": "type2", "_id": "1"}}
{"id": "1", "city": "xx", "state": "yy", "country": "zz"}

UPDATE

After discussing this, it seemed the needed solution was a mix of using

Upvotes: 3

Jettro Coenradie
Jettro Coenradie

Reputation: 4733

There is no clean way to do this with elasticsearch. What you want/need feels like a transaction and that is not possible with elastic. What you could do is do a bulk request with the 2 queries to update/delete the item in there. Still you have to check the response of the bulk to see if both queries went well. The chances for one of them failing might be a little bit smaller.

Upvotes: 1

Andrei Stefan
Andrei Stefan

Reputation: 52368

Elasticsearch cannot guarantee you that it will do those two operations atomically, like a transaction in RDBs. I suggest looking at nested documents or parent/child relationships for what Elasticsearch understands by joined documents.

In this way, if you deleted the parent, the children will be deleted as well.

Upvotes: 0

Kulasangar
Kulasangar

Reputation: 9434

I don't think you can do both at the same time, I mean deleting the same document in two different indexes.

But then deleting a document from an index could be done using the Delete By Query API by giving a matchin query so that the appropriate document could be deleted.

Source: Delete By Query API

Upvotes: 0

Related Questions