managing elasticsearch with bash script
default:
@bash setup_elasticsearch.sh http://10.0.0.0:9200
.PHONY: default
{
"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}" } }
]
}
#/bin/bash
export CONNECTION=$1
mkdir -p tmp
function drop_index {
INDEX=$1
RESULT=$(curl -X DELETE ${CONNECTION}/${INDEX}?pretty -sL -w "%{http_code}\\n" -o /dev/null)
echo "[$RESULT] Indice dropped ${INDEX}"
}
function create_alias2 {
ALIAS=$1
INDEX=$2
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 create_alias {
ALIAS=$1
INDEX=$2
RESULT=$(curl -X PUT ${CONNECTION}/${INDEX}/_alias/${ALIAS}?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 {
ALIAS=$1
OLD_INDEX=$2
NEW_INDEX=$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
}
function mapping {
INDEX=$1
curl -X GET ${CONNECTION}/${INDEX}/_mapping?pretty
}
function create_index {
INDEX=$1
RESULT=$(curl -H "Content-Type: application/json" -X PUT --data @data/trafficevent_mappings.json ${CONNECTION}/${INDEX}?pretty -sL -w "%{http_code}\\n" -o /dev/null)
# curl -H "Content-Type: appli/cation/json" -X PUT --data @data/trafficevent_mappings.json ${CONNECTION}/${INDEX}?pretty
echo "[${RESULT}] Index create status for ${INDEX}"
}
function check_type {
INDEX=$1
TYPE=$2
RESULT=$(curl -I ${CONNECTION}/${INDEX}/_mapping/${TYPE} -sL -w "%{http_code}\\n" -o /dev/null)
echo "[$RESULT] Type check for $TYPE in $INDEX"
}
ALIAS=trafficevent
OLD_INDEX=trafficevent-date:2017.06.01
NEW_INDEX="trafficevent-date:$(date +%Y.%m.%d)"
drop_index ${NEW_INDEX}
create_index ${NEW_INDEX}
create_alias ${ALIAS} ${NEW_INDEX}
# rename_alias trafficevent ${OLD_INDEX} ${NEW_INDEX}
check_type ${ALIAS} EsTrafficEventIndexer
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
}
},
"mappings": {
"EsTrafficEventIndexer": {
"properties": {
"id": {
"type": "long"
},
"plateNumber": {
"type": "keyword"
},
"dateTime": {
"type": "date"
},
"serverId": {
"type": "integer"
},
"cameraId": {
"type": "integer"
},
"laneNo": {
"type": "integer"
},
"speed": {
"type": "integer"
},
"pictureFileName": {
"enabled": false
},
"eventInfo": {
"type": "integer"
},
"eventStatus": {
"type": "integer"
},
"direction": {
"type": "integer"
},
"plateSource": {
"type": "integer"
},
"oldElrocNo": {
"enabled": false
},
"sendState": {
"enabled": false
},
"gpsCoordinates": {
"type": "geo_point"
},
"make": {
"type": "keyword"
},
"model": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"color": {
"type": "keyword"
},
"modelYear": {
"type": "integer"
},
"vehicleTypeId": {
"type": "integer"
}
}
}
}
}