Module: Elasticsearch::Model::Indexing::ClassMethods
- Included in:
- Proxy::ClassMethodsProxy
- Defined in:
- lib/elasticsearch/model/indexing.rb
Instance Method Summary collapse
-
#create_index!(options = {}) ⇒ Object
Creates an index with correct name, automatically passing ‘settings` and `mappings` defined in the model.
-
#delete_index!(options = {}) ⇒ Object
Deletes the index with corresponding name.
-
#index_exists?(options = {}) ⇒ Boolean
Returns true if the index exists.
- #load_settings_from_io(settings) ⇒ Object
-
#mapping(options = {}, &block) ⇒ Object
(also: #mappings)
Defines mappings for the index.
-
#refresh_index!(options = {}) ⇒ Object
Performs the “refresh” operation for the index (useful e.g. in tests).
-
#settings(settings = {}, &block) ⇒ Object
Define settings for the index.
Instance Method Details
#create_index!(options = {}) ⇒ Object
Creates an index with correct name, automatically passing ‘settings` and `mappings` defined in the model
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/elasticsearch/model/indexing.rb', line 241 def create_index!(={}) = .clone target_index = .delete(:index) || self.index_name settings = .delete(:settings) || self.settings.to_hash mappings = .delete(:mappings) || self.mappings.to_hash delete_index!(.merge index: target_index) if [:force] unless index_exists?(index: target_index) .delete(:force) self.client.indices.create({ index: target_index, body: { settings: settings, mappings: mappings } }.merge()) end end |
#delete_index!(options = {}) ⇒ Object
Deletes the index with corresponding name
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/elasticsearch/model/indexing.rb', line 286 def delete_index!(={}) target_index = .delete(:index) || self.index_name begin self.client.indices.delete index: target_index rescue Exception => e if e.class.to_s =~ /NotFound/ && [:force] client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger nil else raise e end end end |
#index_exists?(options = {}) ⇒ Boolean
Returns true if the index exists
270 271 272 273 274 |
# File 'lib/elasticsearch/model/indexing.rb', line 270 def index_exists?(={}) target_index = [:index] || self.index_name self.client.indices.exists(index: target_index) rescue false end |
#load_settings_from_io(settings) ⇒ Object
222 223 224 |
# File 'lib/elasticsearch/model/indexing.rb', line 222 def load_settings_from_io(settings) YAML.load(settings.read) end |
#mapping(options = {}, &block) ⇒ Object Also known as: mappings
Defines mappings for the index
The ‘mappings` and `settings` methods are accessible directly on the model class, when it doesn’t already define them. Use the ‘__elasticsearch__` proxy otherwise.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/elasticsearch/model/indexing.rb', line 154 def mapping(={}, &block) @mapping ||= Mappings.new(document_type, ) @mapping..update() unless .empty? if block_given? @mapping.instance_eval(&block) return self else @mapping end end |
#refresh_index!(options = {}) ⇒ Object
Performs the “refresh” operation for the index (useful e.g. in tests)
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/elasticsearch/model/indexing.rb', line 313 def refresh_index!(={}) target_index = .delete(:index) || self.index_name begin self.client.indices.refresh index: target_index rescue Exception => e if e.class.to_s =~ /NotFound/ && [:force] client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger nil else raise e end end end |
#settings(settings = {}, &block) ⇒ Object
Define settings for the index
You can read settings from any object that responds to :read as long as its return value can be parsed as either YAML or JSON.
208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/elasticsearch/model/indexing.rb', line 208 def settings(settings={}, &block) settings = YAML.load(settings.read) if settings.respond_to?(:read) @settings ||= Settings.new(settings) @settings.settings.update(settings) unless settings.empty? if block_given? self.instance_eval(&block) return self else @settings end end |