# Update only if exist

**URL:** https://discuss.elastic.co/t/update-only-if-exist/138059
**Category:** Elasticsearch
**Created:** [June 30, 2018, 11:27am UTC](https://discuss.elastic.co/t/update-only-if-exist/138059 "2018-06-30T11:27:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![panda2004](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/panda2004/32/19473_2.png) [@panda2004](https://discuss.elastic.co/u/panda2004)
#### Post date: [June 30, 2018, 11:27am UTC](https://discuss.elastic.co/t/update-only-if-exist/138059/1 "2018-06-30T11:27:49Z")

</div>

Hey,

Some of the events sent to my ElasticSearch cluster aren't relevant in case they are related to a document which doesn't exist.

I know I could just make a regular update request, and then get exception about failing to update a missing document. But, I don't believe it's a good practice to throw many exceptions in ElasticSearch neither in my application - as of performance concerns.

So, I thought about using the ctx.op = 'noop' instead.  
Remark: timestamp is a field that always exists in any of my existed documents.  
Is this going to work?

```
POST myindex/mytype/1/_update
{
	"scripted_upsert":true,
    "script" : {
        "source": "if (ctx._source.timestamp != null) { ctx._source.metadata = params.metadata} else { ctx.op = 'none' }",
        "lang": "painless",		
        "params" : {
            "metadata" : "hello"
        }
    },
	"upsert" : {}
}
```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 30, 2018, 1:03pm UTC](https://discuss.elastic.co/t/update-only-if-exist/138059/2 "2018-06-30T13:03:42Z")

</div>

Would that help? [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/docs-index\_.html#operation-type](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/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: [July 28, 2018, 1:03pm UTC](https://discuss.elastic.co/t/update-only-if-exist/138059/3 "2018-07-28T13:03:42Z")

</div>

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