# Index document only if document does not exist

**URL:** <https://discuss.elastic.co/t/index-document-only-if-document-does-not-exist/67385>\
**Category:** Elasticsearch\
**Created:** [November 28, 2016, 5:57pm UTC](https://discuss.elastic.co/t/index-document-only-if-document-does-not-exist/67385 "2016-11-28T17:57:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EuclideanSearch](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@EuclideanSearch](https://discuss.elastic.co/u/EuclideanSearch)\
**Post date:** [November 28, 2016, 5:57pm UTC](https://discuss.elastic.co/t/index-document-only-if-document-does-not-exist/67385/1 "2016-11-28T17:57:23Z")

</div>

Hi,

Is there a way to index a document only if the document does not already exist? For instance, if I have a setup where I have configured the UUID (a field in my document) to be the document identifier, is there a flag (or some other mechanism) that if I perform two inserts then only one insert will succeed (and perhaps the other will throw, ideally soemthing like 'document already exists exception')?

Cheers,  
EuclideanSearch

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 28, 2016, 8:14pm UTC](https://discuss.elastic.co/t/index-document-only-if-document-does-not-exist/67385/2 "2016-11-28T20:14:53Z")

</div>

Yes, look into the op\_type option.

> The index operation also accepts an op\_type that can be used to force a create operation, allowing for "put-if-absent" behavior. When create is used, the index operation will fail if a document by that id already exists in the index.

> **[Index API | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type)**

---

<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 26, 2016, 8:14pm UTC](https://discuss.elastic.co/t/index-document-only-if-document-does-not-exist/67385/3 "2016-12-26T20:14:53Z")

</div>

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