From d55dfe54f8804a228c4130c1c8a5dd16e5ff23bf Mon Sep 17 00:00:00 2001 From: adamgeorgeson Date: Thu, 6 May 2021 12:29:40 +0100 Subject: [PATCH] Expose op_type to Index/IndexAlias classes --- CHANGELOG.md | 5 ++++ lib/elastic_search_framework/index.rb | 4 ++-- lib/elastic_search_framework/index_alias.rb | 4 ++-- lib/elastic_search_framework/version.rb | 2 +- .../index_alias_spec.rb | 24 +++++++++++++++---- spec/elastic_search_framework/index_spec.rb | 21 +++++++++++++--- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a6a8f3..3c65be5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v2.3.1 +Adds support for `op_type` to be supplied for `ElasticSearchFramework::Index#put_item` to allow control of PUT behaviour in create scenarios. +Adds support for `op_type` to be supplied for `ElasticSearchFramework::IndexAlias#put_item` to allow control of PUT behaviour in create scenarios. +See [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-index_.html#operation-type) for more information and accepted values. + ## v2.3.0 Adds support for `op_type` to be supplied for `ElasticSearchFramework::Repository#set` to allow control of PUT behaviour in create scenarios. See [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-index_.html#operation-type) for more information and accepted values. diff --git a/lib/elastic_search_framework/index.rb b/lib/elastic_search_framework/index.rb index 619f4ee..117dbad 100644 --- a/lib/elastic_search_framework/index.rb +++ b/lib/elastic_search_framework/index.rb @@ -172,8 +172,8 @@ def get_item(id:, type: 'default') repository.get(index: self, id: id, type: type) end - def put_item(type: 'default', item:) - repository.set(entity: item, index: self, type: type) + def put_item(type: 'default', item:, op_type: 'index') + repository.set(entity: item, index: self, type: type, op_type: op_type) end def delete_item(id:, type: 'default') diff --git a/lib/elastic_search_framework/index_alias.rb b/lib/elastic_search_framework/index_alias.rb index 92fbe2d..ae49ae9 100644 --- a/lib/elastic_search_framework/index_alias.rb +++ b/lib/elastic_search_framework/index_alias.rb @@ -127,9 +127,9 @@ def get_item(id:, type: "default") repository.get(index: self, id: id, type: type) end - def put_item(type: "default", item:) + def put_item(type: "default", item:, op_type: 'index') indexes.each do |index| - repository.set(entity: item, index: index[:klass], type: type) + repository.set(entity: item, index: index[:klass], type: type, op_type: op_type) end end diff --git a/lib/elastic_search_framework/version.rb b/lib/elastic_search_framework/version.rb index ac9a8c7..00df4e6 100644 --- a/lib/elastic_search_framework/version.rb +++ b/lib/elastic_search_framework/version.rb @@ -1,3 +1,3 @@ module ElasticSearchFramework - VERSION = '2.3.0' + VERSION = '2.3.1' end diff --git a/spec/elastic_search_framework/index_alias_spec.rb b/spec/elastic_search_framework/index_alias_spec.rb index 559509b..f42b4d3 100644 --- a/spec/elastic_search_framework/index_alias_spec.rb +++ b/spec/elastic_search_framework/index_alias_spec.rb @@ -125,10 +125,26 @@ i.number = 5 end end - it 'should call set on the repository for each index of the alias' do - expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex, type: type).once - expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex2, type: type).once - ExampleIndexAlias.put_item(type: type, item: item) + context 'without specifying op_type' do + it 'should call set on the repository for each index of the alias with default op_type (index)' do + expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex, type: type, op_type: 'index').once + expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex2, type: type, op_type: 'index').once + ExampleIndexAlias.put_item(type: type, item: item) + end + end + + context 'with specified op_type' do + it 'should call set on the repository for each index of the alias with supplied op_type (index)' do + expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex, type: type, op_type: 'index').once + expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex2, type: type, op_type: 'index').once + ExampleIndexAlias.put_item(type: type, item: item, op_type: 'index') + end + + it 'should call set on the repository for each index of the alias with supplied op_type (create)' do + expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex, type: type, op_type: 'create').once + expect(ExampleIndexAlias.repository).to receive(:set).with(entity: item, index: ExampleIndex2, type: type, op_type: 'create').once + ExampleIndexAlias.put_item(type: type, item: item, op_type: 'create') + end end end diff --git a/spec/elastic_search_framework/index_spec.rb b/spec/elastic_search_framework/index_spec.rb index 943ae6d..24c4626 100644 --- a/spec/elastic_search_framework/index_spec.rb +++ b/spec/elastic_search_framework/index_spec.rb @@ -301,9 +301,24 @@ i.number = 5 end end - it 'should call set on the repository' do - expect_any_instance_of(ElasticSearchFramework::Repository).to receive(:set).with(entity: item, index: ExampleIndex, type: type) - ExampleIndex.put_item(type: type, item: item) + + context 'without specifying op_type' do + it 'should call set on the repository with default op_type (index)' do + expect_any_instance_of(ElasticSearchFramework::Repository).to receive(:set).with(entity: item, index: ExampleIndex, op_type: 'index', type: type) + ExampleIndex.put_item(type: type, item: item) + end + end + + context 'with specified op_type' do + it 'should call set on the repository with supplied op_type (index)' do + expect_any_instance_of(ElasticSearchFramework::Repository).to receive(:set).with(entity: item, index: ExampleIndex, op_type: 'index', type: type) + ExampleIndex.put_item(type: type, item: item, op_type: 'index') + end + + it 'should call set on the repository with supplied op_type (create)' do + expect_any_instance_of(ElasticSearchFramework::Repository).to receive(:set).with(entity: item, index: ExampleIndex, op_type: 'create', type: type) + ExampleIndex.put_item(type: type, item: item, op_type: 'create') + end end end