From 6060b7cc9583657fc5dca43972e1b24144ec7d58 Mon Sep 17 00:00:00 2001 From: Jon Whiteaker Date: Thu, 6 Dec 2018 12:57:58 -0800 Subject: [PATCH 1/2] Only execute update if document attributes is not empty --- elasticsearch-model/lib/elasticsearch/model/indexing.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/indexing.rb b/elasticsearch-model/lib/elasticsearch/model/indexing.rb index c4433ae7e..e3f598b81 100644 --- a/elasticsearch-model/lib/elasticsearch/model/indexing.rb +++ b/elasticsearch-model/lib/elasticsearch/model/indexing.rb @@ -426,7 +426,7 @@ def update_document(options={}) type: document_type, id: self.id, body: { doc: attributes } }.merge(options) - ) + ) unless attributes.empty? else index_document(options) end From c28c4c5ea7c9265ed651b90bfe5750c3393f627e Mon Sep 17 00:00:00 2001 From: Jon Whiteaker Date: Thu, 13 Dec 2018 13:58:17 -0800 Subject: [PATCH 2/2] Adds test for skipping empty elasticsearch update --- .../spec/elasticsearch/model/indexing_spec.rb | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb b/elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb index 15c8f05e8..cfb078e17 100644 --- a/elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb +++ b/elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb @@ -440,10 +440,10 @@ def changes context 'when changes are present' do before do - expect(instance).to receive(:client).and_return(client) - expect(instance).to receive(:index_name).and_return('foo') - expect(instance).to receive(:document_type).and_return('bar') - expect(instance).to receive(:id).and_return('1') + allow(instance).to receive(:client).and_return(client) + allow(instance).to receive(:index_name).and_return('foo') + allow(instance).to receive(:document_type).and_return('bar') + allow(instance).to receive(:id).and_return('1') end context 'when the changes are included in the as_indexed_json representation' do @@ -474,6 +474,21 @@ def changes end end + context 'when none of the changes are included in the as_indexed_json representation' do + + let(:instance) do + DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new + end + + before do + instance.instance_variable_set(:@__changed_model_attributes, {'bar' => 'D' }) + end + + it 'does not update the document' do + expect(instance.update_document).to_not be(true) + end + end + context 'when there are partial updates' do let(:instance) do