#!bash
# 接口用法
# sh export-es-data.sh 索引名称 css密码 cssIP
index=$1
password=$2
ip=$3
value=$(curl -XPOST -u admin:$2 -k "https://${ip}:9200/${index}/_doc/_search?scroll=1m" -H 'Content-Type: application/json' -d '
{
"size": 10000,
"query": {
"match_all": {}
},
"sort": [
"_doc"
]
}')
scroll_id=$(echo $value | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~"_scroll_id"){print $(i+1)} }}' | tr -d '"' | sed -n 1p)
source=$(echo $value | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~"_source"){print $(i+1)} }}' | tr -d '"' | sed -n 1p)
while [ -n "$source" ]; do
echo $value >> es_csv_${index}.tsv
value=$(curl -XPOST -u admin:$2 -k "https://${ip}:9200/_search/scroll" -H 'Content-Type: application/json' -d '{"scroll": "1m","scroll_id": "'${scroll_id}'"}')
scroll_id=$(echo $value | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~"_scroll_id"){print $(i+1)} }}' | tr -d '"' | sed -n 1p)
source=$(echo $value | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~"_source"){print $(i+1)} }}' | tr -d '"' | sed -n 1p)
done