elastic_search_framework 1.1.0 → 1.2.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: 39a2a5f0c0573e1ce1ea95fe0206a05a2228bf2fb9e1a99f04e61f6fd64ced47
4
- data.tar.gz: 1ccf3eec2961929a4c042585131f2dffa16ddddac011536ee71b1d08582b33a3
3
+ metadata.gz: 86c6e8a7207b86a434d1bfcb90760b09f0e402fe0ee5cc3da4be5403da4d9f07
4
+ data.tar.gz: 2463823e5f08a1c24dac6e10d240646a60e2ead9f7bd31a9ff1a1c50a870b6d4
5
5
  SHA512:
6
- metadata.gz: 63890595521aa8816cef97190b39491d8f7f62d890e293f5311b354c9101f016dc9da630208cb6b8e0d90cf9e026b86a5e5f6390c787b02dc6c7ba03183be064
7
- data.tar.gz: a5429fd019a6cf5b6ad3606c44dc42380f49c4f07f6dec35b5abbeb2676e1095a15b681e2f92915dbd01c386acb24e9217a63a64a700657c2d0d8348232e4f8e
6
+ metadata.gz: 91f9f1fb76a537e8a8cc6631398e6776b26dc0fa59164ed97f017c650d4334e3eae36971f6c5324b7354087df0713badb2a52b8ef04c798d6105668070752935
7
+ data.tar.gz: dcf4530566639e57b4cf7bd8c80a7230c1e2fdd19af5e58a2d5d6207cc50ccd98c920059e12ed89d1c222b5e7eb639166f19125ab7a741e993ee5fe22b4a9b73
@@ -13,7 +13,7 @@ module ElasticSearchFramework
13
13
  client.request(request)
14
14
  end
15
15
 
16
- unless is_valid_response?(response.code)
16
+ unless valid_response?(response.code)
17
17
  raise ElasticSearchFramework::Exceptions::IndexError.new(
18
18
  "An error occurred setting an index document. Response: #{response.body} | Code: #{response.code}"
19
19
  )
@@ -30,7 +30,7 @@ module ElasticSearchFramework
30
30
  client.request(request)
31
31
  end
32
32
 
33
- if is_valid_response?(response.code)
33
+ if valid_response?(response.code)
34
34
  result = JSON.load(response.body)
35
35
  hash_helper.indifferent!(result)
36
36
  return result
@@ -52,7 +52,7 @@ module ElasticSearchFramework
52
52
  client.request(request)
53
53
  end
54
54
 
55
- if is_valid_response?(response.code) || Integer(response.code) == 404
55
+ if valid_response?(response.code) || Integer(response.code) == 404
56
56
  return true
57
57
  else
