Class: ElasticSearchFramework::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_search_framework/repository.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.poolObject



167
168
169
# File 'lib/elastic_search_framework/repository.rb', line 167

def self.pool
  @pool ||= ConnectionPool.new(size: pool_size, timeout: pool_timeout) { ElasticSearchFramework::Repository.new }
end

.pool_sizeObject



171
172
173
# File 'lib/elastic_search_framework/repository.rb', line 171

def self.pool_size
  @pool_size ||= Integer(ENV['CONNECTION_POOL_SIZE'] || 25)
end

.pool_timeoutObject



175
176
177
# File 'lib/elastic_search_framework/repository.rb', line 175

def self.pool_timeout
  @pool_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5)
end

Instance Method Details

#clientObject



108
109
110
111
112
113
114
# File 'lib/elastic_search_framework/repository.rb', line 108

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') ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/elastic_search_framework/repository.rb', line 46

def drop(index:, id:, type: 'default')
  uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{id}")

  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') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/elastic_search_framework/repository.rb', line 24

def get(index:, id:, type: 'default')
  uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{id}/_source")

  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:) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/elastic_search_framework/repository.rb', line 144

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_helperObject



140
141
142
# File 'lib/elastic_search_framework/repository.rb', line 140

def hash_helper
  @hash_helper ||= HashKit::Helper.new
end

#hostObject



132
133
134
# File 'lib/elastic_search_framework/repository.rb', line 132

def host
  "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}"
end

#host_uriObject



136
137
138
# File 'lib/elastic_search_framework/repository.rb', line 136

def host_uri
  URI("#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}")
end

#idle_timeoutObject



116
117
118
# File 'lib/elastic_search_framework/repository.rb', line 116

def idle_timeout
  @idle_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5)
end

#json_query(index_name:, json_query:, type: 'default') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/elastic_search_framework/repository.rb', line 86

def json_query(index_name:, json_query:, type: 'default')
  uri = URI("#{host}/#{index_name}/#{type}/_search")

  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_timeoutObject



124
125
126
# File 'lib/elastic_search_framework/repository.rb', line 124

def open_timeout
  @open_timeout ||= Integer(ENV['CONNECTION_OPEN_TIMEOUT'] || 1)
end

#query(index:, expression:, type: 'default', limit: 10, count: false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/elastic_search_framework/repository.rb', line 64

def query(index:, expression:, type: 'default', limit: 10, count: false)
  uri = URI("#{host}/#{index.full_name}/#{type}/_search?q=#{URI.encode(expression)}&size=#{limit}")

  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_timeoutObject



120
121
122
# File 'lib/elastic_search_framework/repository.rb', line 120

def read_timeout
  @read_timeout ||= Integer(ENV['CONNECTION_READ_TIMEOUT'] || 5)
end

#set(index:, entity:, type: 'default') ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/elastic_search_framework/repository.rb', line 4

def set(index:, entity:, type: 'default')
  uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{get_id_value(index: index, entity: entity)}")
  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

  unless valid_response?(response.code)
    raise ElasticSearchFramework::Exceptions::IndexError.new(
        "An error occurred setting an index document. Response: #{response.body} | Code: #{response.code}"
    )
  end
  return true
end

#valid_response?(status) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/elastic_search_framework/repository.rb', line 128

def valid_response?(status)
  [200, 201, 202].include?(Integer(status))
end

#with_clientObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/elastic_search_framework/repository.rb', line 152

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