Skip to content

Commit bb1fc82

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 d304856 commit bb1fc82

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
@@ -409,7 +409,7 @@ def update_document(options={})
409409
type: document_type,
410410
id: self.id,
411411
body: { doc: attributes } }.merge(options)
412-
)
412+
) unless attributes.empty?
413413
else
414414
index_document(options)
415415
end

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,10 @@ def changes
423423
context 'when changes are present' do
424424

425425
before do
426-
expect(instance).to receive(:client).and_return(client)
427-
expect(instance).to receive(:index_name).and_return('foo')
428-
expect(instance).to receive(:document_type).and_return('bar')
429-
expect(instance).to receive(:id).and_return('1')
426+
allow(instance).to receive(:client).and_return(client)
427+
allow(instance).to receive(:index_name).and_return('foo')
428+
allow(instance).to receive(:document_type).and_return('bar')
429+
allow(instance).to receive(:id).and_return('1')
430430
end
431431

432432
context 'when the changes are included in the as_indexed_json representation' do
@@ -457,6 +457,21 @@ def changes
457457
end
458458
end
459459

460+
context 'when none of the changes are included in the as_indexed_json representation' do
461+
462+
let(:instance) do
463+
DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
464+
end
465+
466+
before do
467+
instance.instance_variable_set(:@__changed_model_attributes, {'bar' => 'D' })
468+
end
469+
470+
it 'does not update the document' do
471+
expect(instance.update_document).to_not be(true)
472+
end
473+
end
474+
460475
context 'when there are partial updates' do
461476

462477
let(:instance) do

0 commit comments

Comments
 (0)