javaelasticsearch

deleting all documents with out dropping index in elasticsearch java API


Is there any simple Java API to delete all the documents from elastic search with out dropping the index.

I know that we could get all the ids and delete each document one by one

DeleteResponse response = _client.prepareDelete(INDEX, TYPE, id)
            .setRefresh(true)
            .execute()
            .actionGet();

But I was looking for TRUNCATE kind of scenario.

At present I am deleting the index and recreating the mapping in unit tests.


Solution

  • You can use the delete-by-query plugin in order to achieve that.

    You need to install it on all nodes with

    sudo bin/plugin install delete-by-query
    

    Then you can add this dependency to your pom.xml

    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>delete-by-query</artifactId>
        <version>2.2.0</version>
    </dependency>
    

    And finally you'll be able to use the DeleteByQueryRequestBuilder in order to delete all your documents after your tests.