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

标签 python python-2.7 curl elasticsearch

你好,我是 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)

关于python - 如何在 python 2.7 中执行此 CURL 以从 Elasticsearch 中删除文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48078887/

相关文章:

python - 在 Python 中逐行打印输出到 GUI

python - 似乎无法丢失此错误 : "You are trying to add a non-nullable field"

Python pandas - 平均 10 分钟测量到 15 分钟平均值和 60 分钟平均值,具体取决于数据差距的长度

python - 在引发 tkinter 框架之前,不会处理 tkinter 键盘中断

Python复杂字典排序

http - 访问url时如何验证中间服务器是否可读用户和密码部分

python - 在内存中存储一​​个大的稀疏矩阵来计算特征值

python - 无法将数组数据从 dtype ('O' ) 转换为 dtype ('float64' )

bash - Google App Scripts curl 授权

linux - 使用 curl 的 bash 脚本在 apache solr 中索引数据