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



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

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

.pool_sizeObject



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

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

.pool_timeoutObject



178
179
180
# File 'lib/elastic_search_framework/repository.rb', line 178

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

Instance Method Details

#clientObject



111
112
113
114
115
116
117
# File 'lib/elastic_search_framework/repository.rb', line 111

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



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

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



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

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



147
148
149
150
151
152
153
# File 'lib/elastic_search_framework/repository.rb', line 147

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



143
144
145
# File 'lib/elastic_search_framework/repository.rb', line 143

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

#hostObject



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

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

#host_uriObject



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

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

#idle_timeoutObject



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

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

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



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

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



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

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

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



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

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



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

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

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



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

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

Returns:

  • (Boolean)


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

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

#with_clientObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/elastic_search_framework/repository.rb', line 155

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