Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch : How to delete an Index using python

Forgive me if this is quite basic but I have Python 2.7 and Elasticsearch 2.1.1 and I am just trying to delete an index using

es.delete(index='researchtest', doc_type='test') 

but this gives me

return func(*args, params=params, **kwargs) TypeError: delete() takes at least 4 arguments (4 given) 

I also tried

es.delete_by_query(index='researchtest', doc_type='test',body='{"query":{"match_all":{}}}') 

but I get

AttributeError: 'Elasticsearch' object has no attribute 'delete_by_query' 

Any idea why? Has the api changed for 2.1.1 for python?

https://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.client.IndicesClient.delete

like image 215
AbtPst Avatar asked Feb 01 '16 15:02

AbtPst


People also ask

How do I delete a specific index in Elasticsearch?

To delete the index, you must roll over the data stream so a new write index is created. You can then use the delete index API to delete the previous write index.

How do I delete all index Elasticsearch files?

2. Delete all documents from the index. We can delete all the documents from the index using _delete_by_query.


2 Answers

From the docs, use this notation:

from elasticsearch import Elasticsearch es = Elasticsearch()  es.indices.delete(index='test-index', ignore=[400, 404]) 
like image 168
ferdy Avatar answered Oct 05 '22 06:10

ferdy


If you have a document object (model) and you're using elasticsearch-dsl, specially with Python-3.X you can directly call the delete method of your model's _index attribute.

ClassName._index.delete() 

Also as it's stated in documentation:

The _index attribute is also home to the load_mappings method which will update the mapping on the Index from elasticsearch. This is very useful if you use dynamic mappings and want the class to be aware of those fields (for example if you wish the Date fields to be properly (de)serialized):

Post._index.load_mappings() 
like image 20
Mazdak Avatar answered Oct 05 '22 04:10

Mazdak