Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 123 lines (104 sloc) 4.174 kb
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'rake'
require 'configliere' ; Configliere.use(:commandline, :env_var, :define)
Settings.define :host, :required => true, :description => "Elastic search cluster host ip address"
Settings.define :port, :default => 9200, :description => "Elastic search cluster port"
Settings.define :index_name, :description => "Which index to address?"
Settings.define :replicas, :default => 1, :description => "Number of replicas to set index to"
Settings.define :object_type, :description => "When 'putting' the mapping, which object type to update"
Settings.define :object_def_file, :description => "When 'putting' the mapping, full path to json config file describing object and its fields"
Settings.resolve!
#
# Prints status of all indices as a json hash to the terminal
#
task :status do
sh "curl -s -XGET \"http://%s:%s/_status?pretty=true\"" % [Settings.host, Settings.port]
end
#
# Prints general cluster health as json hash to the terminal
#
task :health do
sh "curl -s -XGET \"http://%s:%s/_cluster/health?pretty=true\"" % [Settings.host, Settings.port]
end
#
# Executes a full flush of the specified index, can take a while for large indices
#
task :flush_index do
sh "curl -s -XPOST \"http://%s:%s/%s/_flush?full=true\"" % [Settings.host, Settings.port, Settings.index_name]
end
#
# Executes a full flush of all indices, this will negatively impact read performance while
# it's taking place.
#
task :flush_all do
sh "curl -s -XPOST \"http://%s:%s/_flush?full=true\"" % [Settings.host, Settings.port]
end
#
# Creates the specified index
#
task :create_index do
sh "curl -s -XPUT \"http://%s:%s/%s/\"" % [Settings.host, Settings.port, Settings.index_name]
end
#
# Sets the number of replicas for the given index. Typically want it set to 0
# for bulk loading then bump up to 1 later.
#
task :set_replicas do
sh "curl -s -XPUT \"http://%s:%s/%s/_settings\" -d {\"index\":{\"number_of_replicas\":%s}}" % [Settings.host, Settings.port, Settings.index_name, Settings.replicas]
end
#
# Does a full compaction, merging smaller index files, for the given index.
#
task :optimize_index do
sh "curl -s -XPOST \"http://%s:%s/%s/_optimize?max_num_segments=1\"" % [Settings.host, Settings.port, Settings.index_name]
end
#
# Snapshot index to gateway
#
task :snapshot_index do
sh "curl -s -XPOST \"http://%s:%s/%s/_gateway/snapshot\"" % [Settings.host, Settings.port, Settings.index_name]
end
#
# Delete an index, USE WITH CAUTION!
#
task :delete_index do
sh "curl -s -XDELETE \"http://%s:%s/%s/\"" % [Settings.host, Settings.port, Settings.index_name]
end
#
# Refreshes the index and makes all operations since the last refresh avaialable for search
#
task :refresh_index do
sh "curl -s -XPOST \"http://%s:%s/%s/_refresh\"" % [Settings.host, Settings.port, Settings.index_name]
end
#
# Puts simple mapping from json file into the given index
#
task :put_mapping do
json_def = File.read(Settings.object_def_file)
sh "curl -s -XPUT \"http://%s:%s/%s/%s/_mapping\" -d '#{json_def}'" % [Settings.host, Settings.port, Settings.index_name, Settings.object_type]
end
Rake::Task[Settings.rest.first].invoke
#
# Alternatively, one could use the rubberband gem
#
# $: sudo apt-get install libcurl4-dev
# $: sudo gem install rubberband
#
#
# require 'rubberband'
#
# client = ElasticSearch.new('10.195.215.175:9200', :index => "tweet-2010q1", :type => "tweet")
# client.search "text:infochimps"
# index_status = client.index_status
# num_docs = index_status['indices']['tweet-2010q1']['docs']['num_docs']
# num_shards = index_status['indices']['tweet-2010q1']['settings']['index.number_of_shards']
# num_replicas = index_status['indices']['tweet-2010q1']['settings']['index.number_of_replicas']
# size_in_bytes = index_status['indices']['tweet-2010q1']['store_size_in_bytes']
#
# state = client.index_state
# es_node_mapping = state['nodes'].inject({}){|hsh,node| hsh[node.last['name']] = node.last['transport_address']; hsh}
#
# def index_ok?; client.index_status['ok']; done
# def cluster_color; client.cluster_health["status"]; done
Jump to Line
Something went wrong with that request. Please try again.