elastic_search_framework 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39f01fc19a6b4b23c55c54b9c59441ebc4f565db4365096c00031b8e0d28bd46
4
- data.tar.gz: 9a050c4f8a88f6faf6348e01c987999e6fe20cac33058c07b66e11b267641d21
3
+ metadata.gz: ab871a0e3beb296aaf8fbebc1c5c558c235fdbfd6a651f68ed74b7ace457da33
4
+ data.tar.gz: a19a5887d13c308852540302b805243eea991886cb72fe3384a36cc34c4f3ced
5
5
  SHA512:
6
- metadata.gz: 70dc6b3b99fb07405ecf51256393e5dc5534793960197a19f701f85eb28af5ad8fe53723089f009b3e6eecc26b0b26295a37c970c280463188dfdec5d478a23a
7
- data.tar.gz: 1c356af13948e1d11e3011075bd4095eb9fa6c410558708278aabf89c0d3c42e90403eb5a94fcb8d8801570b6cd94e6a3663fc42f15817c716270784482722c0
6
+ metadata.gz: 1f18004a91a131eced9812a7bd1049315c4800ed4afbbe5098bb0848a0b49157337b65da01d201b05524fbf915beb92d3f8d3396b990fb79ad6761c20ed620bb
7
+ data.tar.gz: 699835d74cf0c4eb6331cf336ab8191e76d0cdd33243a73f9c565647b3b74fd5a7d58c1a1ae914629d700b74a42b5e748e28b62f78cd46f215faeaa2822755a8
@@ -1,12 +1,12 @@
1
1
  module ElasticSearchFramework
2
2
  module Index
3
- def index(name:, shards: nil)
3
+ attr_accessor :index_settings
4
+
5
+ def index(name:)
4
6
  unless instance_variable_defined?(:@elastic_search_index_def)
5
- instance_variable_set(:@elastic_search_index_def, {
6
- name: "#{name}", shards: shards
7
- })
7
+ instance_variable_set(:@elastic_search_index_def, name: "#{name}")
8
8
  else
9
- raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name} | Shards: #{shards}.")
9
+ raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index description. Name: #{name}.")
10
10
  end
11
11
  end
12
12
 
@@ -41,8 +41,7 @@ module ElasticSearchFramework
41
41
  ElasticSearchFramework.logger.debug { "[#{self.class}] - Index already exists."}
42
42
  return
43
43
  end
44
-
45
- payload = create_payload(description: description, mappings: mappings)
44
+ payload = create_payload
46
45
 
47
46
  put(payload: payload)
48
47
  end
@@ -101,27 +100,28 @@ module ElasticSearchFramework
101
100
  is_valid_response?(response.code) || Integer(response.code) == 404
102
101
  end
103
102
 
104
- def create_payload(description:, mappings:)
105
- payload = {}
103
+ def settings(name:, type: nil, value:)
104
+ self.index_settings = {} if index_settings.nil?
105
+ index_settings[name] = if type
106
+ { type => value }
107
+ else
108
+ value
109
+ end
110
+ end
106
111
 
107
- if description[:shards] != nil
108
- payload[:settings] = {
109
- number_of_shards: Integer(description[:shards])
110
- }
111
- end
112
+ def create_payload
113
+ payload = { }
114
+ payload[:settings] = index_settings unless index_settings.nil?
112
115
 
113
- if mappings.keys.length > 0
116
+ unless mappings.keys.empty?
114
117
  payload[:mappings] = {}
115
118
 
116
119
  mappings.keys.each do |name|
117
- payload[:mappings][name] = {
118
- properties: {}
119
- }
120
+ payload[:mappings][name] = { properties: {} }
120
121
  mappings[name].keys.each do |field|
121
122
  payload[:mappings][name][:properties][field] = mappings[name][field]
122
123
  end
123
124
  end
124
-
125
125
  end
126
126
 
127
127
  payload
@@ -1,3 +1,3 @@
1
1
  module ElasticSearchFramework
2
- VERSION = '1.4.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -5,7 +5,6 @@ RSpec.describe ElasticSearchFramework::Index do
5
5
  expect(ExampleIndex.description[:name]).to eq 'example_index'
6
6
  expect(ExampleIndex.description[:id]).to eq :id
7
7
  expect(ExampleIndexWithId.description[:id]).to eq :number
8
- expect(ExampleIndexWithShard.description[:shards]).to eq 1
9
8
  end
10
9
  end
11
10
 
@@ -16,7 +15,7 @@ RSpec.describe ElasticSearchFramework::Index do
16
15
  it 'raises an index error' do
17
16
  expect { ExampleIndex.index(name: 'test') }.to raise_error(
18
17
  ElasticSearchFramework::Exceptions::IndexError,
19
- "[Class] - Duplicate index description. Name: test | Shards: ."
18
+ '[Class] - Duplicate index description. Name: test.'
20
19
  )
21
20
  end
22
21
  end
@@ -64,6 +63,37 @@ RSpec.describe ElasticSearchFramework::Index do
64
63
  end
65
64
  end
66
65
 
66
+ describe '#analysis' do
67
+ context 'when analysis is nil' do
68
+ before { ExampleIndex.delete if ExampleIndex.exists? }
69
+
70
+ it 'does not add analysis to the index' do
71
+ ExampleIndex.create
72
+ expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'analysis')).to be_nil
73
+ end
74
+ end
75
+
76
+ context 'when analysis is not nil' do
77
+ before { ExampleIndexWithSettings.delete if ExampleIndexWithSettings.exists? }
78
+ let(:expected) do
79
+ {
80
+ 'normalizer' => {
81
+ 'custom_normalizer' => {
82
+ 'filter' => ['lowercase'],
83
+ 'type' => 'custom'
84
+ }
85
+ }
86
+ }
87
+ end
88
+
89
+ it 'adds analysis to the index' do
90
+ ExampleIndexWithSettings.create
91
+ expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'number_of_shards')).to eq('1')
92
+ expect(ExampleIndexWithSettings.get.dig('example_index', 'settings', 'index', 'analysis')).to eq(expected)
93
+ end
94
+ end
95
+ end
96
+
67
97
  describe '#valid?' do
68
98
  context 'for a valid index definition' do
69
99
  it 'should return true' do
@@ -4,7 +4,6 @@ class ExampleIndex
4
4
  index name: 'example_index'
5
5
 
6
6
  mapping name: 'default', field: :name, type: :keyword, index: true
7
-
8
7
  end
9
8
 
10
9
  class ExampleIndexWithId
@@ -15,16 +14,18 @@ class ExampleIndexWithId
15
14
  id :number
16
15
 
17
16
  mapping name: 'default', field: :name, type: :keyword, index: true
18
-
19
17
  end
20
18
 
21
- class ExampleIndexWithShard
19
+ class ExampleIndexWithSettings
22
20
  extend ElasticSearchFramework::Index
23
21
 
24
- index name: 'example_index', shards: 1
22
+ index name: 'example_index'
25
23
 
26
- mapping name: 'default', field: :name, type: :keyword, index: true
24
+ normalizer_value = { custom_normalizer: { type: 'custom', char_filter: [], filter: ['lowercase'] } }
27
25
 
26
+ settings name: :number_of_shards, value: 1
27
+ settings name: :analysis, type: :normalizer, value: normalizer_value
28
+ mapping name: 'default', field: :name, type: :keyword, index: true
28
29
  end
29
30
 
30
31
  class InvalidExampleIndex
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_search_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-25 00:00:00.000000000 Z
11
+ date: 2018-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler