Skip to content

Instantly share code, notes, and snippets.

@FreeTymeKiyan
Created March 6, 2016 21:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FreeTymeKiyan/bc83e96bb818c6ac311a to your computer and use it in GitHub Desktop.
Save FreeTymeKiyan/bc83e96bb818c6ac311a to your computer and use it in GitHub Desktop.
An example python script to reroute unassigned shards to NODE_NAME node thus recovering from the red cluster status
# example python script
# pip install requests before using requests
import requests
import json
HOSTNAME="your.elasticsearch.host.com" # hostname
PORT=9200 # port number
NODE_NAME="node001" # node to reroute to
def reroute(index, shard):
payload = { "commands": [{ "allocate": { "index": index, "shard": shard, "node": NODE_NAME, "allow_primary": 1 } }] }
res = requests.post("http://" + HOSTNAME + ":" + str(PORT) + "/_cluster/reroute", data=json.dumps(payload))
print res.text
pass
res = requests.post("http://" + HOSTNAME + ":" + str(PORT) + "/_flush/synced")
j = res.json()
for field in j:
if j[field]["failed"] != 0 and field != "_shards":
for item in j[field]["failures"]:
reroute(field, item["shard"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment