簡體   English   中英

從Python 3.x中的elasticsearch索引中刪除所有文檔

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

如何在不刪除索引本身的情況下從索引中刪除Elasticsearch中的所有文檔?

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

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

響應

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

在SQL中是否有像截斷表這樣的選項。 我知道我可以循環所有id並刪除每個id,但也許例如通配符就有一些魔術選項。

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": {}}})

elasticsearch.Elasticsearch.delete方法不是靜態方法,應該使用elasticache.Elasticsearch的實例進行調用,這樣,在調用self會自動作為參數傳遞給delete方法。

例:

from elasticsearch import Elasticsearch

es = Elasticsearch()

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

其次,由彈性搜索文檔描述在這里 ,“您可以使用刪除從索引中刪除一個文件,你必須指定索引名和文件ID。” 因此,您應同時提供兩者。

例:

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

參考: 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