elasticsearch7环境下清理索引数据方法

数据库磁盘空间不够,需要对索引进行清理,如果是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 `find /opt/elk/sp_apps_active_mapping_bak -type f -name "*.json"`;do
/usr/local/node/bin/elasticdump --input=${file_index} --output=http://elastic:pass@172.16.2.130:9200 --type=mapping
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值