#!/bin/bash # This script reads the aliases of all ML results # indices and generates the body of the request # needed to recreate those aliases (POST _aliases). # The script helps with restoring ML results indices # after reindexing them to a temporary place and # back in order to ensure the correct mappings are in place. # # Usage: # With CURL: # curl --user:es_user http://host:port/_aliases | ./gen_post_aliases_body.sh # With the response of GET .ml-anomalies-*/_alias stored in a file: # ./gen_post_aliases_body.sh < file_containing_response.txt GET_ALIASES_RESPONSE=$(cat) INDICES=$(echo $GET_ALIASES_RESPONSE | jq keys) ACTIONS=() for ((i=0;i<$(echo $INDICES | jq length);i++)); do INDEX=$(echo $INDICES | jq ".[$i]") INDEX_ALIASES=$(echo $GET_ALIASES_RESPONSE | jq '.['"$INDEX"']') ALIASES=$(echo $INDEX_ALIASES | jq '.aliases | keys') for ((j=0;j<$(echo $ALIASES | jq length);j++)); do ACTION="" ALIAS=$(echo $ALIASES | jq ".[$j]") FILTER=$(echo $INDEX_ALIASES | jq '.["aliases"]['"$ALIAS"']["filter"]') ACTION="add$i$j" ACTION='{"add": {"index":'"$INDEX"',"alias":'"$ALIAS" if [ "$FILTER" != "null" ]; then ACTION="$ACTION"',"filter":'"$FILTER" fi ACTION="$ACTION}}" ACTIONS+=("$ACTION") done done echo '{"actions":[' ( IFS=$','; echo "${ACTIONS[*]}" ) echo ']}'