2 thread upsert with auto generated id

Lets assume my BL creates documents with auto generated id.

Now when i insert a new doc i first check if it exists ( upsert) by searching on some parameter inside the documents.

If i cant find it, insert, if i can find it update.

Now what happens if i have 2 threads that simultaneously doing the upsert, the first will not find the doc, and also the second.... now both will insert a new doc with a different id since its autogenerated, when i wanted the second thread to update...

Is there any solution for this scenario? or am i forced to use the elastic _id (meaning ill put my own doc value as an id) for upserts so the default version validation of elastic will get to work?
if the _id will be my own parameter, the second insert will fail as the lucene insert is atomic? so the second insert will wait with its insert since the first insert operation is atmoic, and when the second thread tries to insert he will fail since the id already exists?

Maybe you should take a look at the Update API instead, which also offers upsert functionality?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.