# Upsert data using painless script if updated\_at is higher than previous document

**URL:** https://discuss.elastic.co/t/upsert-data-using-painless-script-if-updated-at-is-higher-than-previous-document/294632
**Category:** Elasticsearch
**Tags:** painless, eql-elastic-query-language
**Created:** [January 17, 2022, 9:51pm UTC](https://discuss.elastic.co/t/upsert-data-using-painless-script-if-updated-at-is-higher-than-previous-document/294632 "2022-01-17T21:51:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![garotad508](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@garotad508](https://discuss.elastic.co/u/garotad508)
#### Post date: [January 17, 2022, 9:51pm UTC](https://discuss.elastic.co/t/upsert-data-using-painless-script-if-updated-at-is-higher-than-previous-document/294632/1 "2022-01-17T21:51:31Z")

</div>

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.

````auto
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": {}
}```
````

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 18, 2022, 4:35pm UTC](https://discuss.elastic.co/t/upsert-data-using-painless-script-if-updated-at-is-higher-than-previous-document/294632/2 "2022-01-18T16:35:28Z")

</div>

Hi,

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

---

<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: [February 15, 2022, 4:35pm UTC](https://discuss.elastic.co/t/upsert-data-using-painless-script-if-updated-at-is-higher-than-previous-document/294632/3 "2022-02-15T16:35:42Z")

</div>

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