简体   繁体   English

从Python 3.x中的elasticsearch索引中删除所有文档

[英]delete all documents from elasticsearch index in Python 3.x

How can I delete all documents in Elasticsearch from index without deleting index itself? 如何在不删除索引本身的情况下从索引中删除Elasticsearch中的所有文档?

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. 在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. 我知道我可以循环所有id并删除每个id,但也许例如通配符就有一些魔术选项。

Elasticsearch.delete_by_query method can be used for deleting documents matching a query. Elasticsearch.delete_by_query方法可用于删除与查询匹配的文档。

https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.delete_by_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. elasticsearch.Elasticsearch.delete方法不是静态方法,应该使用elasticache.Elasticsearch的实例进行调用,这样,在调用self会自动作为参数传递给delete方法。

Example: 例:

from elasticsearch import Elasticsearch

es = Elasticsearch()

Reference: https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch 参考: 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." 其次,由弹性搜索文档描述在这里 ,“您可以使用删除从索引中删除一个文件,你必须指定索引名和文件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 参考: https : //elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.delete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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