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

相關文章
相關標籤/搜索