#!/bin/bash
# 存放文件路径
FILE="/home/dpc/temp_es_data.txt"
# ES访问地址
HOST="10.11.11.11:10003"
# ES index
INDEX="v2-ime-statistics-20210817"
# ES type
TYPE="_doc"
#scroll数据
function scroll(){
search_dsl='{
"query": {
"match_all": {}
},
"size": 10
}
'
json=$(curl -s -XGET "$HOST/$INDEX/$TYPE/_search?scroll=1m" -H "Content-Type: application/json" -d "$search_dsl")
scroll_id=$(echo $json | jq -c -r "._scroll_id")
hits=$(echo $json | jq -c ".hits.hits")
length=$(echo $hits | jq length)
echo $length
for i in $(seq $length)
do
index=$i-1
hit=$(echo $hits | jq -c ".[$index]._source")
echo $hit >> $FILE
done
scroll_dsl='{"scroll_id": "'"$scroll_id"'"}'
num=100
counter=1
while [ $counter -lt $num ];
do
hits=$(curl -s -XGET "$HOST/_search/scroll?scroll=1m" -H "Content-Type: application/json" -d "$scroll_dsl" | jq -c ".hits.hits")
if [ "$hits" == "" ];
then
echo "exit"
exit
fi
length=$(echo $hits | jq length)
for i in $(seq $length)
do
index=$i-1
hit=$(echo $hits | jq -c ".[$index]._source")
echo $hit >> $FILE
done
echo "counter $counter"
echo `date +%Y/%m/%d_%k:%M:%S`
let counter+=1
done
}
if [ -f $FILE ]; then
rm $FILE
fi
touch "$FILE"
scroll