📌  相关文章
📜  用于重新索引 elasticsearch 的 shell 脚本 - Shell-Bash 代码示例

📅  最后修改于: 2022-03-11 14:50:11.641000             🧑  作者: Mango

代码示例1
#!/bin/bash

#ES_HOST="localhost"
#ES_PORT="9200"
TMP="_v2"
echo "****  Script Execution Started ****"
echo " "
echo "**** Fetching List of Indices Elasticsearch Reindexing ****"
curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc' 

echo " "
sleep 5

indices_list=$(curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc' | awk '{print $3}' | sed -n '1!p')
#indices=$(curl -s "http://${ES_HOST}:${ES_PORT}/_cat/indices/abc_*?h=index" | egrep 'abc_[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{8}*')

# do for all abc elastic indices
count=0
echo "$indices_list"
for index in $indices_list
do
    echo " "
    echo "**** Index No: $count ****"
    echo "**** Present Index we are iterating:  ${index} ****"
    count=`expr $count + 1`
    echo " "
    echo " "
    echo "Reindex process starting for index: $index"
    tmp_index=$index${TMP}
    output=$(curl -s -X PUT "http://localhost:9200/$tmp_index" -H 'Content-Type: application/json' -d'
    {
        "settings" : {
            "index" : {
                "number_of_shards" : 5,
                "number_of_replicas" : 1
            }
        }
    }')
    echo " "
    echo "Temporary index: $tmp_index created with output: $output"
    echo "Starting reindexing elastic data from original index: $index to temporary index: $tmp_index"
    output=$(curl -s -X POST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": "'$index'"
      },
      "dest": {
        "index": "'$tmp_index'"
      }
    }
    ')
    echo " "
    echo "Reindexing completed from original index: $index to temporary index: $tmp_index with output: $output"
    echo " "
    echo "Deleting $index"
    output=$(curl -s -X DELETE "http://localhost:9200/$index")
    echo "$index deleted with status: $output"
    echo " "
    echo "Creating index: $index"
    output=$(curl -s -X PUT "http://localhost:9200/$index" -H 'Content-Type: application/json' -d'
    {
        "settings" : {
            "index" : {
                "number_of_shards" : 5,
                "number_of_replicas" : 1
            }
        }
    }')
    echo " "
    echo "Index: $index creation status: $output"
    echo " "
    echo "Starting reindexing elastic data from temporary index: $tmp_index to original index: $index"
    output=$(curl -s -X POST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": "'$tmp_index'"
      },
      "dest": {
        "index": "'$index'"
      }
    }
    ')
    echo " "
    echo "Reindexing completed from temporary index:$tmp_index to original index:$index with output: $output"
    echo " "
    echo "Deleting $tmp_index"
    output=$(curl -s -X DELETE "http://localhost:9200/$tmp_index")
    echo "$tmp_index deleted with status: $output"
    echo " "
done

echo " "
sleep 5
echo "**** Fetching List of Indices After Elasticsearch Reindexing ****"
curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc' 
echo " "
sleep 5
echo " "
echo "**** Original indices list ****"
echo "$indices_list"
echo "**** No. of indices in original list: $count ****"
echo " "
count1=0
MIndices_list=$(curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc' | awk '{print $3}' | sed -n '1!p')
echo " "
echo "**** Modified indices list ****"
echo "$MIndices_list"
for j in $MIndices_list
do
     count1=`expr $count1 + 1`
done
echo " "
echo "**** No. of indices in modified list: $count1 ****"
echo "****  Script Execution Ended ****"