Skip to main content
 首页 » 编程设计

python之如何在 python 2.7 中执行此 CURL 以从 Elasticsearch 中删除文档

2025年01月19日8over140

你好,我是 python 和 elasticsearch 的新手。在我的本地我已经设置了 Elasticsearch 并向它添加了数据。 http://127.0.0.1:9200/index_data/type_data .

我想从 type_data 中删除一些 _id。 假设 _ids 列表是 x= ['a','b','c'.'d'] 我想删除。

curl -XDELETE 'localhost:9200/index_data/type_data/a?pretty'

使用这个命令我能够从 elasticsearch 中删除一个特定的 _id 但如何使用 python 执行这个 curl 请求?

是否可以使用 python 删除整个 type_data?

为什么这段代码不起作用?

from elasticsearch import Elasticsearch  
es = Elasticsearch() 
request_body = { 
    "query": { 
        "ids": { 
            "values": ['a','b','c','d','e','f'] 
        } 
    } 
} 
es.delete_by_query(index=es_index, body=request_body) 

我正在使用 Elasticsearch 版本 6.1.0。 elasticsearch-py 版本 5.4.0

请帮助我!

请您参考如下方法:

如果id很多,试试python中的parallel_bulk删除: 此处的文档:http://elasticsearch-py.readthedocs.io/en/master/helpers.html#elasticsearch.helpers.parallel_bulk

from elasticsearch import Elasticsearch 
from elasticsearch import helpers 
 
es = Elasticsearch() 
index_name = es_index 
doc_type = your_doc_type 
ids = ['a','b','c','d','e','f'] 
 
 
def generate_actions(ids): 
    for i in ids: 
        yield { 
            '_op_type': 'delete', 
            '_index': index_name, 
            '_type': doc_type, 
            '_id': i 
        } 
 
 
for success, info in helpers.parallel_bulk(client=es, actions=generate_actions(ids), thread_count=4): 
    if not success:  
        print('Doc failed', info)