elastic_search_framework 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86c6e8a7207b86a434d1bfcb90760b09f0e402fe0ee5cc3da4be5403da4d9f07
4
- data.tar.gz: 2463823e5f08a1c24dac6e10d240646a60e2ead9f7bd31a9ff1a1c50a870b6d4
3
+ metadata.gz: 39f01fc19a6b4b23c55c54b9c59441ebc4f565db4365096c00031b8e0d28bd46
4
+ data.tar.gz: 9a050c4f8a88f6faf6348e01c987999e6fe20cac33058c07b66e11b267641d21
5
5
  SHA512:
6
- metadata.gz: 91f9f1fb76a537e8a8cc6631398e6776b26dc0fa59164ed97f017c650d4334e3eae36971f6c5324b7354087df0713badb2a52b8ef04c798d6105668070752935
7
- data.tar.gz: dcf4530566639e57b4cf7bd8c80a7230c1e2fdd19af5e58a2d5d6207cc50ccd98c920059e12ed89d1c222b5e7eb639166f19125ab7a741e993ee5fe22b4a9b73
6
+ metadata.gz: 70dc6b3b99fb07405ecf51256393e5dc5534793960197a19f701f85eb28af5ad8fe53723089f009b3e6eecc26b0b26295a37c970c280463188dfdec5d478a23a
7
+ data.tar.gz: 1c356af13948e1d11e3011075bd4095eb9fa6c410558708278aabf89c0d3c42e90403eb5a94fcb8d8801570b6cd94e6a3663fc42f15817c716270784482722c0
@@ -18,26 +18,21 @@ module ElasticSearchFramework
18
18
  end
19
19
  end
20
20
 
21
- def mapping(name:, field:, type:, index:)
22
-
21
+ def mapping(name:, field:, **options)
23
22
  unless instance_variable_defined?(:@elastic_search_index_mappings)
24
23
  instance_variable_set(:@elastic_search_index_mappings, {})
25
24
  end
26
25
 
27
26
  mappings = instance_variable_get(:@elastic_search_index_mappings)
28
27
 
29
- if mappings[name] == nil
30
- mappings[name] = {}
31
- end
28
+ mappings[name] = {} if mappings[name].nil?
32
29
 
33
- mappings[name][field] = { type: type, index: index}
30
+ mappings[name][field] = options
34
31
 
35
32
  instance_variable_set(:@elastic_search_index_mappings, mappings)
36
-
37
33
  end
38
34
 
39
35
  def create
40
-
41
36
  if !valid?
42
37
  raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Invalid Index description specified.")
43
38
  end
@@ -50,7 +45,6 @@ module ElasticSearchFramework
50
45
  payload = create_payload(description: description, mappings: mappings)
51
46
 
52
47
  put(payload: payload)
53
-
54
48
  end
55
49
 
56
50
  def put(payload:)
@@ -65,7 +59,7 @@ module ElasticSearchFramework
65
59
  end
66
60
 
67
61
  unless is_valid_response?(response.code)
68
- if response.body.dig(:error, :root_cause, 0, :type) == 'index_already_exists_exception'
62
+ if JSON.parse(response.body, symbolize_names: true).dig(:error, :root_cause, 0, :type) == 'index_already_exists_exception'
69
63
  # We get here because the `exists?` check in #create is non-atomic
70
64
  ElasticSearchFramework.logger.warn "[#{self.class}] - Failed to create preexisting index. | Response: #{response.body}"
71
65
  else
@@ -112,12 +106,11 @@ module ElasticSearchFramework
112
106
 
113
107
  if description[:shards] != nil
114
108
  payload[:settings] = {
115
- number_of_shards: Integer(description[:shards])
109
+ number_of_shards: Integer(description[:shards])
116
110
  }
117
111
  end
118
112
 
119
113
  if mappings.keys.length > 0
120
-
121
114
  payload[:mappings] = {}
122
115
 
