Delete a document in OpenSearch via Java client

Learn how to delete a document in OpenSearch in the specified index with the given id via the Java client.

We can simply delete a document by creating a lambda function and passing the index and id to it.

client.delete(b -> b.index("sample-index").id("1"));

Or alternatively, we can create a DeleteRequest that will delete the document.

DeleteRequest deleteRequest = new DeleteRequest.Builder().index("sample-index").id("b99EcYUBz_c_efRw7hss").build();

DeleteResponse deleteResponse = openSearchClient.delete(deleteRequest);

Result response = deleteResponse.result();

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

With this method, we can create multiple delete requests and pass them at once.