# Update document with Rest High Level Client

**URL:** <https://discuss.elastic.co/t/update-document-with-rest-high-level-client/248714>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 15, 2020, 4:19pm UTC](https://discuss.elastic.co/t/update-document-with-rest-high-level-client/248714 "2020-09-15T16:19:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Velzi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuel_velzi/32/66975_2.png) [@Emanuel\_Velzi](https://discuss.elastic.co/u/Emanuel_Velzi)\
**Post date:** [September 15, 2020, 4:19pm UTC](https://discuss.elastic.co/t/update-document-with-rest-high-level-client/248714/1 "2020-09-15T16:19:42Z")

</div>

Hi, I have to **update** a document in my Java app, using the **Rest High Level Client**.  
I must to override the document, i.e.: I need to **replace** completely the document with a new version.

I am trying this but it only makes a partial update:

```auto
client.update(new UpdateRequest(index, id)
                .doc(jsonDocument, XContentType.JSON)
                .docAsUpsert(true), RequestOptions.DEFAULT);

```

How can I achieve this with de rest client?

---

<div class="post-metadata">

**Author:** ![Emanuil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuil/32/36783_2.png) [@Emanuil](https://discuss.elastic.co/u/Emanuil)\
**Post date:** [September 16, 2020, 1:06am UTC](https://discuss.elastic.co/t/update-document-with-rest-high-level-client/248714/2 "2020-09-16T01:06:11Z")

</div>

I'm not a Java dev but looking at [the high level Java REST client docs for Updating](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update.html), they say:

> The Update API allows to update an existing document by using a script or by passing a partial document.

That's because the client matches Elasticsearch's own [Update API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html):

> Enables you to script document updates. The script can update, delete, or skip modifying the document. The update API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API.

OK, as per the last sentence, to replace a doc (i.e. `PUT /your_index/_doc/yourdocID`) you need to use the high-level REST client's [Index API](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html) instead of Update.

You might think of indexing as only creating docs, but it's also replacing them. You can even control this via an [optional argument](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html#_optional_arguments). Look for `request.opType`. But you don't need to change this. The default value allows creating _or_ replacing a doc which works for you.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Velzi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuel_velzi/32/66975_2.png) [@Emanuel\_Velzi](https://discuss.elastic.co/u/Emanuel_Velzi)\
**Post date:** [September 16, 2020, 4:19pm UTC](https://discuss.elastic.co/t/update-document-with-rest-high-level-client/248714/3 "2020-09-16T16:19:13Z")

</div>

You are right.. Thanks!

---

<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:** [October 14, 2020, 4:19pm UTC](https://discuss.elastic.co/t/update-document-with-rest-high-level-client/248714/4 "2020-10-14T16:19:38Z")

</div>

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