Skip to content

Commit 266b59c

Browse files
jonbwhiteestolfo
authored andcommitted
Only execute update if document attributes is not empty (#862)
* Only execute update if document attributes is not empty * Adds test for skipping empty elasticsearch update
1 parent 9c40f63 commit 266b59c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def update_document(options={})
426426
type: document_type,
427427
id: self.id,
428428
body: { doc: attributes } }.merge(options)
429-
)
429+
) unless attributes.empty?
430430
else
431431
index_document(options)
432432
end

elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,10 @@ def changes
440440
context 'when changes are present' do
441441

442442
before do
443-
expect(instance).to receive(:client).and_return(client)
444-
expect(instance).to receive(:index_name).and_return('foo')
445-
expect(instance).to receive(:document_type).and_return('bar')
446-
expect(instance).to receive(:id).and_return('1')
443+
allow(instance).to receive(:client).and_return(client)
444+
allow(instance).to receive(:index_name).and_return('foo')
445+
allow(instance).to receive(:document_type).and_return('bar')
446+
allow(instance).to receive(:id).and_return('1')
447447
end
448448

449449
context 'when the changes are included in the as_indexed_json representation' do
@@ -474,6 +474,21 @@ def changes
474474
end
475475
end
476476

477+
context 'when none of the changes are included in the as_indexed_json representation' do
478+
479+
let(:instance) do
480+
DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
481+
end
482+
483+
before do
484+
instance.instance_variable_set(:@__changed_model_attributes, {'bar' => 'D' })
485+
end
486+
487+
it 'does not update the document' do
488+
expect(instance.update_document).to_not be(true)
489+
end
490+
end
491+
477492
context 'when there are partial updates' do
478493

479494
let(:instance) do

0 commit comments

Comments
 (0)