Module: Elasticsearch::Model::Indexing::InstanceMethods
- Included in:
- Proxy::InstanceMethodsProxy
- 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
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/elasticsearch/model/indexing.rb', line 322 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 |obj| if obj.respond_to?(:changes_to_save) # Rails 5.1 changes_to_save = obj.changes_to_save elsif obj.respond_to?(:changes) changes_to_save = obj.changes end if changes_to_save attrs = obj.instance_variable_get(:@__changed_model_attributes) || {} latest_changes = changes_to_save.inject({}) { |latest_changes, (k,v)| latest_changes.merge!(k => v.last) } obj.instance_variable_set(:@__changed_model_attributes, attrs.merge(latest_changes)) end end if base.respond_to?(:before_save) end |
Instance Method Details
#delete_document(options = {}) ⇒ Hash
Deletes the model instance from the index
383 384 385 386 387 388 |
# File 'lib/elasticsearch/model/indexing.rb', line 383 def delete_document(={}) request = { index: index_name, id: self.id } client.delete(request.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.
361 362 363 364 365 366 367 368 |
# File 'lib/elasticsearch/model/indexing.rb', line 361 def index_document(={}) document = as_indexed_json request = { index: index_name, id: id, body: document } client.index(request.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.
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/elasticsearch/model/indexing.rb', line 414 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 unless attributes.empty? request = { index: index_name, id: self.id, body: { doc: attributes } } client.update(request.merge!()) end 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)
448 449 450 451 452 453 454 |
# File 'lib/elasticsearch/model/indexing.rb', line 448 def update_document_attributes(attributes, ={}) request = { index: index_name, id: self.id, body: { doc: attributes } } client.update(request.merge!()) end |