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 `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

原文地址:https://www.cnblogs.com/reblue520/p/13533544.html