# Conditional update to the document

**URL:** <https://discuss.elastic.co/t/conditional-update-to-the-document/64964>\
**Category:** Elasticsearch\
**Created:** [November 4, 2016, 2:06am UTC](https://discuss.elastic.co/t/conditional-update-to-the-document/64964 "2016-11-04T02:06:45Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)\
**Post date:** [October 16, 2019, 6:53am UTC](https://discuss.elastic.co/t/conditional-update-to-the-document/64964/21 "2019-10-16T06:53:27Z")

</div>

@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](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](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/docs-update.html) does not have any such warning.

---

<div class="post-metadata">

**Author:** ![Callonski](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Callonski](https://discuss.elastic.co/u/Callonski)\
**Post date:** [July 17, 2020, 12:54pm UTC](https://discuss.elastic.co/t/conditional-update-to-the-document/64964/22 "2020-07-17T12:54:56Z")

</div>

Thanks! 🙏 Just what I needed!

---

<div class="post-metadata">

**Author:** ![Kwex](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@Kwex](https://discuss.elastic.co/u/Kwex)\
**Post date:** [September 7, 2020, 11:56am UTC](https://discuss.elastic.co/t/conditional-update-to-the-document/64964/23 "2020-09-07T11:56:25Z")

</div>

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.

```auto
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

> <https://stackoverflow.com/questions/50743317/elasticsearch-update-the-whole-source-field-based-on-search-query/63744751#63744751>

---

<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:** [November 4, 2022, 4:31am UTC](https://discuss.elastic.co/t/conditional-update-to-the-document/64964/24 "2022-11-04T04:31:34Z")

</div>



[Previous page](https://discuss.elastic.co/t/conditional-update-to-the-document/64964.md?page=1)
