forked from Pindar/docker-es-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch-optimize-index.sh
36 lines (31 loc) · 1.02 KB
/
elasticsearch-optimize-index.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/sh
# COPY of http://pastebin.com/zC9HU2pU
# Do elasticsearch optimize on logstash previous day index
# if $1 = all then optimize all indicies
ELASTICSEARCH=${ELASTICSEARCH:-"http://localhost:9200"}
GREP="logstash"
esindex=`curl -s "$ELASTICSEARCH/_status?pretty=true" | grep $GREP | grep -v \"index\" | sort -r | awk -F\" {'print $2'}`
# Grab yesterday's values
D=`date +%d -d yesterday`
M=`date +%m -d yesterday`
Y=`date +%Y -d yesterday`
today="`date +%Y`.`date +%m`.`date +%d`"
yesterday="$Y.$M.$D"
# If $1 = all
if [ "x$1" = "xall" ]
then
# Loop through all ES indicies except today
for index in $esindex
do
if [[ `echo $index | grep $today` ]]; then
# skip today
continue;
fi
# Run through all the indicies and optimize them
echo "Optimizing $index"
curl -XPOST "$ELASTICSEARCH/$index/_optimize?max_num_segments=2"
done
else
echo "Optimizing index logstash-$yesterday"
curl -XPOST "$ELASTICSEARCH/logstash-$yesterday/_optimize?max_num_segments=2"
fi