有的時候我們在使用ES時,由於資源有限或業務需求,我們只想保存最近一段時間的數據,所以有如下腳本可以定時刪除數據
delete_es_by_day.sh
#!/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, "include_upper": true } } } } } } } }" echo "ok"
注解:腳本傳入參數說明:1.索引名;2.日期字段名;3.保留最近幾天數據,單位天;4.日期格式,可不輸(默認形式20160101)
【轉自】:http://blog.csdn.net/shan1369678/article/details/51352350
