# Upsert a document when document id is autogenerated using upsert?

**URL:** <https://discuss.elastic.co/t/upsert-a-document-when-document-id-is-autogenerated-using-upsert/348747>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 6, 2023, 4:52pm UTC](https://discuss.elastic.co/t/upsert-a-document-when-document-id-is-autogenerated-using-upsert/348747 "2023-12-06T16:52:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iby\_dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iby_dev/32/127095_2.png) [@iby\_dev](https://discuss.elastic.co/u/iby_dev)\
**Post date:** [December 6, 2023, 4:52pm UTC](https://discuss.elastic.co/t/upsert-a-document-when-document-id-is-autogenerated-using-upsert/348747/1 "2023-12-06T16:52:10Z")

</div>

According to the docs, on the [Update API](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docs-update.html)

The `<_id>` field is required.

I have a bulk insert process which is sometimes known to insert duplicates given a particular scenario.

The ids for us, are auto generated so how does one use the Update API with Upsert functionality give this limitation in our set-up ?

Do we start generating the ids upfront and perform a simple calculation or does the API have a deterministic approach where the document can be identified using a different unique value ?

My hope was to use the DocumentPath class but under the covers, this too uses an id value.

I am using the nest sdk with c# (dotnet core 7).

Thanks for reading.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 6, 2023, 5:44pm UTC](https://discuss.elastic.co/t/upsert-a-document-when-document-id-is-autogenerated-using-upsert/348747/2 "2023-12-06T17:44:02Z")

</div>

> [@iby\_dev](#):
>
> does the API have a deterministic approach where the document can be identified using a different unique value ?

No it does not. You need to supply the correct index name and document id.

> [@iby\_dev](#):
>
> The ids for us, are auto generated so how does one use the Update API with Upsert functionality give this limitation in our set-up ?

If [you can change the generated id so it is deterministic based on the data in the event](https://www.elastic.co/blog/efficient-duplicate-prevention-for-event-based-data-in-elasticsearch), that may be a way to go. If you can not do this you will generally need to search to see if the document exists before inserting it, which adds a lot of overhead.

---

<div class="post-metadata">

**Author:** ![iby\_dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iby_dev/32/127095_2.png) [@iby\_dev](https://discuss.elastic.co/u/iby_dev)\
**Post date:** [December 7, 2023, 9:46am UTC](https://discuss.elastic.co/t/upsert-a-document-when-document-id-is-autogenerated-using-upsert/348747/3 "2023-12-07T09:46:31Z")

</div>

Ah that figures - thank you for responding.

---

<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:** [January 4, 2024, 9:47am UTC](https://discuss.elastic.co/t/upsert-a-document-when-document-id-is-autogenerated-using-upsert/348747/4 "2024-01-04T09:47:17Z")

</div>

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