58
58
  raise ElasticSearchFramework::Exceptions::IndexError.new(
@@ -70,7 +70,7 @@ module ElasticSearchFramework
70
70
  client.request(request)
71
71
  end
72
72
 
73
- if is_valid_response?(response.code)
73
+ if valid_response?(response.code)
74
74
  result = JSON.parse(response.body)
75
75
  hash_helper.indifferent!(result)
76
76
  if count
@@ -83,6 +83,27 @@ module ElasticSearchFramework
83
83
  end
84
84
  end
85
85
 
86
+ def json_query(index_name:, json_query:, type: 'default')
87
+ uri = URI("#{host}/#{index_name}/#{type}/_search")
88
+
89
+ request = Net::HTTP::Get.new(uri.request_uri)
90
+ request.body = json_query
91
+
92
+ response = with_client do |client|
93
+ client.request(request)
94
+ end
95
+
96
+ if valid_response?(response.code)
97
+ result = JSON.parse(response.body)
98
+ return result['hits']
99
+ else
100
+ raise(
101
+ ElasticSearchFramework::Exceptions::IndexError,
102
+ "An error occurred executing an index query. Response: #{response.body}"
103
+ )
104
+ end
105
+ end
106
+
86
107
  def client
87
108
  @client ||= Net::HTTP.new(host_uri.host, host_uri.port).tap do |c|
88
109
  c.use_ssl = host_uri.scheme == 'https'
@@ -103,8 +124,8 @@ module ElasticSearchFramework
103
124
  @read_timeout ||= Integer(ENV['CONNECTION_OPEN_TIMEOUT'] || 1)
104
125
  end
105
126
 
106
- def is_valid_response?(status)
107
- [200,201,202].include?(Integer(status))
127
+ def valid_response?(status)
128
+ [200, 201, 202].include?(Integer(status))
108
129
  end
109
130
 
110
131
  def host
@@ -1,3 +1,3 @@
1
1
  module ElasticSearchFramework
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -121,6 +121,116 @@ RSpec.describe ElasticSearchFramework::Repository do
121
121
  end
122
122
  end
123
123
 
124
+ describe '#json_query' do
125
+ before do
126
+ ExampleIndex.delete
127
+ ExampleIndex.create
128
+
129
+ subject.set(index: ExampleIndex, entity: item1)
130
+ subject.set(index: ExampleIndex, entity: item2)
131
+ subject.set(index: ExampleIndex, entity: item3)
132
+ subject.set(index: ExampleIndex, entity: item4)
133
+ sleep 1
134
+ end
135
+
136
+ it 'returns the expected query results' do
137
+ result_query = {
138
+ 'query' => {
139
+ 'bool' => {
140
+ 'must' => {
141
+ 'match' => {
142
+ 'name' => 'fred'
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ results = subject.json_query(index_name: 'test_example_index', json_query: result_query.to_json)
149
+ expect(results['hits'].length).to eq 1
150
+ expect(results['hits'][0]['_source']['name']).to eq item1.name
151
+ expect(results['hits'][0]['_source']['id']).to eq item1.id
152
+ expect(results['hits'][0]['_source']['timestamp']).to eq item1.timestamp
153
+ expect(results['hits'][0]['_source']['number']).to eq item1.number
154
+
155
+ example_one = {
156
+ 'query' => {
157
+ 'range' => {
158
+ 'number' => {
159
+ 'gt' => 5
160
+ }
161
+ }
162
+ }
163
+ }
164
+ example_two = {
165
+ 'query' => {
166
+ 'range' => {
167
+ 'number' => {
168
+ 'gte' => 15
169
+ }
170
+ }
171
+ }
172
+ }
173
+ example_three = {
174
+ 'query' => {
175
+ 'bool' => {
176
+ 'must_not' => {
177
+ 'match' => {
178
+ 'name' => 'fred'
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+ results = subject.json_query(index_name: 'test_example_index', json_query: example_one.to_json)
185
+ expect(results['hits'].length).to eq 3
186
+
187
+ results = subject.json_query(index_name: 'test_example_index', json_query: example_two.to_json)
188
+ expect(results['hits'].length).to eq 2
189
+
190
+ results = subject.json_query(index_name: 'test_example_index', json_query: example_three.to_json)
191
+ expect(results['hits'].length).to eq 3
192
+ end
193
+
194
+ context 'when an error occurs' do
195
+ it 'raises an ElasticSearchFramework::Exceptions::IndexError' do
196
+ example = {
197
+ 'query' => {
198
+ 'bool' => {
199
+ 'must_not' => {
200
+ 'name' => 'fred'
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ response_body = {
207
+ 'error' => {
208
+ 'root_cause' => [
209
+ {
210
+ 'type' => 'parsing_exception',
211
+ 'reason' => '[name] query malformed, no start_object after query name',
212
+ 'line' => 1,
213
+ 'col' => 38
214
+ }
215
+ ],
216
+ 'type' => 'parsing_exception',
217
+ 'reason' => '[name] query malformed, no start_object after query name',
218
+ 'line' => 1,
219
+ 'col' => 38
220
+ },
221
+ 'status' => 400
222
+ }
223
+
224
+ expect{ subject.json_query(index_name: 'test_example_index', json_query: example.to_json) }
225
+ .to raise_error(ElasticSearchFramework::Exceptions::IndexError, "An error occurred executing an index query. Response: #{response_body.to_json}")
226
+ end
227
+ end
228
+
229
+ after do
230
+ ExampleIndex.delete
231
+ end
232
+ end
233
+
124
234
  describe '#host' do
125
235
  it 'should return the expected host based on default host & port values' do
126
236
  expect(subject.host).to eq "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}"
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.1.0
4
+ version: 1.2.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-04-25 00:00:00.000000000 Z
11
+ date: 2018-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,10 +123,10 @@ files:
123
123
  - lib/elastic_search_framework/query.rb
124
124
  - lib/elastic_search_framework/repository.rb
125
125
  - lib/elastic_search_framework/version.rb
126
+ - spec/elastic_search_framework/index_spec.rb
127
+ - spec/elastic_search_framework/query_spec.rb
128
+ - spec/elastic_search_framework/repository_spec.rb
126
129
  - spec/example_index.rb
127
- - spec/index_spec.rb
128
- - spec/query_spec.rb
129
- - spec/repository_spec.rb
130
130
  - spec/spec_helper.rb
131
131
  - spec/test_item.rb
132
132
  homepage: https://github.com/sage/elastic_search_framework