# 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:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![timothytong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timothytong/32/24168_2.png) [@timothytong](https://discuss.elastic.co/u/timothytong)\
**Post date:** [November 14, 2017, 12:17am UTC](https://discuss.elastic.co/t/conditional-update-to-the-document/64964/18 "2017-11-14T00:17:49Z")

</div>

A clean way to do this without enumerating the entire map would be wrapping the entire map inside a wrapper object (e.g. called "document") then replace the original with `ctx._source = params.document`. A full example:

```auto
POST /name/type/1/_update
{
   "scripted_upsert": true,
   "script" : {
     "inline": "if (ctx._source.timestamp >= params.timestamp) { ctx.op = 'none' } else { ctx._source = params.document }", 
     "lang": "painless",
     "params": {
        "document": {
          "timestamp": 112255
        }
     }
   },
   "upsert": {
      "timestamp": 33333
   }
}

```

Hope this helps

---

_[View the full topic](https://discuss.elastic.co/t/conditional-update-to-the-document/64964)._
