# Bulk index if not exists

**URL:** <https://discuss.elastic.co/t/bulk-index-if-not-exists/247912>\
**Category:** Elasticsearch\
**Created:** [September 8, 2020, 4:15pm UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912 "2020-09-08T16:15:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![noorul.m](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noorul.m/32/46539_2.png) [@noorul.m](https://discuss.elastic.co/u/noorul.m)\
**Post date:** [September 8, 2020, 4:15pm UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912/1 "2020-09-08T16:15:48Z")

</div>

I have the following document structure  
`{ "id": "977373", "name": "Air Odisha" }`

I want to create documents in bulk only if id and name are not present already.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [September 8, 2020, 11:54pm UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912/2 "2020-09-08T23:54:04Z")

</div>

Welcome to our community! 😃

The closest thing would be [an upsert](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/docs-update.html#upserts).

---

<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:** [September 9, 2020, 4:38am UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912/3 "2020-09-09T04:38:06Z")

</div>

You can specify the action in the bulk request to be `create` instead of `index`. This will create a new document and fail if the document already exists. There is an example [in the docs](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/docs-bulk.html).

---

<div class="post-metadata">

**Author:** ![noorul.m](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noorul.m/32/46539_2.png) [@noorul.m](https://discuss.elastic.co/u/noorul.m)\
**Post date:** [September 9, 2020, 9:18am UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912/4 "2020-09-09T09:18:01Z")

</div>

That upsert is based on \_id of the document but I need to check the matching document based on two keys(id, name) of the document. So it's like I have to create thousands of documents in the ES and the condition is to create the documents only when there is no matching(id & name) in the ES for all the document to be inserted.

---

<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:** [September 9, 2020, 9:45am UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912/5 "2020-09-09T09:45:52Z")

</div>

The you need to create the document id based on the two fields you mentioned.

---

<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:** [October 7, 2020, 9:46am UTC](https://discuss.elastic.co/t/bulk-index-if-not-exists/247912/6 "2020-10-07T09:46:04Z")

</div>

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