'Is there a way to conditionally upsert in Elastic search in a single atomic transaction?

Upsert should only occur if the value of a particular field in the document that I'm attempting to upsert (the index) is greater than the one that is currently there. This has to be accomplished in a single atomic transaction as there are concurrent lambdas that are going to do this . That is, I can't query first and then upsert second in order to handle the condition myself because in between operations, the data will likely already become dirty.

Based on few readings- we can use scripts to do this, but that seems to be a costly operation.

POST <<endpoint>>
{
  "scripted_upsert":true,
  "script": {
    "lang": "painless",
    "inline" : "if //some_condition//",
    "params": {
    //params
    }
  }
}

Is there a better way ?



Solution 1:[1]

if you used bulk for upsert, When your client gets the responses, it should check the items array and make sure that each item status is 200 (updated) or 201 (created). If that's the case, your bulk "transaction" was properly committed, if not, then everything with status 200 or 201 was committed otherwise the commit failed. also we know that elastic not have rollback or commit function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rasoul Harouni