python elasticsearch delete_如何从Elasticsearch中删除文档

由于几个原因,已从版本2中的ES核心中删除了Delete By Query API。这个函数变成了一个插件。您可以在此处查找更多详细信息:

因为我不想添加另一个依赖项(因为我以后需要在docker映像中运行这个),所以我编写了一个自己的函数来解决这个问题。我的解决方案是搜索具有指定索引和类型的所有引号。之后,我使用Bulk API删除它们:def delete_es_type(es, index, type_):

try:

count = es.count(index, type_)['count']

response = es.search(

index=index,

filter_path=["hits.hits._id"],

body={"size": count, "query": {"filtered" : {"filter" : {

"type" : {"value": type_ }}}}})

ids = [x["_id"] for x in response["hits"]["hits"]]

if len(ids) > 0:

return

bulk_body = [

'{{"delete": {{"_index": "{}", "_type": "{}", "_id": "{}"}}}}'

.format(index, type_, x) for x in ids]

es.bulk('\n'.join(bulk_body))

# es.indices.flush_synced([index])

except elasticsearch.exceptions.TransportError as ex:

print("Elasticsearch error: " + ex.error)

raise ex

我希望这能帮助未来的谷歌用户;)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值