Update only if exist

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" : {}
}

Would that help? https://www.elastic.co/guide/en/elasticsearch/reference/6.3/docs-index_.html#operation-type

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