Module: ElasticSearchFramework::Index
- Defined in:
- lib/elastic_search_framework/index.rb
Instance Attribute Summary collapse
-
#index_settings ⇒ Object
Returns the value of attribute index_settings.
Instance Method Summary collapse
- #create ⇒ Object
- #create_payload ⇒ Object
- #delete ⇒ Object
- #delete_item(id:, type: 'default', routing_key: nil) ⇒ Object
- #description ⇒ Object
- #exists? ⇒ Boolean
- #full_name ⇒ Object
- #get ⇒ Object
- #get_item(id:, type: 'default', routing_key: nil) ⇒ Object
- #host ⇒ Object
- #id(field) ⇒ Object
- #index(name:, version: nil) ⇒ Object
- #is_valid_response?(code) ⇒ Boolean
- #mapping(name:, field:, **options) ⇒ Object
- #mappings ⇒ Object
- #put(payload:) ⇒ Object
- #put_item(type: 'default', item:, op_type: 'index', routing_key: nil) ⇒ Object
- #query ⇒ Object
- #repository ⇒ Object
- #routing_enabled? ⇒ Boolean
- #settings(name:, type: nil, value:) ⇒ Object
- #valid? ⇒ Boolean
- #version ⇒ Object
Instance Attribute Details
#index_settings ⇒ Object
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
#create ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/elastic_search_framework/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 ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/elastic_search_framework/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 ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/elastic_search_framework/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) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/elastic_search_framework/index.rb', line 189 def delete_item(id:, type: 'default', routing_key: nil) = { index: self, id: id, type: type } [:routing_key] = routing_key if routing_enabled? && routing_key repository.drop(**) end |
#description ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/elastic_search_framework/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? ⇒ Boolean
96 97 98 |
# File 'lib/elastic_search_framework/index.rb', line 96 def exists? get != nil end |
#full_name ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/elastic_search_framework/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 ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/elastic_search_framework/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) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/elastic_search_framework/index.rb', line 175 def get_item(id:, type: 'default', routing_key: nil) = { index: self, id: id, type: type } [:routing_key] = routing_key if routing_enabled? && routing_key repository.get(**) end |
#host ⇒ Object
167 168 169 |
# File 'lib/elastic_search_framework/index.rb', line 167 def host "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}" end |
#id(field) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/elastic_search_framework/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) ⇒ 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
155 156 157 |
# File 'lib/elastic_search_framework/index.rb', line 155 def is_valid_response?(code) [200,201,202].include?(Integer(code)) end |
#mapping(name:, field:, **options) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/elastic_search_framework/index.rb', line 30 def mapping(name:, field:, **) 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] = instance_variable_set(:@elastic_search_index_mappings, mappings) end |
#mappings ⇒ Object
151 152 153 |
# File 'lib/elastic_search_framework/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:) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/elastic_search_framework/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) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/elastic_search_framework/index.rb', line 182 def put_item(type: 'default', item:, op_type: 'index', routing_key: nil) = { entity: item, index: self, type: type, op_type: op_type } [:routing_key] = routing_key if routing_enabled? && routing_key repository.set(**) end |
#query ⇒ Object
196 197 198 |
# File 'lib/elastic_search_framework/index.rb', line 196 def query ElasticSearchFramework::Query.new(index: self) end |
#repository ⇒ Object
171 172 173 |
# File 'lib/elastic_search_framework/index.rb', line 171 def repository @repository ||= ElasticSearchFramework::Repository.new end |
#routing_enabled? ⇒ Boolean
18 19 20 |
# File 'lib/elastic_search_framework/index.rb', line 18 def routing_enabled? false end |
#settings(name:, type: nil, value:) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/elastic_search_framework/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? ⇒ Boolean
137 138 139 |
# File 'lib/elastic_search_framework/index.rb', line 137 def valid? self.instance_variable_get(:@elastic_search_index_def) ? true : false end |
#version ⇒ Object
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 |