數據庫磁盤空間不夠,需要對索引進行清理,如果是mysql直接truncate tablename 就行了
看了一下網上的主流做法,一種是刪除索引重建索引,其次是刪除數據
一、直接刪除所有數據(類似mysql的delete方法,基本是刪不動,放棄)
POST quality_control/my_type/_delete_by_query?refresh&slices=5&pretty { "query": { "match_all": {} } }
二、刪除索引,然后重建索引(推薦)
拿一個索引做測試:
1.備份需要清理的索引mapping數據結構
# 備份索引mapping數據結構
# bakmapping.sh
#!/bin/bash
mappinglist=`curl -u elastic:pass -sXGET http://172.16.2.130:9200/_cat/indices?v|awk '{print $3}' |egrep "^sp_apps_active"`
for i in ${mappinglist};do
/usr/local/node/bin/elasticdump --ignore-errors=true --scrollTime=120m --bulk=true --input=http://elastic:pass@172.16.2.130:9200/${i} --output=/opt/elk/sp_apps_active_mapping_bak/${i}.mapping.json --type=mapping
done
2.刪除我們需要清理的索引
# curl -u elastic:pass -XDELETE "http://172.16.2.130:9200/sp_apps_active_16"
{"acknowledged":true}
3.再次導入索引的mapping結構
/usr/local/node/bin/elasticdump --input=/opt/elk/sp_apps_active_mapping_bak/sp_apps_active_16.mapping.json --output=http://elastic:pass@172.16.2.130:9200 --type=mapping
# 查看結構是否導入成功
curl -u elastic:pass -sXGET http://172.16.2.130:9200/_cat/indices?v|egrep "sp_apps_active_16"
# 查看結構
curl -u elastic:pass http://172.16.2.130:9200/sp_apps_active_16/_mapping?pretty
4.腳本批量化處理
# more mapping_clean.sh
#!/bin/bash
need_del_indexes=`curl -u elastic:pass -sXGET http://172.16.2.130:9200/_cat/indices?v|awk '{print $3}'|egrep "^sp_apps_active"`
# 批量刪除索引
for i in ${need_del_indexes};do
echo "curl -u elastic:pass -XDELETE http://172.16.2.130:9200/${i}"
done
# 根據之前的備份,批量恢復索引
for file_index in `ls /opt/elk/sp_apps_active_mapping_bak "*.json"`;do
/usr/local/node/bin/elasticdump --input=${file_index} --output=http://elastic:pass@172.16.2.130:9200/file_index --type=mapping
done
