Skip to content

Commit

Permalink
Merge pull request #13 from Sage/add_method_for_analysis_objects
Browse files Browse the repository at this point in the history
Add the ability to provide analysis objects when creating your index
  • Loading branch information
philearley authored May 31, 2018
2 parents 999bb08 + ecd53a4 commit d19329f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
38 changes: 19 additions & 19 deletions lib/elastic_search_framework/index.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/elastic_search_framework/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ElasticSearchFramework
VERSION = '1.4.0'
VERSION = '2.0.0'
end
34 changes: 32 additions & 2 deletions spec/elastic_search_framework/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions spec/example_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class ExampleIndex
index name: 'example_index'

mapping name: 'default', field: :name, type: :keyword, index: true

end

class ExampleIndexWithId
Expand All @@ -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
Expand Down

0 comments on commit d19329f

Please sign in to comment.