123
116
  mappings.keys.each do |name|
@@ -125,10 +118,7 @@ module ElasticSearchFramework
125
118
  properties: {}
126
119
  }
127
120
  mappings[name].keys.each do |field|
128
- payload[:mappings][name][:properties][field] = {
129
- type: mappings[name][field][:type],
130
- index: mappings[name][field][:index]
131
- }
121
+ payload[:mappings][name][:properties][field] = mappings[name][field]
132
122
  end
133
123
  end
134
124
 
@@ -87,6 +87,7 @@ module ElasticSearchFramework
87
87
  uri = URI("#{host}/#{index_name}/#{type}/_search")
88
88
 
89
89
  request = Net::HTTP::Get.new(uri.request_uri)
90
+ request.content_type = 'application/json'
90
91
  request.body = json_query
91
92
 
92
93
  response = with_client do |client|
@@ -1,3 +1,3 @@
1
1
  module ElasticSearchFramework
2
- VERSION = '1.2.0'
2
+ VERSION = '1.4.0'
3
3
  end
@@ -1,5 +1,4 @@
1
1
  RSpec.describe ElasticSearchFramework::Index do
2
-
3
2
  describe '#description' do
4
3
  it 'should return the index details' do
5
4
  expect(ExampleIndex.description).to be_a(Hash)
@@ -10,6 +9,31 @@ RSpec.describe ElasticSearchFramework::Index do
10
9
  end
11
10
  end
12
11
 
12
+ describe '#index' do
13
+ before { ExampleIndex.create unless ExampleIndex.exists? }
14
+ context 'when the instance variable is not defined' do
15
+ before { allow(ExampleIndex).to receive(:instance_variable_defined?).and_return(true) }
16
+ it 'raises an index error' do
17
+ expect { ExampleIndex.index(name: 'test') }.to raise_error(
18
+ ElasticSearchFramework::Exceptions::IndexError,
19
+ "[Class] - Duplicate index description. Name: test | Shards: ."
20
+ )
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#id' do
26
+ context 'when the instance variable is not defined' do
27
+ before { allow(ExampleIndex).to receive(:instance_variable_defined?).and_return(true) }
28
+ it 'raises an index error' do
29
+ expect { ExampleIndex.id('name') }.to raise_error(
30
+ ElasticSearchFramework::Exceptions::IndexError,
31
+ "[Class] - Duplicate index id. Field: name."
32
+ )
33
+ end
34
+ end
35
+ end
36
+
13
37
  describe '#full_name' do
14
38
  let(:namespace) { 'uat' }
15
39
  let(:namespace_delimiter) { '.' }
@@ -20,6 +44,14 @@ RSpec.describe ElasticSearchFramework::Index do
20
44
  it 'should return the full index name including namespace and delimiter' do
21
45
  expect(ExampleIndex.full_name).to eq "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{ExampleIndex.description[:name]}"
22
46
  end
47
+
48
+ context 'when the namespace is nil' do
49
+ before { ElasticSearchFramework.namespace = nil }
50
+
51
+ it 'returns the description name downcased' do
52
+ expect(ExampleIndex.full_name).to eq 'example_index'
53
+ end
54
+ end
23
55
  end
24
56
 
25
57
  describe '#mapping' do
@@ -46,18 +78,40 @@ RSpec.describe ElasticSearchFramework::Index do
46
78
  end
47
79
 
48
80
  describe '#create' do
49
- before do
50
- if ExampleIndex.exists?
81
+ context 'when index is valid and does not exist' do
82
+ before { ExampleIndex.delete if ExampleIndex.exists? }
83
+
84
+ it 'should create an index' do
85
+ expect(ExampleIndex.exists?).to be false
86
+ ExampleIndex.create
87
+ expect(ExampleIndex.exists?).to be true
88
+ end
89
+
90
+ after do
51
91
  ExampleIndex.delete
52
92
  end
53
93
  end
