module ElasticSearchFramework::ShardedIndex
Attributes
index_settings[RW]
Public Instance Methods
create()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 44 def create if !valid? raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Invalid Index description specified.") end if exists? ElasticSearchFramework.logger.debug { "[#{self.class}] - Index already exists."} return end payload = create_payload put(payload: payload) end
create_payload()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 119 def create_payload payload = { } payload[:settings] = index_settings unless index_settings.nil? unless mappings.keys.empty? payload[:mappings] = {} mappings.keys.each do |name| payload[:mappings][name] = { properties: {} } mappings[name].keys.each do |field| payload[:mappings][name][:properties][field] = mappings[name][field] end end end payload end
delete()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 100 def delete uri = URI("#{host}/#{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/sharded_index.rb, line 189 def delete_item(id:, type: 'default', routing_key: nil) options = { index: self, id: id, type: type } options[:routing_key] = routing_key if routing_enabled? && routing_key repository.drop(options) end
description()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 141 def description hash = self.instance_variable_get(:@elastic_search_index_def) if instance_variable_defined?(:@elastic_search_index_id) hash[:id] = self.instance_variable_get(:@elastic_search_index_id) else hash[:id] = :id end hash end
exists?()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 96 def exists? get != nil end
full_name()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 159 def full_name if ElasticSearchFramework.namespace != nil "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{description[:name].downcase}" else description[:name].downcase end end
get()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 80 def get uri = URI("#{host}/#{full_name}") request = Net::HTTP::Get.new(uri.request_uri) response = repository.with_client do |client| client.request(request) end result = nil if is_valid_response?(response.code) result = JSON.parse(response.body) end result end
get_item(id:, type: 'default', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 175 def get_item(id:, type: 'default', routing_key: nil) options = { index: self, id: id, type: type } options[:routing_key] = routing_key if routing_enabled? && routing_key repository.get(options) end
host()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 167 def host "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}" end
id(field)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 22 def id(field) unless instance_variable_defined?(:@elastic_search_index_id) instance_variable_set(:@elastic_search_index_id, field) else raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index id. Field: #{field}.") end end
index(name:, version: nil)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 5 def index(name:, version: nil) unless instance_variable_defined?(:@elastic_search_index_def) instance_variable_set(:@elastic_search_index_def, name: "#{name}#{version}") instance_variable_set(:@elastic_search_index_version, version: version) unless version.nil? else raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name}.") end end
is_valid_response?(code)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 155 def is_valid_response?(code) [200,201,202].include?(Integer(code)) end
mapping(name:, field:, **options)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 30 def mapping(name:, field:, **options) unless instance_variable_defined?(:@elastic_search_index_mappings) instance_variable_set(:@elastic_search_index_mappings, {}) end mappings = instance_variable_get(:@elastic_search_index_mappings) mappings[name] = {} if mappings[name].nil? mappings[name][field] = options instance_variable_set(:@elastic_search_index_mappings, mappings) end
mappings()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 151 def mappings self.instance_variable_defined?(:@elastic_search_index_mappings) ? self.instance_variable_get(:@elastic_search_index_mappings) : {} end
put(payload:)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 58 def put(payload:) uri = URI("#{host}/#{full_name}") request = Net::HTTP::Put.new(uri.request_uri) request.body = JSON.dump(payload) request.content_type = 'application/json' response = repository.with_client do |client| client.request(request) end unless is_valid_response?(response.code) if JSON.parse(response.body, symbolize_names: true).dig(:error, :root_cause, 0, :type) == 'index_already_exists_exception' # We get here because the `exists?` check in #create is non-atomic ElasticSearchFramework.logger.warn "[#{self.class}] - Failed to create preexisting index. | Response: #{response.body}" else raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self}] - Failed to put index. Payload: #{payload} | Response: #{response.body}") end end true end
put_item(type: 'default', item:, op_type: 'index', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 182 def put_item(type: 'default', item:, op_type: 'index', routing_key: nil) options = { entity: item, index: self, type: type, op_type: op_type } options[:routing_key] = routing_key if routing_enabled? && routing_key repository.set(options) end
query()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 196 def query ElasticSearchFramework::Query.new(index: self) end
repository()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 171 def repository @repository ||= ElasticSearchFramework::Repository.new end
routing_enabled?()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 18 def routing_enabled? true end
settings(name:, type: nil, value:)
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 112 def settings(name:, type: nil, value:) self.index_settings = {} if index_settings.nil? index_settings[name] = {} if index_settings[name].nil? return index_settings[name][type] = value if type index_settings[name] = value end
valid?()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 137 def valid? self.instance_variable_get(:@elastic_search_index_def) ? true : false end
version()
click to toggle source
# File lib/elastic_search_framework/sharded_index.rb, line 14 def version instance_variable_defined?(:@elastic_search_index_version) ? instance_variable_get(:@elastic_search_index_version) : 0 end