Module: Elasticsearch::Model::Indexing::InstanceMethods
- Defined in:
- lib/elasticsearch/model/indexing.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#delete_document(options = {}) ⇒ Hash
Deletes the model instance from the index.
-
#index_document(options = {}) ⇒ Hash
Serializes the model instance into JSON (by calling ‘as_indexed_json`), and saves the document into the Elasticsearch index.
-
#update_document(options = {}) ⇒ Hash
Tries to gather the changed attributes of a model instance (via [ActiveModel::Dirty](api.rubyonrails.org/classes/ActiveModel/Dirty.html)), performing a partial update of the document.
-
#update_document_attributes(attributes, options = {}) ⇒ Hash
Perform a partial update of specific document attributes (without consideration for changed attributes as in #update_document).
Class Method Details
.included(base) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/elasticsearch/model/indexing.rb', line 308 def self.included(base) # Register callback for storing changed attributes for models # which implement `before_save` and return changed attributes # (ie. when `Elasticsearch::Model` is included) # # @note This is typically triggered only when the module would be # included in the model directly, not within the proxy. # # @see #update_document # base.before_save do |i| if i.class.instance_methods.include?(:changes_to_save) # Rails 5.1 i.instance_variable_set(:@__changed_model_attributes, Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ]) elsif i.class.instance_methods.include?(:changes) i.instance_variable_set(:@__changed_model_attributes, Hash[ i.changes.map { |key, value| [key, value.last] } ]) end end if base.respond_to?(:before_save) end |
Instance Method Details
#delete_document(options = {}) ⇒ Hash
Deletes the model instance from the index
367 368 369 370 371 372 373 |
# File 'lib/elasticsearch/model/indexing.rb', line 367 def delete_document(={}) client.delete( { index: index_name, type: document_type, id: self.id }.merge() ) end |
#index_document(options = {}) ⇒ Hash
Serializes the model instance into JSON (by calling ‘as_indexed_json`), and saves the document into the Elasticsearch index.
343 344 345 346 347 348 349 350 351 352 |
# File 'lib/elasticsearch/model/indexing.rb', line 343 def index_document(={}) document = self.as_indexed_json client.index( { index: index_name, type: document_type, id: self.id, body: document }.merge() ) end |
#update_document(options = {}) ⇒ Hash
Tries to gather the changed attributes of a model instance (via [ActiveModel::Dirty](api.rubyonrails.org/classes/ActiveModel/Dirty.html)), performing a partial update of the document.
When the changed attributes are not available, performs full re-index of the record.
See the #update_document_attributes method for updating specific attributes directly.
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/elasticsearch/model/indexing.rb', line 399 def update_document(={}) if attributes_in_database = self.instance_variable_get(:@__changed_model_attributes).presence attributes = if respond_to?(:as_indexed_json) self.as_indexed_json.select { |k,v| attributes_in_database.keys.map(&:to_s).include? k.to_s } else attributes_in_database end client.update( { index: index_name, type: document_type, id: self.id, body: { doc: attributes } }.merge() ) else index_document() end end |
#update_document_attributes(attributes, options = {}) ⇒ Hash
Perform a partial update of specific document attributes (without consideration for changed attributes as in #update_document)
432 433 434 435 436 437 438 439 |
# File 'lib/elasticsearch/model/indexing.rb', line 432 def update_document_attributes(attributes, ={}) client.update( { index: index_name, type: document_type, id: self.id, body: { doc: attributes } }.merge() ) end |