module ElasticSearchFramework::IndexAlias
Public Instance Methods
create()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 28 def create if !valid? raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Invalid Index alias.") end uri = URI("#{host}/_aliases") payload = { actions: [], } indexes.each do |index| action = nil if exists?(index: index[:klass]) action = "remove" if !index[:active] else action = "add" if index[:active] end next if action.nil? payload[:actions] << {action => {index: index[:klass].full_name, alias: self.full_name}} end request = Net::HTTP::Post.new(uri.request_uri) request.body = JSON.dump(payload) request.content_type = "application/json" response = repository.with_client do |client| client.request(request) end is_valid_response?(response.code) || Integer(response.code) == 404 end
delete()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 62 def delete uri = URI("#{host}/_all/_aliases/#{full_name}") request = Net::HTTP::Delete.new(uri.request_uri) response = repository.with_client do |client| client.request(request) end is_valid_response?(response.code) || Integer(response.code) == 404 end
delete_item(id:, type: "default", routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 144 def delete_item(id:, type: "default", routing_key: nil) indexes.each do |index| options = { index: index[:klass], id: id, type: type } options[:routing_key] = routing_key if index[:klass].routing_enabled? && routing_key repository.drop(options) end end
description()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 107 def description index = indexes.last[:klass] hash = index.instance_variable_get(:@elastic_search_index_def) if index.instance_variable_defined?(:@elastic_search_index_id) hash[:id] = index.instance_variable_get(:@elastic_search_index_id) else hash[:id] = :id end hash end
exists?(index:)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 74 def exists?(index:) uri = URI("#{host}/#{index.full_name}/_alias/#{full_name}") request = Net::HTTP::Get.new(uri.request_uri) response = repository.with_client do |client| client.request(request) end return false if response.code == "404" result = nil if is_valid_response?(response.code) result = JSON.parse(response.body) end return true if !result.nil? && result[index.full_name]["aliases"] != nil return false end
full_name()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 98 def full_name name = instance_variable_get(:@elastic_search_index_alias_name) if ElasticSearchFramework.namespace != nil "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{name.downcase}" else name.downcase end end
get_item(id:, type: "default", routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 126 def get_item(id:, type: "default", routing_key: nil) active_index = indexes.detect { |i| i[:active] == true }[:klass] options = { index: self, id: id, type: type } options[:routing_key] = routing_key if active_index.routing_enabled? && routing_key repository.get(options) end
host()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 118 def host "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}" end
index(klass, active:)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 3 def index(klass, active:) unless instance_variable_defined?(:@elastic_search_indexes) instance_variable_set(:@elastic_search_indexes, []) end indexes = self.instance_variable_get(:@elastic_search_indexes) indexes << {klass: klass, active: active} instance_variable_set(:@elastic_search_indexes, indexes) end
indexes()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 12 def indexes self.instance_variable_get(:@elastic_search_indexes) end
is_valid_response?(code)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 94 def is_valid_response?(code) [200, 201, 202].include?(Integer(code)) end
name(name)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 16 def name(name) unless instance_variable_defined?(:@elastic_search_index_alias_name) instance_variable_set(:@elastic_search_index_alias_name, "#{name}") else raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index alias name: #{name}.") end end
put_item(type: "default", item:, op_type: 'index', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 135 def put_item(type: "default", item:, op_type: 'index', routing_key: nil) indexes.each do |index| options = { entity: item, index: index[:klass], type: type, op_type: op_type } options[:routing_key] = routing_key if index[:klass].routing_enabled? && routing_key repository.set(options) end end
query()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 153 def query ElasticSearchFramework::Query.new(index: self) end
repository()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 122 def repository @repository ||= ElasticSearchFramework::Repository.new end
routing_enabled?()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 157 def routing_enabled? indexes.find(active: true).first[:klass].routing_enabled? end
valid?()
click to toggle source
# File lib/elastic_search_framework/index_alias.rb, line 24 def valid? indexes.select { |i| i[:active] == true }.length == 1 end