Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save innovia/a4ad63c1844ae194e5d9 to your computer and use it in GitHub Desktop.
Save innovia/a4ad63c1844ae194e5d9 to your computer and use it in GitHub Desktop.
ElasticSaerch unassigned shards reassign
#!/usr/bin/env ruby
require 'json'
es_cluster = 'http://localhost:9200'
master_node = "Howard the Duck" # sey your master node name here
unassigned_shards_info = `curl -s -XGET #{es_cluster}/_cat/shards | grep UNASSIGNED `.split("\n")
shard_indexes = []
unassigned_shards_info.inject(shard_indexes){|acc, index|
shard = index.split(" ")
shard_data ||= {}
shard_data[shard.first] = shard[1]
acc << shard_data
}
puts "total unassiged shards: #{unassigned_shards_info.size}"
shard_indexes.each {|shard_info|
shard_info.each {|index, shard|
puts "reassigning shard no: #{shard} with index #{index} to #{node}"
payload = {"commands"=> [{"allocate"=>{"index"=>"#{index}", "shard"=>"#{shard}", "node"=>"#{node}", "allow_primary"=>true}}]}
result = `curl -s -XPOST #{es_cluster}/_cluster/reroute -d '#{payload.to_json}'`
}
sleep 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment