Skip to content

Commit b72a07d

Browse files
meesterdudekarmi
authored andcommitted
[MODEL] Added the update_document_attributes method
Allows to directly perform a _partial_ update of a document, without consideration for the changed attributes (as is the case in `update_document`) Example: @Article = Article.first @article.title = "New title" @article.__elasticsearch__.update_document_attributes({title: "New title"}) Closes elastic#230
1 parent 4e1a9f3 commit b72a07d

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+23
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,29 @@ def update_document(options={})
351351
index_document(options)
352352
end
353353
end
354+
355+
# Allows for partial update of a document by manually supplying the changes
356+
#
357+
# @param attributes [Hash] Required attributes to be updated
358+
# @param options [Hash] Optional arguments for passing to the client
359+
#
360+
# @example update just the title
361+
#
362+
# @article = Article.first
363+
# @article.title = "New title"
364+
# @article.__elasticsearch__.update_document_attributes({title: "New title"})
365+
#
366+
# @return [Hash] The response from Elasticsearch
367+
#
368+
#
369+
def update_document_attributes(attributes,options={})
370+
client.update(
371+
{ index: index_name,
372+
type: document_type,
373+
id: self.id,
374+
body: { doc: attributes } }.merge(options)
375+
)
376+
end
354377
end
355378

356379
end

elasticsearch-model/test/integration/active_record_basic_test.rb

+13
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ class ::Article < ActiveRecord::Base
128128
assert_equal 1, response.records.size
129129
end
130130

131+
should "allow specific updates to be made to the document directly" do
132+
article = Article.first
133+
134+
article.update_document_attributes({title: 'green grass'})
135+
136+
Article.__elasticsearch__.refresh_index!
137+
138+
response = Article.search 'title:green'
139+
140+
assert_equal 1, response.results.size
141+
assert_equal 1, response.records.size
142+
end
143+
131144
should "return results for a DSL search" do
132145
response = Article.search query: { match: { title: { query: 'test' } } }
133146

elasticsearch-model/test/unit/indexing_test.rb

+40
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,46 @@ def as_indexed_json(options={})
323323

324324
instance.update_document
325325
end
326+
327+
should "have the update_document_attributes method" do
328+
client = mock('client')
329+
instance = ::DummyIndexingModelWithCallbacks.new
330+
331+
client.expects(:update).with do |payload|
332+
assert_equal 'foo', payload[:index]
333+
assert_equal 'bar', payload[:type]
334+
assert_equal '1', payload[:id]
335+
end
336+
337+
instance.expects(:client).returns(client)
338+
instance.expects(:index_name).returns('foo')
339+
instance.expects(:document_type).returns('bar')
340+
instance.expects(:id).returns('1')
341+
342+
instance.update_document_attributes({})
343+
end
344+
345+
should "update only the supplied attributes" do
346+
client = mock('client')
347+
instance = ::DummyIndexingModelWithCallbacks.new
348+
349+
# Set the fake `changes` hash
350+
instance.instance_variable_set(:@__changed_attributes, {foo: 'bar'})
351+
352+
client.expects(:update).with do |payload|
353+
assert_equal 'foo', payload[:index]
354+
assert_equal 'bar', payload[:type]
355+
assert_equal '1', payload[:id]
356+
assert_equal({title: 'green'}, payload[:body][:doc])
357+
end
358+
359+
instance.expects(:client).returns(client)
360+
instance.expects(:index_name).returns('foo')
361+
instance.expects(:document_type).returns('bar')
362+
instance.expects(:id).returns('1')
363+
364+
instance.update_document_attributes({title: "green"})
365+
end
326366
end
327367

328368
context "Re-creating the index" do

0 commit comments

Comments
 (0)