# 2 thread upsert with auto generated id

**URL:** <https://discuss.elastic.co/t/2-thread-upsert-with-auto-generated-id/214765>\
**Category:** Elasticsearch\
**Created:** [January 13, 2020, 6:32am UTC](https://discuss.elastic.co/t/2-thread-upsert-with-auto-generated-id/214765 "2020-01-13T06:32:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharongur](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@sharongur](https://discuss.elastic.co/u/sharongur)\
**Post date:** [January 13, 2020, 6:32am UTC](https://discuss.elastic.co/t/2-thread-upsert-with-auto-generated-id/214765/1 "2020-01-13T06:32:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 13, 2020, 1:34pm UTC](https://discuss.elastic.co/t/2-thread-upsert-with-auto-generated-id/214765/2 "2020-01-13T13:34:28Z")

</div>

Maybe you should take a look at the [Update API](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html) instead, which also offers upsert functionality?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 10, 2020, 1:34pm UTC](https://discuss.elastic.co/t/2-thread-upsert-with-auto-generated-id/214765/3 "2020-02-10T13:34:30Z")

</div>

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