zoukankan      html  css  js  c++  java
  • 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】加小助手索取。



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


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

    image

  • 相关阅读:
    linux下shell显示-bash-4.1#不显示路径解决方法
    update chnroute
    An error "Host key verification failed" when you connect to other computer by OSX SSH
    使用dig查询dns解析
    DNS被污染后
    TunnelBroker for EdgeRouter 后记
    mdadm详细使用手册
    关于尼康黄的原因
    Panda3d code in github
    Python实例浅谈之三Python与C/C++相互调用
  • 原文地址:https://www.cnblogs.com/liabio/p/11696089.html
Copyright © 2011-2022 走看看