您的位置:首页 > 其它

ElasticSearch之定时删除数据

2016-11-02 16:28 232 查看
原链接
http://blog.csdn.net/shan1369678/article/details/51352350
有的时候我们在使用ES时,由于资源有限或业务需求,我们只想保存最近一段时间的数据,所以有如下脚本可以定时删除数据

delete_es_by_day.sh

[plain]
view plain
copy

#!/bin/sh  
# example: sh  delete_es_by_day.sh logstash-kettle-log logsdate 30  
  
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%d'  
fi  
  
sevendayago=`date -d "-${savedays} day " +${format_day}`  
  
curl -XDELETE "10.130.3.102:9200/${index_name}/_query?pretty" -d "  
{  
        "query": {  
                "filtered": {  
                        "filter": {  
                                "bool": {  
                                        "must": {  
                                                "range": {  
                                                        "${daycolumn}": {  
                                                                "from": null,  
                                                                "to": ${sevendayago},  
                                                                "include_lower": true,  
                                                                
4000
"include_upper": true  
                                                        }  
                                                }  
                                        }  
                                }  
                        }  
                }  
        }  
}"  
  
echo "ok"  

注解:脚本传入参数说明:1.索引名;2.日期字段名;3.保留最近几天数据,单位天;4.日期格式,可不输(默认形式20160101)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: