如何在python 2.7中执行此CURL以从Elasticsearch删除文档? [英] How to execute this CURL in python 2.7 to delete documents from Elasticsearch?

查看:131
本文介绍了如何在python 2.7中执行此CURL以从Elasticsearch删除文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



使用此命令,我能够删除



是否可以使用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删除:
documentaion此处: http://elasticsearch-py.readthedocs.io/en/master/helpers.html#elasticsearch.helpers.parallel_bulk

 来自elasticsearch导入Elasticsearch 
来自elasticsearch导入助手

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
}


表示成功,信息在helpers.parallel_bulk(client = es,actions = generat e_actions(ids),thread_count = 4):
如果未成功:
print('Doc failed',info)


Hello I am new to python and elasticsearch. On my local I have setup Elasticsearch and have added data to it. http://127.0.0.1:9200/index_data/type_data.

I want to delete some _ids from the type_data. suppose the list of _ids are x= ['a','b','c'.'d'] that i want to delete.

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

using this command I was able to delete a particular _id from elasticsearch but how do execute this curl request using python?

Is it possible to delete the entire type_data using python?

why is this code not working?

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)

i am using Elasticsearch version 6.1.0. elasticsearch-py version 5.4.0

Please Help me!

解决方案

If there are a lot of ids, try parallel_bulk deletion in python: documentaion here: 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)

这篇关于如何在python 2.7中执行此CURL以从Elasticsearch删除文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