Module: Elasticsearch::Persistence::Model::Store::InstanceMethods

Defined in:
lib/elasticsearch/persistence/model/store.rb

Instance Method Summary collapse

Instance Method Details

#decrement(attribute, value = 1, options = {}) ⇒ Hash

Decrements a numeric attribute (via Elasticsearch’s “Update” API) and returns the response

Examples:

Decrement the ‘salary` attribute by 1


p.decrement :salary

Decrement the ‘salary` attribute by 100


p.decrement :salary, 100

Returns:

  • (Hash)

    The Elasticsearch response as a Hash

Raises:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/elasticsearch/persistence/model/store.rb', line 165

def decrement(attribute, value=1, options={})
  raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?

  options.update index: self._index if self._index
  options.update type:  self._type  if self._type

  response = self.class.gateway.update(self.id, { script: "ctx._source.#{attribute} = ctx._source.#{attribute} - #{value}"}.merge(options))
  self[attribute] -= value

  @_index    = response['_index']
  @_type     = response['_type']
  @_version  = response['_version']

  response
end

#destroy(options = {}) ⇒ Hash Also known as: delete

Deletes the model from Elasticsearch (if it’s persisted), freezes it, and returns the response

Examples:

Delete a model instance


p.destroy
=> {"_index"=>"people", ... "_id"=>"RzFSXFR0R8u1CZIWNs2Gvg", "_version"=>2 ...}

Returns:

  • (Hash)

    The Elasticsearch response as a Hash

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/elasticsearch/persistence/model/store.rb', line 79

def destroy(options={})
  raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?

  run_callbacks :destroy do
    options.update index: self._index if self._index
    options.update type:  self._type  if self._type

    response = self.class.gateway.delete(self.id, options)

    @destroyed = true
    @persisted = false
    self.freeze
    response
  end
end

#destroyed?TrueClass, FalseClass

Returns true when the model has been destroyed, false otherwise

Returns:

  • (TrueClass, FalseClass)


218
219
220
# File 'lib/elasticsearch/persistence/model/store.rb', line 218

def destroyed?
  !!@destroyed
end

#increment(attribute, value = 1, options = {}) ⇒ Hash

Increments a numeric attribute (via Elasticsearch’s “Update” API) and returns the response

Examples:

Increment the ‘salary` attribute by 1


p.increment :salary

Increment the ‘salary` attribute by 100


p.increment :salary, 100

Returns:

  • (Hash)

    The Elasticsearch response as a Hash

Raises:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/elasticsearch/persistence/model/store.rb', line 136

def increment(attribute, value=1, options={})
  raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?

  options.update index: self._index if self._index
  options.update type:  self._type  if self._type

  response = self.class.gateway.update(self.id, { script: "ctx._source.#{attribute} += #{value}"}.merge(options))

  self[attribute] += value

  @_index    = response['_index']
  @_type     = response['_type']
  @_version  = response['_version']

  response
end

#new_record?TrueClass, FalseClass

Returns true when the model has not been saved yet, false otherwise

Returns:

  • (TrueClass, FalseClass)


234
235
236
# File 'lib/elasticsearch/persistence/model/store.rb', line 234

def new_record?
  !persisted? && !destroyed?
end

#persisted?TrueClass, FalseClass

Returns true when the model has been already saved to the database, false otherwise

Returns:

  • (TrueClass, FalseClass)


226
227
228
# File 'lib/elasticsearch/persistence/model/store.rb', line 226

def persisted?
  !!@persisted && !destroyed?
end

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

Saves the model (if validations pass) and returns the response (or ‘false`)

Examples:

Save a valid model instance


p = Person.new(name: 'John')
p.save
=> {"_index"=>"people", ... "_id"=>"RzFSXFR0R8u1CZIWNs2Gvg", "_version"=>1, "created"=>true}

Save an invalid model instance


p = Person.new(name: nil)
p.save
# => false

Returns:

  • (Hash, FalseClass)

    The Elasticsearch response as a Hash or ‘false`



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/model/store.rb', line 46

def save(options={})
  unless options.delete(:validate) == false
    return false unless valid?
  end

  run_callbacks :save do
    options.update id: self.id
    options.update index: self._index if self._index
    options.update type:  self._type  if self._type

    response = self.class.gateway.save(self, options)

    self[:updated_at] = Time.now.utc

    @_id       = response['_id']
    @_index    = response['_index']
    @_type     = response['_type']
    @_version  = response['_version']
    @persisted = true

    response
  end
end

#touch(attribute = :updated_at, options = {}) ⇒ Hash

Updates the ‘updated_at` attribute, saves the model and returns the response

Examples:

Update the ‘updated_at` attribute (default)


p.touch

Update a custom attribute: ‘saved_on`


p.touch :saved_on

Returns:

  • (Hash)

    The Elasticsearch response as a Hash

Raises:



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/elasticsearch/persistence/model/store.rb', line 193

def touch(attribute=:updated_at, options={})
  raise DocumentNotPersisted, "Object not persisted: #{self.inspect}"  unless persisted?
  raise ArgumentError, "Object does not have '#{attribute}' attribute" unless respond_to?(attribute)

  run_callbacks :touch do
    options.update index: self._index if self._index
    options.update type:  self._type  if self._type

    value = Time.now.utc
    response = self.class.gateway.update(self.id, { doc: { attribute => value.iso8601 }}.merge(options))

    self[attribute] = value

    @_index    = response['_index']
    @_type     = response['_type']
    @_version  = response['_version']

    response
  end
end

#update(attributes = {}, options = {}) ⇒ Hash Also known as: update_attributes

Updates the model (via Elasticsearch’s “Update” API) and returns the response

Examples:

Update a model with partial attributes


p.update name: 'UPDATED'
=> {"_index"=>"people", ... "_version"=>2}

Returns:

  • (Hash)

    The Elasticsearch response as a Hash

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/elasticsearch/persistence/model/store.rb', line 104

def update(attributes={}, options={})
  raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?

  run_callbacks :update do
    options.update index: self._index if self._index
    options.update type:  self._type  if self._type

    attributes.update( { updated_at: Time.now.utc } )

    response = self.class.gateway.update(self.id, { doc: attributes}.merge(options))

    self.attributes = self.attributes.merge(attributes)
    @_index    = response['_index']
    @_type     = response['_type']
    @_version  = response['_version']

    response
  end
end