Переиндексация упругих данных для всех показателей конкретного паттерна - PullRequest
0 голосов
/ 22 мая 2018

У меня есть несколько эластичных индексов.Название индексов имеет определенный формат.Ниже приведены примеры моих индексов:

  • abc_2ab99742-94d2-43f8-a582-ce10a0f031dc;

  • abc_e8241182-1a40-410b-a95d-c883472444f4;

Теперь мне нужно переиндексировать все данные в этих индексах.Для этого я написал сценарий оболочки, который выполняет следующее:

1. Loop for all indices
  1.1 create a temporary index like abc_2ab99742-94d2-43f8-a582-ce10a0f031dc_tmp.
  1.2 reindix all the data from the original index to temp.
  1.3 delete and re-create the original index.
  1.4 reindex the data from temp to original index.
  1.5 delete the temporary index.

Ниже приведен сценарий оболочки, который я написал для того же.

#!/bin/bash

ES_HOST="localhost"
ES_PORT="9200"
TMP="_tmp"

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
for index in $indices
do
    echo "Reindex process starting for index: $index"
    tmp_index=$index${TMP}
    output=$(curl -X PUT "http://${ES_HOST}:${ES_PORT}/$tmp_index" -H 'Content-Type: application/json' -d'
    {
        "settings" : {
            "index" : {
                "number_of_shards" : 16,
                "number_of_replicas" : 1
            }
        }
    }')
    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 -X POST "http://${ES_HOST}:${ES_PORT}/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": '"$index"'
      },
      "dest": {
        "index": '"$tmp_index"'
      }
    }
    ')
    echo "Reindexing completed from original index:$index to temporary index:$tmp_index with output: $output"
    echo "Deleting $index"
    output=$(curl -X DELETE "http://${ES_HOST}:${ES_PORT}/$index")
    echo "$index deleted with status: $output"
    echo "Creating index: $index"
    output=$(curl -X PUT "http://${ES_HOST}:${ES_PORT}/$index" -H 'Content-Type: application/json' -d'
    {
        "settings" : {
            "index" : {
                "number_of_shards" : 16,
                "number_of_replicas" : 1
            }
        }
    }')
    echo "Index: $index creation status: $output"
    echo "Starting reindexing elastic data from temporary index:$tmp_index to original index:$index"
    output=$(curl -X POST "http://${ES_HOST}:${ES_PORT}/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": '"$tmp_index"'
      },
      "dest": {
        "index": '"$index"'
      }
    }
    ')
    echo "Reindexing completed from temporary index:$tmp_index to original index:$index with output: $output"
    echo "Deleting $tmp_index"
    output=$(curl -X DELETE "http://${ES_HOST}:${ES_PORT}/$tmp_index")
    echo "$tmp_index deleted with status: $output"
done

Но яполучить исключение в команде переиндексации.Вот исключение

Reindexing completed from original index:abc_58b888be-a90f-e3be-838d-88877aee572c to temporary index:abc_58b888be-a90f-e3be-838d-88877aee572c_tmp with output: {"error":{"root_cause":[{"type":"parsing_exception","reason":"[reindex] failed to parse field [source]","line":4,"col":9}],"type":"parsing_exception","reason":"[reindex] failed to parse field [source]","line":4,"col":9,"caused_by":{"type":"json_parse_exception","reason":"Unrecognized token 'abc_58b888be': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@39913ba; line: 4, column: 31]"}},"status":400}

Кто-нибудь может мне помочь, так как я не очень хорош в сценариях оболочки.

1 Ответ

0 голосов
/ 22 мая 2018

Проблема связана с вашим сценарием оболочки, проверьте следующую часть:

output=$(curl -X POST "http://${ES_HOST}:${ES_PORT}/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": '"$index"'
      },
      "dest": {
        "index": '"$tmp_index"'
      }
    }
    ')

Здесь вы должны опубликовать json, но этот json недействителен, измените его следующим образом, тогда ваш скрипт будет работать:

output=$(curl -XPOST "http://${ES_HOST}:${ES_PORT}/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": "'"$index"'"
      },
      "dest": {
        "index": "'"$tmp_index"'"
      }
    }
    ')



"index": "'"$index"'"

Это создает действительную пару ключ-значение в формате json

...