diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f5cf72..a0b00ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,20 @@ +## v2.0.0 +### Breaking change + +The index method no longer accepts shards as a parameter. +This change follows the implementation of a new settings method allowing you to pass any number of settings objects to build out the index. + +Where the index method is currently being used as follows +``` +index name: 'index_name', shards: 1 +``` + +This should now look as follows. +``` +index name 'index_name' +settings name: :number_of_shards, value: 1 +``` + ## v1.4.0 * Resolve JSON query to work with ES6 by adding the request content type. * Make the mapping method more flexible to allow more than just type and index to be passed for the mapping parameters/options. diff --git a/lib/elastic_search_framework/index.rb b/lib/elastic_search_framework/index.rb index afaf4b1..3fa52b1 100644 --- a/lib/elastic_search_framework/index.rb +++ b/lib/elastic_search_framework/index.rb @@ -1,12 +1,12 @@ module ElasticSearchFramework module Index - def index(name:, shards: nil) + attr_accessor :index_settings + + def index(name:) unless instance_variable_defined?(:@elastic_search_index_def) - instance_variable_set(:@elastic_search_index_def, { - name: "#{name}", shards: shards - }) + instance_variable_set(:@elastic_search_index_def, name: "#{name}") else - raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name} | Shards: #{shards}.") + raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name}.") end end @@ -41,8 +41,7 @@ def create ElasticSearchFramework.logger.debug { "[#{self.class}] - Index already exists."} return end - - payload = create_payload(description: description, mappings: mappings) + payload = create_payload put(payload: payload) end @@ -101,27 +100,28 @@ def delete is_valid_response?(response.code) || Integer(response.code) == 404 end - def create_payload(description:, mappings:) - payload = {} + def settings(name:, type: nil, value:) + self.index_settings = {} if index_settings.nil? + index_settings[name] = if type + { type => value } + else + value + end + end - if description[:shards] != nil - payload[:settings] = { - number_of_shards: Integer(description[:shards]) - } - end + def create_payload + payload = { } + payload[:settings] = index_settings unless index_settings.nil? - if mappings.keys.length > 0 + unless mappings.keys.empty? payload[:mappings] = {} mappings.keys.each do |name| - payload[:mappings][name] = { - properties: {} - } + payload[:mappings][name] = { properties: {} } mappings[name].keys.each do |field| payload[:mappings][name][:properties][field] = mappings[name][field] end end - end payload diff --git a/lib/elastic_search_framework/version.rb b/lib/elastic_search_framework/version.rb index d1af8bd..0528745 100644 --- a/lib/elastic_search_framework/version.rb +++ b/lib/elastic_search_framework/version.rb @@ -1,3 +1,3 @@ module ElasticSearchFramework - VERSION = '1.4.0' + VERSION = '2.0.0' end diff --git a/spec/elastic_search_framework/index_spec.rb b/spec/elastic_search_framework/index_spec.rb index c188a9d..a4679d2 100644 --- a/spec/elastic_search_framework/index_spec.rb +++ b/spec/elastic_search_framework/index_spec.rb @@ -5,7 +5,6 @@ expect(ExampleIndex.description[:name]).to eq 'example_index' expect(ExampleIndex.description[:id]).to eq :id expect(ExampleIndexWithId.description[:id]).to eq :number - expect(ExampleIndexWithShard.description[:shards]).to eq 1 end end @@ -16,7 +15,7 @@ it 'raises an index error' do expect { ExampleIndex.index(name: 'test') }.to raise_error( ElasticSearchFramework::Exceptions::IndexError, - "[Class] - Duplicate index description. Name: test | Shards: ." + '[Class] - Duplicate index description. Name: test.' ) end end @@ -64,6 +63,37 @@ end end + describe '#analysis' do + context 'when analysis is nil' do + before { ExampleIndex.delete if ExampleIndex.exists? } + + it 'does not add analysis to the index' do + ExampleIndex.create + expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'analysis')).to be_nil + end + end + + context 'when analysis is not nil' do + before { ExampleIndexWithSettings.delete if ExampleIndexWithSettings.exists? } + let(:expected) do + { + 'normalizer' => { + 'custom_normalizer' => { + 'filter' => ['lowercase'], + 'type' => 'custom' + } + } + } + end + + it 'adds analysis to the index' do + ExampleIndexWithSettings.create + expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'number_of_shards')).to eq('1') + expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'analysis')).to eq(expected) + end + end + end + describe '#valid?' do context 'for a valid index definition' do it 'should return true' do diff --git a/spec/example_index.rb b/spec/example_index.rb index f458e59..0c4fcb4 100644 --- a/spec/example_index.rb +++ b/spec/example_index.rb @@ -4,7 +4,6 @@ class ExampleIndex index name: 'example_index' mapping name: 'default', field: :name, type: :keyword, index: true - end class ExampleIndexWithId @@ -15,16 +14,18 @@ class ExampleIndexWithId id :number mapping name: 'default', field: :name, type: :keyword, index: true - end -class ExampleIndexWithShard +class ExampleIndexWithSettings extend ElasticSearchFramework::Index - index name: 'example_index', shards: 1 + index name: 'example_index' - mapping name: 'default', field: :name, type: :keyword, index: true + normalizer_value = { custom_normalizer: { type: 'custom', char_filter: [], filter: ['lowercase'] } } + settings name: :number_of_shards, value: 1 + settings name: :analysis, type: :normalizer, value: normalizer_value + mapping name: 'default', field: :name, type: :keyword, index: true end class InvalidExampleIndex