# Upsert With Script

**URL:** https://discuss.elastic.co/t/upsert-with-script/175631
**Category:** Elasticsearch
**Created:** [April 5, 2019, 4:12pm UTC](https://discuss.elastic.co/t/upsert-with-script/175631 "2019-04-05T16:12:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Anurag\_Mankar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurag_mankar/32/42607_2.png) [@Anurag\_Mankar](https://discuss.elastic.co/u/Anurag_Mankar)
#### Post date: [April 5, 2019, 4:12pm UTC](https://discuss.elastic.co/t/upsert-with-script/175631/1 "2019-04-05T16:12:49Z")

</div>

I am trying to write a query to update or insert a document. The query is given below.

POST /abc/\_doc/108/\_update  
{  
"script" : {  
"source": """  
// some process is done here  
""",  
"lang": "painless",  
"params" : {

```
      **"_source": {**

```

\*\* "cus": "abcd2"\*\*  
\*\* }\*\*  
}  
},  
"scripted\_upsert": true,  
"upsert": {  
**"\_source": {**  
\*\* "cus": "abcd2"\*\*  
\*\* }\*\*  
}  
}

I am using the same payload in upsert and inside params  
is there a way to use the same payload without writing it twice

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [April 6, 2019, 9:10am UTC](https://discuss.elastic.co/t/upsert-with-script/175631/2 "2019-04-06T09:10:57Z")

</div>

If you set `scripted_upsert` to `true` (as you're doing already), then you can pass in an empty `upsert` clause, [as shown in the documentation here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_literal_scripted_upsert_literal). The script is then responsible for initializing the document, and you only have to pass in the values inside `params`.

---

<div class="post-metadata">

### Author: ![Anurag\_Mankar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurag_mankar/32/42607_2.png) [@Anurag\_Mankar](https://discuss.elastic.co/u/Anurag_Mankar)
#### Post date: [April 8, 2019, 2:23pm UTC](https://discuss.elastic.co/t/upsert-with-script/175631/3 "2019-04-08T14:23:41Z")

</div>

thank You

---

<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: [May 6, 2019, 2:23pm UTC](https://discuss.elastic.co/t/upsert-with-script/175631/4 "2019-05-06T14:23:45Z")

</div>

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