Module: Elasticsearch::Model::Indexing::InstanceMethods

Defined in:
lib/elasticsearch/model/indexing.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/elasticsearch/model/indexing.rb', line 250

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

Examples:

Delete a record


@article.__elasticsearch__.delete_document
2013-11-20 16:27:00 +0100: DELETE http://localhost:9200/articles/article/1

Parameters:

  • options (Hash) (defaults to: {})

    Optional arguments for passing to the client

Returns:

  • (Hash)

    The response from Elasticsearch

See Also:



303
304
305
306
307
308
309
# File 'lib/elasticsearch/model/indexing.rb', line 303

def delete_document(options={})
  client.delete(
    { index: index_name,
      type:  document_type,
      id:    self.id }.merge(options)
  )
end

#index_document(options = {}) ⇒ Hash

Serializes the model instance into JSON (by calling ‘as_indexed_json`), and saves the document into the Elasticsearch index.

Examples:

Index a record


@article.__elasticsearch__.index_document
2013-11-20 16:25:57 +0100: PUT http://localhost:9200/articles/article/1 ...

Parameters:

  • options (Hash) (defaults to: {})

    Optional arguments for passing to the client

Returns:

  • (Hash)

    The response from Elasticsearch

See Also:



279
280
281
282
283
284
285
286
287
288
# File 'lib/elasticsearch/model/indexing.rb', line 279

def index_document(options={})
  document = self.as_indexed_json

  client.index(
    { index: index_name,
      type:  document_type,
      id:    self.id,
      body:  document }.merge(options)
  )
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.

Examples:

Update a document corresponding to the record


@article = Article.first
@article.update_attribute :title, 'Updated'
# SQL (0.3ms)  UPDATE "articles" SET "title" = ?...

@article.__elasticsearch__.update_document
# 2013-11-20 17:00:05 +0100: POST http://localhost:9200/articles/article/1/_update ...
# 2013-11-20 17:00:05 +0100: > {"doc":{"title":"Updated"}}

Parameters:

  • options (Hash) (defaults to: {})

    Optional arguments for passing to the client

Returns:

  • (Hash)

    The response from Elasticsearch

See Also:



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/elasticsearch/model/indexing.rb', line 335

def update_document(options={})
  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(options)
    )
  else
    index_document(options)
  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)

Examples:

Update the ‘title` attribute


@article = Article.first
@article.title = "New title"
@article.__elasticsearch__.update_document_attributes title: "New title"

Parameters:

  • attributes (Hash)

    Attributes to be updated

  • options (Hash) (defaults to: {})

    Optional arguments for passing to the client

Returns:

  • (Hash)

    The response from Elasticsearch



368
369
370
371
372
373
374
375
# File 'lib/elasticsearch/model/indexing.rb', line 368

def update_document_attributes(attributes, options={})
  client.update(
    { index: index_name,
      type:  document_type,
      id:    self.id,
      body:  { doc: attributes } }.merge(options)
  )
end