简体   繁体   中英

delete all documents from elasticsearch index in Python 3.x

How can I delete all documents in Elasticsearch from index without deleting index itself?

from elasticsearch import Elasticsearch
es = Elasticsearch("http://elasticsearch.internal:9200")

Elasticsearch.delete(es, index="index")

response

TypeError: delete() missing 1 required positional argument: 'id'

Is there option like truncate table in sql. I know that I can loop all ids and delete each of them but maybe there is some magic option with wildcard for example.

Elasticsearch.delete_by_query method can be used for deleting documents matching a query.

https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.delete_by_query

indices = ['index1', 'index2', 'other-index-names']
es.delete_by_query(index=indices, body={"query": {"match_all": {}}})

The elasticsearch.Elasticsearch.delete method is not a static method and should be called using an instance of elasticache.Elasticsearch , and this way self will automatically be passed as an argument to the delete method when it is called.

Example:

from elasticsearch import Elasticsearch

es = Elasticsearch()

Reference: https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch

Secondly, as described by the elastic search documentation here , "You use DELETE to remove a document from an index. You must specify the index name and document ID." Therefore, you should provide it both.

Example:

doc_id = "my_document"
my_index = "index"
es.delete(id=doc_id, index=my_index)

Reference: https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.delete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM