Update document in OpenSearch via Java client

Learn how to update documents in a specified index in OpenSearch via Java client.

OpenSearch provides UpdateRequest class using which we can create an update request and then pass it to the client to make an update to the given Id in the index.

Index newDoc = new Index("Prashant", "Yadav", 27);

UpdateRequest updateRequest = new UpdateRequest.Builder().id("b99EcYUBz_c_efRw7hss").index("sample-index").doc(newDoc).build();

UpdateResponse updateResponse = openSearchClient.update(updateRequest, Index.class);

Object response = updateResponse.result();

System.out.println(response.equals(Result.Updated));

After the update, the client returns a UpdateResponse in which we can validate the result if the update has happened or not.