首页 文章

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

提问于
浏览
2

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

我想从type_data中删除一些_ids . 假设我要删除的_id列表是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

请帮我!

1 回答

  • 1

    如果有很多id,请尝试在python中删除parallel_bulk:documentaion: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)
    

相关问题