54
- it 'should create an index' do
55
- expect(ExampleIndex.exists?).to be false
56
- ExampleIndex.create
57
- expect(ExampleIndex.exists?).to be true
94
+
95
+ context 'when index is not valid' do
96
+ before { allow(ExampleIndex).to receive(:valid?).and_return(false) }
97
+
98
+ it 'raises an error' do
99
+ expect(ExampleIndex.exists?).to be false
100
+ expect { ExampleIndex.create }.to raise_error(
101
+ ElasticSearchFramework::Exceptions::IndexError,
102
+ '[Class] - Invalid Index description specified.'
103
+ )
104
+ end
58
105
  end
59
- after do
60
- ExampleIndex.delete
106
+
107
+ context 'when index is valid but already exists' do
108
+ before { ExampleIndex.delete if ExampleIndex.exists? }
109
+
110
+ it 'does not try to create a new index' do
111
+ allow(ExampleIndex).to receive(:exists?).and_return(true)
112
+ expect(ExampleIndex).not_to receive(:put)
113
+ ExampleIndex.create
114
+ end
61
115
  end
62
116
  end
63
117
 
@@ -94,6 +148,48 @@ RSpec.describe ElasticSearchFramework::Index do
94
148
  end
95
149
  end
96
150
 
151
+ describe '#put' do
152
+ let(:payload) { {} }
153
+ context 'when there is a valid response' do
154
+ before { allow(ExampleIndex).to receive(:is_valid_response?).and_return(true) }
155
+
156
+ it 'returns true' do
157
+ ExampleIndex.create
158
+ expect(ExampleIndex.put(payload: payload)).to eq true
159
+ end
160
+ end
161
+
162
+ context 'when there is not a valid response' do
163
+ before { allow(ExampleIndex).to receive(:is_valid_response?).and_return(false) }
164
+
165
+ context 'when the error is "index_already_exists_exception"' do
166
+ let(:response_body) { { error: { root_cause: [{ type: 'index_already_exists_exception' }] } } }
167
+ let(:request) { double }
168
+
169
+ before { ExampleIndex.delete if ExampleIndex.exists? }
170
+ it 'returns true' do
171
+ allow(request).to receive(:body).and_return(response_body.to_json)
172
+ allow(request).to receive(:code).and_return(404)
173
+ allow_any_instance_of(Net::HTTP).to receive(:request).and_return(request)
174
+ expect(ExampleIndex.put(payload: payload)).to eq true
175
+ end
176
+ end
177
+
178
+ context 'when the error is not "index_already_exists_exception"' do
179
+ let(:response_body) { { error: { root_cause: [{ type: 'foo' }] } } }
180
+ let(:request) { double }
181
+ it 'raises an IndexError' do
182
+ allow(request).to receive(:body).and_return(response_body.to_json)
183
+ allow(request).to receive(:code).and_return(404)
184
+ allow_any_instance_of(Net::HTTP).to receive(:request).and_return(request)
185
+ expect { ExampleIndex.put(payload: payload) }.to raise_error(
186
+ ElasticSearchFramework::Exceptions::IndexError
187
+ )
188
+ end
189
+ end
190
+ end
191
+ end
192
+
97
193
  describe '#is_valid_response?' do
98
194
  let(:code) { 200 }
99
195
  context 'when a 200 response code is returned' do
@@ -182,5 +278,4 @@ RSpec.describe ElasticSearchFramework::Index do
182
278
  ExampleIndex.delete_item(id: id, type: type)
183
279
  end
184
280
  end
185
-
186
281
  end
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.2.0
4
+ version: 1.4.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-17 00:00:00.000000000 Z
11
+ date: 2018-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  version: '0'
150
150
  requirements: []
151
151
  rubyforge_project:
152
- rubygems_version: 2.7.6
152
+ rubygems_version: 2.7.7
153
153
  signing_key:
154
154
  specification_version: 4
155
155
  summary: A lightweight framework to for working with elastic search.