ElasticSearch5.x 删除数据

以下测试在elasticsearch5.6.10版本。

首先要说明的是ElasticSearch从2.x开始就已经不支持删除一个type了,所以使用delete命令想要尝试删除一个type的时候会出现如下错误:

No handler found for uri [/dating_profile/zhenai/] and method [DELETE]

测试

假如存在一个名为dating_profile的index和zhenai的type:

curl -XDELETE http://192.168.1.102:9200/dating_profile/zhenai

执行后报错如下:

image

所以现在如果想要删除type有两种选择:

***1、***重新设置index。

***2、***删除type下的所有数据。

如果重新设置index,官方建议:

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices-delete-mapping.html

Delete Mapping
It is no longer possible to delete the mapping for a type. Instead you should delete the index and recreate it with the new mappings.

删除index

如下,删除名为dating_profile的index:

curl -XDELETE http://192.168.1.102:9200/dating_profile/

image

删除成功,返回值为:

{
 "acknowledged": true
}

删除type下的所有数据

想要一次性删除type为zhenai所有数据内容的话,可以参考官方文档:

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/docs-delete-by-query.html

其中有讲到,可以通过_delete_by_query限制到一个单独的type,如下,它仅仅会删除index为dating_profile下type为zhenai下的所有数据:

curl -X POST "http://192.168.1.102:9200/dating_profile/zhenai/_delete_by_query?conflicts=proceed" -H 'Content-Type: application/json' -d'
{
 "query": {
   "match_all": {}
 }
}'

image

删除成功,返回值如下:

{
 "took": 78,
 "timed_out": false,
 "total": 107,
 "deleted": 107,
 "batches": 1,
 "version_conflicts": 0,
 "noops": 0,
 "retries": {
   "bulk": 0,
   "search": 0
 },
 "throttled_millis": 0,
 "requests_per_second": -1.0,
 "throttled_until_millis": 0,
 "failures": []
}

也可以一次性删除多个index和多个type下的文档,如下:删除index为dating_profile下的type为zhenai的数据;同时删除index为movies下的type为movie的数据。

curl -X POST "http://192.168.1.102:9200/dating_profile,movies/zhenai,movie/_delete_by_query" -H 'Content-Type: application/json' -d'
{
 "query": {
   "match_all": {}
 }
}
'

image

返回值如下:

{
 "took": 93,
 "timed_out": false,
 "total": 61,
 "deleted": 61,
 "batches": 1,
 "version_conflicts": 0,
 "noops": 0,
 "retries": {
   "bulk": 0,
   "search": 0
 },
 "throttled_millis": 0,
 "requests_per_second": -1.0,
 "throttled_until_millis": 0,
 "failures": []
}

题外话

5.xES提供的reindex可以直接在搜索集群中对数据进行重建。如下可以直接修改mapping。

如下将index为dating_profile改为new_dating_profile

curl -XPOST "http://192.168.1.102:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
 "source": {
   "index": "dating_profile"
 },
 "dest": {
   "index": "new_dating_profile"
 }
}
'

这样执行后,旧的index还是存在的,dating_profile和new_dating_profile都可以查到旧数据。

image

ElasticSearch+ELK日志平台全套视频教程等相关学习资源可以在公众号后台回复【1】加小助手索取。



作者简洁

作者:小碗汤,一位热爱、认真写作的小伙,目前维护原创公众号:『我的小碗汤』,专注于写golang、docker、kubernetes等知识等提升硬实力的文章,期待你的关注。 转载说明:务必注明来源(注明:来源于公众号:我的小碗汤, 作者:小碗汤)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击云原生

众筹一元植发

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值