guneysus
5/31/2017 - 1:06 PM

alias_create.json

{
    "actions" : [
        { "add" : { "index" : "${INDEX}", "alias" : "${ALIAS}" } }
    ]
}
{
    "actions" : [
        { "remove" : { "index" : "${INDEX}", "alias" : "${ALIAS}" } }
    ]
}
{
    "actions" : [
        { "remove" : { "index" : "${OLD_INDEX}", "alias" : "${ALIAS}" } },
        { "add" : { "index" : "${NEW_INDEX}", "alias" : "${ALIAS}" } }
    ]
}
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
#/bin/bash

export CONNECTION=$1
mkdir -p tmp
rm tmp/*

function index_create {
    INDEX=$1
    RESULT=$(curl -H "Content-Type: application/json" -X PUT --data @data/index_create.json ${CONNECTION}/${INDEX}?pretty -sL -w "%{http_code}\\n" -o /dev/null)
    echo "$RESULT Created Index: $INDEX"
}

function drop_index {
    INDEX=$1
    RESULT=$(curl -X DELETE ${CONNECTION}/${INDEX}?pretty -sL -w "%{http_code}\\n" -o /dev/null)
    echo "$RESULT Dropped Index: $INDEX"
}

function create_alias {
    INDEX=$1    
    ALIAS="${INDEX}-$(date +%Y.%m.%d)"
    export INDEX ALIAS
    cat <(envsubst < data/alias_create.json) > tmp/alias_create.json
    RESULT=$(curl -H "Content-Type: application/json" -X POST --data @tmp/alias_create.json ${CONNECTION}/_aliases?pretty -sL -w "%{http_code}\\n" -o /dev/null)
    echo "$RESULT Created alias $ALIAS for $INDEX"
}

function delete_alias {
    INDEX=$1
    ALIAS=$2
    export INDEX ALIAS
    cat <(envsubst < data/alias_delete.json) > tmp/alias_delete.json
    RESULT=$(curl -H "Content-Type: application/json" -X POST --data @tmp/alias_delete.json ${CONNECTION}/_aliases?pretty -sL -w "%{http_code}\\n" -o /dev/null)
    echo "$RESULT Deleted alias $ALIAS for index: $INDEX"
}


function rename_alias {
    OLD_INDEX=$1
    NEW_INDEX=$2
    ALIAS=$3
    export OLD_INDEX NEW_INDEX ALIAS
    cat <(envsubst < data/alias_rename.json) > tmp/alias_rename.json
    RESULT=$(curl -H "Content-Type: application/json" -X POST --data @tmp/alias_rename.json ${CONNECTION}/_aliases?pretty -sL -w "%{http_code}\\n" -o /dev/null)
    echo "$RESULT Renamed alias $ALIAS from index $OLD_INDEX to $NEW_INDEX"
}

function aliases {
    curl ${CONNECTION}/_cat/aliases
}

function indices {
    curl ${CONNECTION}/_cat/indices?pretty
}


indices
aliases
delete_alias my_index
create_alias my_index
rename_alias my_index my_index my_index-2017.05.31

drop_index my_index
index_create my_index