随着es的数据越来越多,占用磁盘容量越大,需要扩容,要花money就越多,实际上可以定期删除一些时间久远不常用的数据
可以在服务器上定期执行以下脚本
#!/bin/sh
# example: sh ./delete_es_by_day.sh chat create_time 365
index_name=$1
daycolumn=$2
savedays=$3
format_day=$4
if [ ! -n "$savedays" ]; then
echo "the args is not right,please input again...."
exit 1
fi
if [ ! -n "$format_day" ]; then
format_day='%Y-%m-%dT00:00:00'
fi
#获取x日之前的时间
savedayago=`date -d "-${savedays} day" +"${format_day}"`
echo "开始删除索引${index_name} 的 ${daycolumn} <= ${savedayago} 的数据"
curl -H "Content-Type: application/json" -XPOST "192.168.31.205:9200/${index_name}/_delete_by_query?refresh&slices=10&scroll_size=10000" -d '
{"query": {
"bool": {
"must": [
{"range": {
"'${daycolumn}'": {
"lt": "'${savedayago}'"
}
}}
]
}
}
}'
echo "ok"
如果出问题了,可以取消任务:
查询当前任务
GET _tasks?detailed=true&actions=*/delete/byquery
取消任务
POST _tasks/${任务id}/_cancel
补充:
删除数据的时候,会发现磁盘占用量反增不减,因为不是立马删除数据的,而是会在.del文件上追加被删除的记录,只有有等到下一次段合并的时候才真正被物理删除,这个时候磁盘空间才会释放。
参考:
https://blog.csdn.net/weixin_42348946/article/details/100172478
https://cloud.tencent.com/developer/article/1737025