DecKno
DecKno

Reputation: 323

Delete Document with empty document id - elasticsearch TCP client

I am using TCP client to perform delete operation. sample code:

DeleteRequestBuilder builder = client.prepareDelete(indexName, indexType,indexDocumentId);

ListenableActionFuture<DeleteResponse> deleteResponse =   builder.setOperationThreaded(false).execute();

deleteResponse.actionGet(ESTemplateHelper.INDEX_STATE_ACK_TIMEOUT);
deleteStatus = deleteResponse.isDone();

I am passing empty value/"" to indexDocumentId.

deleteStatus is always true for empty documentId. But document is not deleted. am i missing something? Isn't it expected to throw any error?

Upvotes: 0

Views: 1047

Answers (1)

Ashish Goel
Ashish Goel

Reputation: 919

The prepareDelete command is for deleting a single document by its ID. For more information: https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/delete.html

Now, the ID of a document cannot be empty string. So, there should be no such document. The reason deleteStatus is true because it holds the value "whether the request is done or not?" and not "was the document deleted?". If you drill down the response, I believe you will find: found = false.

In case, you are passing an empty string in the hope of deleting all the documents of type indexType in the index indexName, then prepareDelete is not the right API for that.

Maybe, you can execute a query on all documents in your type, and delete them one by one. There is also delete by query API but it has been deprecated in 1.5 and removed in 2.0 because it can potentially cause OOM errors. More details here: https://www.elastic.co/guide/en/elasticsearch/reference/1.6/docs-delete-by-query.html

In case, you don't care about this index altogether, then deleting the index is the quickest and cleanest way to go: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-delete-index.html. I believe in the similar fashion you can delete your type too. Eg: curl -XDELETE http://localhost:9200/indexName/indexType

Upvotes: 1

Related Questions