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
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/elasticsearch/model/indexing.rb', line 304 def self.included(base) # Register callback for storing changed attributes for models # which implement `before_save` and `changed_attributes` methods # # @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 |instance| instance.instance_variable_set(:@__changed_attributes, Hash[ instance.changes.map { |key, value| [key, value.last] } ]) end if base.respond_to?(:before_save) && base.instance_methods.include?(:changed_attributes) end |
Instance Method Details
#delete_document(options = {}) ⇒ Hash
Deletes the model instance from the index
357 358 359 360 361 362 363 |
# File 'lib/elasticsearch/model/indexing.rb', line 357 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.
333 334 335 336 337 338 339 340 341 342 |
# File 'lib/elasticsearch/model/indexing.rb', line 333 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.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/elasticsearch/model/indexing.rb', line 389 def update_document(={}) if changed_attributes = self.instance_variable_get(:@__changed_attributes) attributes = if respond_to?(:as_indexed_json) self.as_indexed_json.select { |k,v| changed_attributes.keys.map(&:to_s).include? k.to_s } else changed_attributes 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)
422 423 424 425 426 427 428 429 |
# File 'lib/elasticsearch/model/indexing.rb', line 422 def update_document_attributes(attributes, ={}) client.update( { index: index_name, type: document_type, id: self.id, body: { doc: attributes } }.merge() ) end |