Update document with Rest High Level Client

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:

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

How can I achieve this with de rest client?

I'm not a Java dev but looking at the high level Java REST client docs for Updating, 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:

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 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. 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.

1 Like

You are right.. Thanks!

1 Like