# Update /Upsert with document AND script

**URL:** https://discuss.elastic.co/t/update-upsert-with-document-and-script/32095
**Category:** Elasticsearch
**Created:** [October 13, 2015, 2:30pm UTC](https://discuss.elastic.co/t/update-upsert-with-document-and-script/32095 "2015-10-13T14:30:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ramdev\_Wudali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramdev_wudali/32/7894_2.png) [@Ramdev\_Wudali](https://discuss.elastic.co/u/Ramdev_Wudali)
#### Post date: [October 13, 2015, 2:30pm UTC](https://discuss.elastic.co/t/update-upsert-with-document-and-script/32095/1 "2015-10-13T14:30:54Z")

</div>

Hi:  
The problem I have is as follows:  
Index may or may not have the document, If the document does not exist, index(INSERT) the document, Else,  
Update the existing document using an INLINE script. (the script takes a value from the new document and adds it to the existing document)

However, the Update/Upsert API will not allow for me to have a script and a DOC at the same time. Can some one provide suggestions on how I might be able to achieve it ? (using the Java API) and ES 1.5.2 with Java 7

Thanks  
Ramdev

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [October 13, 2015, 2:53pm UTC](https://discuss.elastic.co/t/update-upsert-with-document-and-script/32095/2 "2015-10-13T14:53:58Z")

</div>

Have a look at the upsert documentation here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#upserts](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#upserts)

You can specify the `script` together with the `upsert` option and this should achieve what you are looking for.

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [October 13, 2015, 2:54pm UTC](https://discuss.elastic.co/t/update-upsert-with-document-and-script/32095/3 "2015-10-13T14:54:59Z")

</div>

In the Java API you would be looking for the `setUpsert()` method on `UpdateRequestBuilder`

---

<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: [July 5, 2017, 9:59pm UTC](https://discuss.elastic.co/t/update-upsert-with-document-and-script/32095/5 "2017-07-05T21:59:27Z")

</div>


