Module: ElasticSearchFramework::Index

Defined in:
lib/elastic_search_framework/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index_settingsObject

Returns the value of attribute index_settings.



3
4
5
# File 'lib/elastic_search_framework/index.rb', line 3

def index_settings
  @index_settings
end

Instance Method Details

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/elastic_search_framework/index.rb', line 40

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_payloadObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/elastic_search_framework/index.rb', line 115

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

#deleteObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/elastic_search_framework/index.rb', line 96

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



179
180
181
# File 'lib/elastic_search_framework/index.rb', line 179

def delete_item(id:, type: 'default')
  repository.drop(index: self, id: id, type: type)
end

#descriptionObject



137
138
139
140
141
142
143
144
145
# File 'lib/elastic_search_framework/index.rb', line 137

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?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/elastic_search_framework/index.rb', line 92

def exists?
  get != nil
end

#full_nameObject



155
156
157
158
159
160
161
# File 'lib/elastic_search_framework/index.rb', line 155

def full_name
  if ElasticSearchFramework.namespace != nil
    "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{description[:name].downcase}"
  else
    description[:name].downcase
  end
end

#getObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/elastic_search_framework/index.rb', line 76

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



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

def get_item(id:, type: 'default')
  repository.get(index: self, id: id, type: type)
end

#hostObject



163
164
165
# File 'lib/elastic_search_framework/index.rb', line 163

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

#id(field) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/elastic_search_framework/index.rb', line 18

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



5
6
7
8
9
10
11
12
# File 'lib/elastic_search_framework/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) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/elastic_search_framework/index.rb', line 151

def is_valid_response?(code)
  [200,201,202].include?(Integer(code))
end

#mapping(name:, field:, **options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/elastic_search_framework/index.rb', line 26

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

#mappingsObject



147
148
149
# File 'lib/elastic_search_framework/index.rb', line 147

def mappings
  self.instance_variable_defined?(:@elastic_search_index_mappings) ? self.instance_variable_get(:@elastic_search_index_mappings) : {}
end

#put(payload:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/elastic_search_framework/index.rb', line 54

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



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

def put_item(type: 'default', item:)
  repository.set(entity: item, index: self, type: type)
end

#queryObject



183
184
185
# File 'lib/elastic_search_framework/index.rb', line 183

def query
  ElasticSearchFramework::Query.new(index: self)
end

#repositoryObject



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

def repository
  @repository ||= ElasticSearchFramework::Repository.new
end

#settings(name:, type: nil, value:) ⇒ Object



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

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?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/elastic_search_framework/index.rb', line 133

def valid?
  self.instance_variable_get(:@elastic_search_index_def) ? true : false
end

#versionObject



14
15
16
# File 'lib/elastic_search_framework/index.rb', line 14

def version
  instance_variable_defined?(:@elastic_search_index_version) ? instance_variable_get(:@elastic_search_index_version) : 0
end