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

Included in:
Class
Defined in:
lib/elasticsearch/persistence/repository/store.rb

Overview

Save and delete documents in Elasticsearch

Instance Method Summary collapse

Instance Method Details

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

Remove the serialized object or document with specified ID from Elasticsearch

Examples:

Remove the document with ID 1


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

Returns:

  • (Hash)

    The response from Elasticsearch



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/elasticsearch/persistence/repository/store.rb', line 79

def delete(document, options={})
  if document.is_a?(String) || document.is_a?(Integer)
    id   = document
    type = document_type || __get_type_from_class(klass)
  else
    serialized = serialize(document)
    id   = __get_id_from_document(serialized)
    type = document_type || __get_type_from_class(klass || document.class)
  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"=>"...", "_type"=>"...", "_id"=>"...", "_version"=>1, "created"=>true}

Returns:

  • (Hash)

    The response from Elasticsearch



17
18
19
20
21
# File 'lib/elasticsearch/persistence/repository/store.rb', line 17

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

#update(document, 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"=>"...", "_type"=>"...", "_id"=>"1", "_version"=>2}

Update the document with a script


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

Returns:

  • (Hash)

    The response from Elasticsearch



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/elasticsearch/persistence/repository/store.rb', line 37

def update(document, options={})
  case
    when document.is_a?(String) || document.is_a?(Integer)
      id = document
    when document.respond_to?(:to_hash)
      serialized = document.to_hash
      id = __extract_id_from_document(serialized)
    else
      raise ArgumentError, "Expected a document ID or a Hash-like object, #{document.class} given"
  end

  type = options.delete(:type) || \
         (defined?(serialized) && serialized && serialized.delete(:type)) || \
         document_type || \
         __get_type_from_class(klass)

  if defined?(serialized) && serialized
    body = if serialized[:script]
               serialized.select { |k, v| [:script, :params, :upsert].include? k }
             else
               { doc: serialized }
           end
  else
    body = {}
    body.update( doc: options.delete(:doc)) if options[:doc]
    body.update( script: options.delete(:script)) if options[:script]
    body.update( params: options.delete(:params)) if options[:params]
    body.update( upsert: options.delete(:upsert)) if options[:upsert]
  end

  client.update( { index: index_name, id: id, body: body }.merge(options) )
end