Conditional update to the document

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:

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

2 Likes