Module: Elasticsearch::Persistence::Repository::Store

Included in:
Elasticsearch::Persistence::Repository
Defined in:
lib/elasticsearch/persistence/repository/store.rb

Overview

Save and delete documents in Elasticsearch

Since:

  • 6.0.0

Instance Method Summary collapse

Instance Method Details

#delete(document_or_id, options = {}) ⇒ Hash

Remove the serialized object or document with specified ID from Elasticsearch

Examples:

Remove the document with ID 1


repository.delete(1)
# => {"_index"=>"...", "_id"=>"1", "_version"=>4}

Parameters:

  • document_or_id (Object)

    The document to delete or the id of the document to delete.

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

    The delete request options.

Returns:

  • (Hash)

    The response from Elasticsearch

Since:

  • 6.0.0



91
92
93
94
95
96
97
98
99
# File 'lib/elasticsearch/persistence/repository/store.rb', line 91

def delete(document_or_id, options = {})
  if document_or_id.is_a?(String) || document_or_id.is_a?(Integer)
    id = document_or_id
  else
    serialized = serialize(document_or_id)
    id = __get_id_from_document(serialized)
  end
  client.delete({ index: index_name, id: id }.merge(options))
end

#save(document, options = {}) ⇒ Hash

Store the serialized object in Elasticsearch

Examples:

repository.save(myobject)
=> {"_index"=>"...", "_id"=>"...", "_version"=>1, "created"=>true}

Parameters:

  • document (Object)

    The document to save into Elasticsearch.

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

    The save request options.

Returns:

  • (Hash)

    The response from Elasticsearch

Since:

  • 6.0.0



37
38
39
40
41
42
43
44
# File 'lib/elasticsearch/persistence/repository/store.rb', line 37

def save(document, options={})
  serialized = serialize(document)
  id = __get_id_from_document(serialized)
  request = { index: index_name,
              id: id,
              body: serialized }
  client.index(request.merge(options))
end

#update(document_or_id, options = {}) ⇒ Hash

Update the serialized object in Elasticsearch with partial data or script

Examples:

Update the document with partial data


repository.update id: 1, title: 'UPDATED',  tags: []
# => {"_index"=>"...", "_id"=>"1", "_version"=>2}

Update the document with a script


repository.update 1, script: 'ctx._source.views += 1'
# => {"_index"=>"...", "_id"=>"1", "_version"=>3}

Parameters:

  • document_or_id (Object)

    The document to update or the id of the document to update.

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

    The update request options.

Returns:

  • (Hash)

    The response from Elasticsearch

Since:

  • 6.0.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/elasticsearch/persistence/repository/store.rb', line 63

def update(document_or_id, options = {})
  if document_or_id.is_a?(String) || document_or_id.is_a?(Integer)
    id = document_or_id
    body = options
  else
    document = serialize(document_or_id)
    id = __extract_id_from_document(document)
    if options[:script]
      body = options
    else
      body = { doc: document }.merge(options)
    end
  end
  client.update(index: index_name, id: id, body: body)
end