How to erase ElasticSearch index? How to erase ElasticSearch index? elasticsearch elasticsearch

How to erase ElasticSearch index?


Found solution:

IndicesAdminClient adminClient = searchConnection.client.admin().indices();String indexName = "location";DeleteIndexResponse delete = adminClient.delete(new DeleteIndexRequest(indexName)).actionGet()if (!delete.isAcknowledged()) {    log.error("Index {} wasn't deleted", indexName);}

and

client.admin().indices().flush(new FlushRequest('location')).actionGet();

after putting new data into index.


First of all you don't have to clear all data by issuing a delete on each doc id. You can just delete all data with delete by query matching all documents http://www.elasticsearch.org/guide/reference/api/delete-by-query.html Having that said I don't recommend that either, because it's not recommended to do this often on large doc collections (see docs).

What you really want to do is delete the whole index (it's fast) http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index.html , recreate it, put in data and this is important refresh the index to "commit" the changes and make them visible. http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html

I do this in my tests and never had a problem.


  1. it is not the async call (you can add a listener and avoid actionGet to get the async call)
  2. delete all items via:

    client.prepareDeleteByQuery(indexName).            setQuery(QueryBuilders.matchAllQuery()).            setTypes(indexType).            execute().actionGet();
  3. refresh your index to see the changes (only required in unit tests)