How to delete all data from an index using delete_

2019-07-23 18:07发布

问题:

I have upgraded my elasticsearch to 2.0 version in my rails 4.2.7 app and am trying to delete all index data using delete-by-query plugin. How can i achieve this?
i was using delete_by_query method like this

Elasticsearch::Model.client
                    .delete_by_query(index: index_klass.index_name,
                                     body: {query: {match_all: {}}})

But this is deprecated in ES 2.X versions. so how can i use plugin to do this. thanks in advance.

回答1:

Instead of deleting all the data in an index, I strongly suggest deleting the entire index and re-creating it.

Deleting the documents will mark them as deleted, but not actually physically deleting them from disk. When the automatic merging process of Elasticsearch happens then those will actually be deleted. But, the merging process will consider certain segment files for deletions only and most certainly you will still have disk space being used by an index which has no documents. More about segments' merging: https://www.elastic.co/guide/en/elasticsearch/guide/current/merge-process.html and more about the deletion of these documents at merge time - https://www.elastic.co/blog/lucenes-handling-of-deleted-documents

So, you should much much better by simply deleting the index and recreating it.



回答2:

You have two solutions:

  1. upgrade to ES 5.0 which has brought the delete-by-query API back into the core.
  2. stay on 2.x and install the delete-by-query plugin

Thereafter, you code will work again.