Conditional update to the document

@Mark_Harwood or anyone in elastic, has anything changed with ES v7.x?

I observed that the note / warning about update API and external version incompatibility has disappeared from the documentation in version 7.

https://www.elastic.co/guide/en/elasticsearch/reference/6.6/docs-update.html
has the warning.

https://www.elastic.co/guide/en/elasticsearch/reference/7.4/docs-update.html does not have any such warning.

Thanks! :pray: Just what I needed!

I extended this concept to deal with cases where you have objects within an ES document as the putAll only seems to work when you have fields in an ES document and not when you have objects within an ES document that contain fields (properties) themselves.

POST /transaction_index/_update/33384637-3137-3132-5543-31304c4c3151
{
   "script": {
      "source": "if (ctx._source.Metadata == null || params.Metadata.Version >= ctx._source.Metadata.Version) { for (k in params.keySet()){ if (ctx._source[k] != null) { ctx._source[k].putAll(params.get(k)) } else { ctx._source.put(k, params.get(k)) } } } else { ctx.op = 'none' }",
      "params": {
         "Transaction": {
            "TransactionId": "33384637-3137-3132-5543-31304c4c3151",
            "TransactionKey": "Key1"
         },
         "Message": {
            "MessageId": "505a5953-374a-385a-4a48-52353549445a",
            "Context": "This is a test context"
         },
         "MessageDefinition": {
            "MessageDefinitionId": "a1c05e06-fa6b-40ce-992f-d083ff6c0243",
            "Code": 1010101010
         },
         "Metadata": {
            "Version": 1,
            "CreateTime": "2020-09-04T14:27:51.1986439+01:00",
            "IsLatest": true,
            "IsDummy": false,
            "VersionString": "20200903111111"
         }
    }
 },
 "scripted_upsert": true,
 "upsert": {}
}

Also posted this on Stackoverflow

1 Like