lib/desi/index_manager.rb in desi-0.2.7 vs lib/desi/index_manager.rb in desi-0.2.8
- old
+ new
@@ -6,10 +6,31 @@
# Performs some simple index-related operations on a local or distance
# Elastic Search cluster
class IndexManager
+ class Index
+ attr_reader :name, :number_of_documents
+
+ def initialize(name, data)
+ @name = name
+ @number_of_documents = data["docs"]["num_docs"] if data && data["docs"]
+ end
+
+ def to_s
+ @name
+ end
+
+ def inspect
+ "#{@name} (#{@number_of_documents} documents)"
+ end
+
+ def <=>(other)
+ @name <=> other.name
+ end
+ end
+
# Initializes a Desi::IndexManager instance
#
# @param [#to_hash] opts Hash of extra opts
#
# @option opts [#to_s] :host ('http://127.0.0.1:9200') Host to manage indices for
@@ -51,11 +72,11 @@
pattern = Regexp.new(pattern || '.*')
@outputter.puts "Indices from host #{@host} matching the pattern #{pattern.inspect}\n\n" if @verbose
list = indices(pattern).sort
- list.each {|i| @outputter.puts i } if @verbose
+ list.each {|i| @outputter.puts i.inspect } if @verbose
list
end
# Delete all indices matching the specified pattern
#
@@ -76,11 +97,11 @@
@outputter.puts "The following indices from host #{@host} are now deleted" if @verbose
indices(Regexp.new(pattern)).each do |index|
@client.delete("/#{index}")
- @outputter.puts " * #{index}" if @verbose
+ @outputter.puts " * #{index.inspect}" if @verbose
end
end
# Empty (remove all records) from indices matching the specified pattern
#
@@ -108,12 +129,12 @@
end
private
def indices(pattern)
- JSON.parse(@client.get('/_status').body)["indices"].keys.select {|i|
- i =~ pattern
- }
+ JSON.parse(@client.get('/_status').body)["indices"].map {|k, v|
+ Index.new(k, v) if k =~ pattern
+ }.compact
end
def to_uri(host_string)
scheme, host, port = ['http', '127.0.0.1', 9200]