Upsert data using painless script if updated_at is higher than previous document

I have a collection which gets updated frequently.

  1. We need to perform upsert operation, i.e., if document doesn't exist then create else update.
  2. Another constraint we require is timestamp of updated_at should be higher.

Please help. The query will be something like this. But I am getting error even after multiple try.

POST /test/_update/1
  "scripted_upsert": true,
  "script": {
    "source": """
      if ( ctx.op == 'create' ) {
        ctx._source.counter = params.count
      } else {
        ctx._source.counter += params.count
    "params": {
      "updated_at": "2022-01-17T21:54:29.074Z"
  "upsert": {}


Is it a whole query? It looks there are no "count" field in "params".
What does the error message show?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.