lib/desi/index_manager.rb in desi-0.5.0 vs lib/desi/index_manager.rb in desi-0.6.0
- old
+ new
@@ -8,28 +8,56 @@
# Performs some simple index-related operations on a local or distance
# Elastic Search cluster
class IndexManager
class Index
- attr_reader :name, :number_of_documents
+ attr_reader :name, :number_of_documents, :aliases, :state, :number_of_documents
- def initialize(name, data)
+ def initialize(name, state_data, status_data)
@name = name
- @number_of_documents = data["docs"]["num_docs"] if data && data["docs"]
+ @number_of_documents = status_data["docs"]["num_docs"] if status_data && status_data["docs"]
+ @aliases = []
+
+ if state_data
+ @aliases = state_data['aliases']
+ @state = state_data['state']
+ end
end
def to_s
- @name
+ name
end
def inspect
- "#{@name} (#{@number_of_documents} documents)"
+ "#{name} (#{number_of_docs_label})#{aliases_label}"
end
+ def aliased?
+ !(aliases.nil? || aliases.empty?)
+ end
+
def <=>(other)
- @name <=> other.name
+ name <=> other.name
end
+
+ def open?
+ state == "open"
+ end
+
+ def closed?
+ state == "close"
+ end
+
+ private
+
+ def number_of_docs_label
+ closed? ? 'CLOSED' : "#{number_of_documents} docs"
+ end
+
+ def aliases_label
+ aliased? ? ". Aliases: #{aliases.join(', ')}" : nil
+ end
end
# Initializes a Desi::IndexManager instance
#
# @param [#to_hash] opts Hash of extra opts
@@ -130,11 +158,16 @@
end
private
def indices(pattern)
- JSON.parse(@client.get('/_status').body)["indices"].map {|k, v|
- Index.new(k, v) if k =~ pattern
+ cluster_state = JSON.parse(@client.get('/_cluster/state').body)
+ status = JSON.parse(@client.get('/_status').body)
+
+ cluster_state["metadata"]["indices"].map {|k, v|
+ if k =~ pattern
+ Index.new(k, v, status['indices'][k])
+ end
}.compact
end
def to_uri(host_string)
scheme, host, port = ['http', 'localhost', 9200]