ElasticSearch5.x 删除数据

如下测试在elasticsearch5.6.10版本。html

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

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

测试

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

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

执行后报错以下:python

image

因此如今若是想要删除type有两种选择: 面试

一、从新设置index。 spring

二、删除type下的全部数据。shell

若是从新设置index,官方建议:编程

https://www.elastic.co/guide/...json

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:segmentfault

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

image

删除成功,返回值为:

{
 "acknowledged": true
}

删除type下的全部数据

想要一次性删除type为zhenai全部数据内容的话,能够参考官方文档:

https://www.elastic.co/guide/...

其中有讲到,能够经过_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】加小助手索取。



本公众号免费提供csdn下载服务,海量IT学习资源,若是你准备入IT坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java、go、python、springcloud、elk、嵌入式 、大数据、面试资料、前端 等资源。同时咱们组建了一个技术交流群,里面有不少大佬,会不定时分享技术文章,若是你想来一块儿学习提升,能够公众号后台回复【2】,免费邀请加技术交流群互相学习提升,会不按期分享编程IT相关资源。


扫码关注,精彩内容第一时间推给你

image

相关文章
相关标签/搜索