3

I'm approaching ElastichSearch with a Java client. I'm trying to delete an entire index. I'm able to delete a single document with the following code:

DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();

I would like to delete all documents for a given index in a single instruction. Note that I'm using the version 2.2.

Thanks

EDIT: I've found a similar question but it refers to an old API version. I'm currently working with version 2.2.

3

2 Answers 2

8

The easiest way would be to delete your index, then recreate it.

DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest("your-index")).actionGet()

then

client.admin().indices().prepareCreate("your-index").get();

This will work with the 2.2 api

Sign up to request clarification or add additional context in comments.

1 Comment

Do you have any idea on how same could be achieved with Java High Level Rest Client. This works fine with transport client but now it is deprecated by elastic search.
0

Same solution as in maximede's answer but using newer version of RestHighLevelClient (non depreciated):

// delete current index
var deleteRequest = new DeleteIndexRequest("index-name");
client.indices().delete(deleteRequest, RequestOptions.DEFAULT);

// create new one
CreateIndexRequest request = new CreateIndexRequest("index-name");
client.indices().create(request, RequestOptions.DEFAULT);

Please make sure to import from the right package:

org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest
org.elasticsearch.client.indices.CreateIndexRequest;

Works fine with micronaut framework io.micronaut.elasticsearch:micronaut-elasticsearch:4.0.0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.