class ElasticSearchFramework::Repository
Public Class Methods
pool()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 185 def self.pool @pool ||= ConnectionPool.new(size: pool_size, timeout: pool_timeout) { ElasticSearchFramework::Repository.new } end
pool_size()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 189 def self.pool_size @pool_size ||= Integer(ENV['CONNECTION_POOL_SIZE'] || 25) end
pool_timeout()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 193 def self.pool_timeout @pool_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5) end
Public Instance Methods
client()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 126 def client @client ||= Net::HTTP.new(host_uri.host, host_uri.port).tap do |c| c.use_ssl = host_uri.scheme == 'https' c.open_timeout = open_timeout c.read_timeout = read_timeout end end
drop(index:, id:, type: 'default', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 55 def drop(index:, id:, type: 'default', routing_key: nil) uri_string = "#{host}/#{index.full_name}/#{type.downcase}/#{id}" uri_string += "?routing=#{routing_key}" if routing_key uri = URI(uri_string) request = Net::HTTP::Delete.new(uri.request_uri) response = with_client do |client| client.request(request) end if valid_response?(response.code) || Integer(response.code) == 404 return true else raise ElasticSearchFramework::Exceptions::IndexError.new( "An error occurred dropping an index document. Response: #{response.body}" ) end end
get(index:, id:, type: 'default', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 30 def get(index:, id:, type: 'default', routing_key: nil) uri_string = "#{host}/#{index.full_name}/#{type.downcase}/#{id}/_source" uri_string += "?routing=#{routing_key}" if routing_key uri = URI(uri_string) request = Net::HTTP::Get.new(uri.request_uri) response = with_client do |client| client.request(request) end if valid_response?(response.code) result = JSON.load(response.body) hash_helper.indifferent!(result) return result elsif Integer(response.code) == 404 return nil else raise ElasticSearchFramework::Exceptions::IndexError.new( "An error occurred getting an index document. Response: #{response.body}" ) end end
get_id_value(index:, entity:)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 162 def get_id_value(index:, entity:) if entity.is_a?(Hash) entity[index.description[:id].to_sym] || entity[index.description[:id].to_s] else entity.instance_variable_get("@#{index.description[:id]}") end end
hash_helper()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 158 def hash_helper @hash_helper ||= HashKit::Helper.new end
host()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 150 def host "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}" end
host_uri()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 154 def host_uri URI("#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}") end
idle_timeout()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 134 def idle_timeout @idle_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5) end
json_query(index_name:, json_query:, type: 'default', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 101 def json_query(index_name:, json_query:, type: 'default', routing_key: nil) uri_string = "#{host}/#{index_name}/#{type}/_search" uri_string += "?routing=#{routing_key}" if routing_key uri = URI(uri_string) request = Net::HTTP::Get.new(uri.request_uri) request.content_type = 'application/json' request.body = json_query response = with_client do |client| client.request(request) end if valid_response?(response.code) result = JSON.parse(response.body) return result['hits'] else raise( ElasticSearchFramework::Exceptions::IndexError, "An error occurred executing an index query. Response: #{response.body}" ) end end
open_timeout()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 142 def open_timeout @open_timeout ||= Integer(ENV['CONNECTION_OPEN_TIMEOUT'] || 1) end
query(index:, expression:, type: 'default', limit: 10, count: false, routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 76 def query(index:, expression:, type: 'default', limit: 10, count: false, routing_key: nil) uri_string = "#{host}/#{index.full_name}/#{type}/_search?q=#{URI.encode(expression)}&size=#{limit}" uri_string += "&routing=#{routing_key}" if routing_key uri = URI(uri_string) request = Net::HTTP::Get.new(uri.request_uri) response = with_client do |client| client.request(request) end if valid_response?(response.code) result = JSON.parse(response.body) hash_helper.indifferent!(result) if count return result[:hits][:total] else return result[:hits][:total] > 0 ? result[:hits][:hits] : [] end else raise ElasticSearchFramework::Exceptions::IndexError.new("An error occurred executing an index query. Response: #{response.body}") end end
read_timeout()
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 138 def read_timeout @read_timeout ||= Integer(ENV['CONNECTION_READ_TIMEOUT'] || 5) end
set(index:, entity:, type: 'default', op_type: 'index', routing_key: nil)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 4 def set(index:, entity:, type: 'default', op_type: 'index', routing_key: nil) uri_string = "#{host}/#{index.full_name}/#{type.downcase}/#{get_id_value(index: index, entity: entity)}?op_type=#{op_type}" uri_string += "&routing=#{routing_key}" if routing_key uri = URI(uri_string) hash = hash_helper.to_hash(entity) request = Net::HTTP::Put.new(uri.request_uri) request.body = JSON.dump(hash) request.content_type = 'application/json' response = with_client do |client| client.request(request) end if valid_response?(response.code) return true elsif op_type == 'create' && Integer(response.code) == 409 return true else raise ElasticSearchFramework::Exceptions::IndexError.new( "An error occurred setting an index document. Response: #{response.body} | Code: #{response.code}" ) end end
valid_response?(status)
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 146 def valid_response?(status) [200, 201, 202].include?(Integer(status)) end
with_client() { |client| ... }
click to toggle source
# File lib/elastic_search_framework/repository.rb, line 170 def with_client response = nil self.class.pool.with do |base| base.client.read_timeout = read_timeout begin response = yield base.client ensure base.client.read_timeout = idle_timeout end end response end