# Upsert / Update if exist, create if not

**URL:** <https://discuss.elastic.co/t/upsert-update-if-exist-create-if-not/194681>\
**Category:** Logstash\
**Created:** [August 9, 2019, 10:36pm UTC](https://discuss.elastic.co/t/upsert-update-if-exist-create-if-not/194681 "2019-08-09T22:36:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ELK-Developer](https://avatars.discourse-cdn.com/v4/letter/e/7993a0/32.png) [@ELK-Developer](https://discuss.elastic.co/u/ELK-Developer)\
**Post date:** [August 9, 2019, 10:36pm UTC](https://discuss.elastic.co/t/upsert-update-if-exist-create-if-not/194681/1 "2019-08-09T22:36:23Z")

</div>

Hello,

how do I use upsert, to create or update entries = documents via the output-plugin to elasticsearch, dependent on the document with the given id is already in elasticsearch or even not.

My data will come from two different logstash-instances A and B and it will not be clear if A comes before B or vice versa. The document-id is clear and known and should be as it is, but all other attributes may be different (disjunct) and should be added to the document.  
I tried out with a simple doc\_as\_upsert statement on B with action = update. This will only work, if B comes after A.

What might be a solution of that ?

Thank you

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 9, 2019, 11:34pm UTC](https://discuss.elastic.co/t/upsert-update-if-exist-create-if-not/194681/2 "2019-08-09T23:34:03Z")

</div>

The whole point of doc\_as\_upsert is that if the document does not exist it will get created. You can use doc\_as\_upsert on both. If A sends the document first it will insert and B will update. If B sends the document first then B will insert and A will update.

If they both send it at the same time then I would trust Elastic to have locking in place to resolve the race so that one is an insert and the other an update.

---

<div class="post-metadata">

**Author:** ![ELK-Developer](https://avatars.discourse-cdn.com/v4/letter/e/7993a0/32.png) [@ELK-Developer](https://discuss.elastic.co/u/ELK-Developer)\
**Post date:** [August 10, 2019, 6:59am UTC](https://discuss.elastic.co/t/upsert-update-if-exist-create-if-not/194681/3 "2019-08-10T06:59:33Z")

</div>

Thank you for your reply.  
Your answer is that what i did expect from doc\_as\_upsert.

However in my case it does not work, maybe it is about action=\>update, so that limits the doing just to update. I will try out without action =\> update.

This is how it looks now for Logstash-Instance A and B. The output is kinda same

...

```
output{
elasticsearch{
hosts => ["localhost:1234:9200"]
index => "customer"
document_type => "testdata"
document_id = "%{costomer_id}%{invoice_nr}"  
doc_as_upsert => true
action => "update" 
manage_template => true
}
```

---

<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:** [September 7, 2019, 6:59am UTC](https://discuss.elastic.co/t/upsert-update-if-exist-create-if-not/194681/4 "2019-09-07T06:59:50Z")

</div>

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