# Prevent checking existence of document ID when specified

**URL:** https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175
**Category:** Elasticsearch
**Created:** [December 3, 2018, 12:40pm UTC](https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175 "2018-12-03T12:40:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![iamredlus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iamredlus/32/15504_2.png) [@iamredlus](https://discuss.elastic.co/u/iamredlus)
#### Post date: [December 3, 2018, 12:40pm UTC](https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175/1 "2018-12-03T12:40:14Z")

</div>

Hey

It is [well documented](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/tune-for-indexing-speed.html#_use_auto_generated_ids) that when specifying an ID of a document upon ingestion, elasticsearch will first check whether that ID is already in use, and only then approve the new document for indexing. This comes with a big performance price tag. We have given up setting our own ID for performance issues, but this makes our codebase much more complex as documents need to be searched instead of directly accessed.

Why is this check necessary?  
We are very sure of the uniqueness of the ID we are providing and would expect a collision to be automatically interpreted as an update action.  
I would like to suggest such a flag to disable checking the ID of documents before indexing.

p.s.  
If I compare this behavior with mapping, for example, elasticsearch does not check whether the mapping of the document I am about to index actually matches the expected one in the index. Instead, it tries to index the document, and in case of a mapping collision the document is rejected and an exception is thrown.

Thanks  
Lior

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [December 3, 2018, 12:56pm UTC](https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175/2 "2018-12-03T12:56:05Z")

</div>

> [@iamredlus](#):
>
> Why is this check necessary?

It would be a pretty catastrophic error if we trusted a client to provide unique IDs without elasticsearch checking them first and it turned out the client had provided us with duplicates. The behaviour of the system at that point would be "interesting".  
Relinquishing that fundamental check would be like asking a database to not reject records that violated a primary key constraint. The performance benefits are not to be disputed but that's the sort of feature we'd call a foot gun.

> [@iamredlus](#):
>
> documents need to be searched instead of directly accessed.

You can use routing to target reads and writes to the appropriate shard.

---

<div class="post-metadata">

### Author: ![iamredlus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iamredlus/32/15504_2.png) [@iamredlus](https://discuss.elastic.co/u/iamredlus)
#### Post date: [December 3, 2018, 1:03pm UTC](https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175/3 "2018-12-03T13:03:51Z")

</div>

@Mark_Harwood thanks for the quick answer.  
Can't the behavior fallback duplicates to an update? This is what I would expect as the API suggest (indexing is exposed as an upsert).  
What would the behavior of the system be if such a fallback isn't possible and the aforementioned violation happens?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [December 3, 2018, 1:10pm UTC](https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175/4 "2018-12-03T13:10:18Z")

</div>

> [@iamredlus](#):
>
> Can't the behavior fallback duplicates to an update?

It already does. Elasticsearch attempts a read on the provided ID to know whether an update or insert is the appropriate action - which is where the noted performance cost comes in.  
Autogen IDs are always assumed inserts and therefore skip a read.

> [@iamredlus](#):
>
> What would the behavior of the system be if such a fallback isn't possible and the aforementioned violation happens?

It's like Ghostbusters and "crossing the streams" - we should just assume bad things will happen.

---

<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: [December 31, 2018, 1:10pm UTC](https://discuss.elastic.co/t/prevent-checking-existence-of-document-id-when-specified/159175/5 "2018-12-31T13:10:27Z")

</div>

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