elastic_adapter 0.0.3 → 0.0.4

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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/elastic_adapter.rb +11 -9
  3. data/lib/elastic_adapter/index.rb +6 -4
  4. data/lib/elastic_adapter/{decoration → responses}/aggregation_response.rb +7 -13
  5. data/lib/elastic_adapter/responses/base_response.rb +28 -0
  6. data/lib/elastic_adapter/responses/count_response.rb +13 -0
  7. data/lib/elastic_adapter/responses/get_response.rb +13 -0
  8. data/lib/elastic_adapter/{decoration → responses}/response_decorator_factory.rb +5 -23
  9. data/lib/elastic_adapter/responses/sanitized_response.rb +46 -0
  10. data/lib/elastic_adapter/responses/search_response.rb +21 -0
  11. data/lib/elastic_adapter/responses/suggestion_response.rb +46 -0
  12. data/lib/elastic_adapter/responses/validation_response.rb +18 -0
  13. data/lib/elastic_adapter/version.rb +1 -1
  14. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_fixnum.yml +30 -0
  15. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/number.yml +57 -0
  16. data/spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_fixnum.yml +30 -0
  17. data/spec/cassettes/ElasticAdapter_Index/_get/document_exists.yml +3 -3
  18. data/spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/{contains_the_document.yml → document/returns_the_document.yml} +1 -1
  19. data/spec/cassettes/ElasticAdapter_Index/_search.yml +16 -16
  20. data/spec/cassettes/ElasticAdapter_Index/_search/match_all/returns_all_documents.yml +2 -2
  21. data/spec/cassettes/ElasticAdapter_Index/_search/zoo/returns_one_document.yml +1 -1
  22. data/spec/cassettes/ElasticAdapter_Index/_search/zoo/returns_the_wanted_document.yml +2 -2
  23. data/spec/cassettes/ElasticAdapter_Index/_validate.yml +2 -2
  24. data/spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_a_response.yml +1 -1
  25. data/spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/{is_false.yml → valid_/is_false.yml} +1 -1
  26. data/spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_a_response.yml +1 -1
  27. data/spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_true.yml +1 -1
  28. data/spec/index/aggregation_spec.rb +1 -1
  29. data/spec/index/count_spec.rb +4 -4
  30. data/spec/index/get_spec.rb +4 -2
  31. data/spec/index/index_spec.rb +1 -1
  32. data/spec/index/search_spec.rb +4 -4
  33. data/spec/index/shared_examples.rb +2 -2
  34. data/spec/index/suggest_spec.rb +2 -3
  35. data/spec/index/validate_spec.rb +7 -5
  36. data/spec/{decoration → responses}/aggregation_response_spec.rb +1 -2
  37. data/spec/responses/base_response_spec.rb +19 -0
  38. data/spec/responses/count_response_spec.rb +24 -0
  39. data/spec/responses/get_response_spec.rb +29 -0
  40. data/spec/{decoration → responses}/response_decorator_factory_spec.rb +54 -6
  41. data/spec/responses/sanitized_response_spec.rb +36 -0
  42. data/spec/responses/search_response_spec.rb +43 -0
  43. data/spec/responses/suggestion_response_spec.rb +63 -0
  44. data/spec/responses/validation_response_spec.rb +45 -0
  45. metadata +38 -20
  46. data/lib/elastic_adapter/decoration/count_response.rb +0 -16
  47. data/lib/elastic_adapter/decoration/decorator.rb +0 -37
  48. data/lib/elastic_adapter/decoration/hit_decorator.rb +0 -25
  49. data/lib/elastic_adapter/decoration/search_response.rb +0 -27
  50. data/lib/elastic_adapter/decoration/suggestion_response.rb +0 -24
  51. data/lib/elastic_adapter/decoration/validation_response.rb +0 -15
  52. data/lib/elastic_adapter/response.rb +0 -51
  53. data/spec/response_spec.rb +0 -64
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module ElasticAdapter
4
- module Decoration
4
+ module Responses
5
5
  describe ResponseDecoratorFactory do
6
6
  describe "class methods" do
7
7
  describe "#decorate" do
@@ -14,12 +14,12 @@ module ElasticAdapter
14
14
  end
15
15
  end
16
16
 
17
- context "hit" do
17
+ context "get" do
18
18
  let(:response) {{ source: {} }}
19
- subject { ResponseDecoratorFactory.decorate(response, :hit) }
19
+ subject { ResponseDecoratorFactory.decorate(response, :get) }
20
20
 
21
- it "returns a HitDecorator" do
22
- expect(subject).to be_a HitDecorator
21
+ it "returns a GetResponse" do
22
+ expect(subject).to be_a GetResponse
23
23
  end
24
24
  end
25
25
 
@@ -42,7 +42,22 @@ module ElasticAdapter
42
42
  end
43
43
 
44
44
  context "suggestion" do
45
- let(:response) {{ foo: "bar", foo_suggestion: [{options: []}] }}
45
+ let(:response) {{
46
+ my_suggest_1: [
47
+ {
48
+ text: "amsterdma",
49
+ offset: 4,
50
+ length: 9,
51
+ options: [
52
+ {
53
+ text: "amsterdam",
54
+ freq: 77,
55
+ score: 0.8888889
56
+ }
57
+ ]
58
+ }
59
+ ]
60
+ }}
46
61
  subject { ResponseDecoratorFactory.decorate(response, :suggestion) }
47
62
 
48
63
  it "returns a SuggestionResponse" do
@@ -71,6 +86,39 @@ module ElasticAdapter
71
86
  expect(subject).to be_a AggregationResponse
72
87
  end
73
88
  end
89
+
90
+ context "mixed aggregation and search" do
91
+ let(:response){{
92
+ hits: { hits: [] },
93
+ aggregations: {
94
+ products: {
95
+ doc_count_error_upper_bound: 46,
96
+ buckets: [
97
+ {
98
+ key: "Product A",
99
+ doc_count: 100
100
+ }
101
+ ]
102
+ }
103
+ }
104
+ }}
105
+
106
+ subject { ResponseDecoratorFactory.decorate(response, :aggregation, :search) }
107
+
108
+ it "doent raise an exception" do
109
+ expect{
110
+ subject
111
+ }.not_to raise_error
112
+ end
113
+
114
+ it "behaves like a AggregationResponse" do
115
+ expect(subject).to respond_to :aggregations
116
+ end
117
+
118
+ it "behaves like a SearchResponse" do
119
+ expect(subject).to respond_to :count
120
+ end
121
+ end
74
122
  end
75
123
  end
76
124
  end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ module ElasticAdapter
4
+ module Responses
5
+ describe SanitizedResponse do
6
+ describe "symbolize keys" do
7
+ it "turns all strings in Hash keys to symbols" do
8
+ hash = {"foo" => "bar"}
9
+ response = SanitizedResponse.new(hash)
10
+ expect(response.keys.first).to be_a Symbol
11
+ end
12
+
13
+ it "removes all leading underscores from keys" do
14
+ hash = {"_foo" => {"_bar" => "baz" }}
15
+ expected = {foo: {bar: "baz" }}
16
+ response = SanitizedResponse.new(hash)
17
+ expect(response.object).to eq expected
18
+ end
19
+
20
+ it "works with arrays" do
21
+ hash = { "foo" => ["bar", "buz"] }
22
+ expected = { foo: ["bar", "buz"] }
23
+ response = SanitizedResponse.new(hash)
24
+ expect(response.object).to eq expected
25
+ end
26
+
27
+ it "works with hashes in arrays" do
28
+ hash = { boo: [ {"foo" => ["bar", "buz"] } ] }
29
+ expected = { boo: [ {foo: ["bar", "buz"] } ] }
30
+ response = SanitizedResponse.new(hash)
31
+ expect(response.object).to eq expected
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ module ElasticAdapter
4
+ module Responses
5
+ describe SearchResponse do
6
+ let(:response) {{
7
+ shards: {
8
+ total: 5,
9
+ successful: 5,
10
+ failed: 0
11
+ },
12
+ hits: {
13
+ total: 1,
14
+ hits: [
15
+ {
16
+ index: "twitter",
17
+ type: "tweet",
18
+ id: "1",
19
+ source: {
20
+ user: "kimchy",
21
+ postDate: "2009-11-15T14:12:12",
22
+ message: "trying out Elasticsearch"
23
+ }
24
+ }
25
+ ]
26
+ }
27
+ }}
28
+
29
+ subject { SearchResponse.new(response) }
30
+
31
+ describe "hits" do
32
+ it "returns the hits" do
33
+ expect(subject.hits).to include({
34
+ id: "1",
35
+ user: "kimchy",
36
+ postDate: "2009-11-15T14:12:12",
37
+ message: "trying out Elasticsearch"
38
+ })
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ module ElasticAdapter
4
+ module Responses
5
+ describe SuggestionResponse do
6
+ let(:response){{
7
+ shards: {
8
+ total: 5, successful: 5, failed: 0
9
+ },
10
+ foo_suggest: [
11
+ {
12
+ text: "ba",
13
+ offset: 0,
14
+ length: 2,
15
+ options: [
16
+ {
17
+ text: "bar",
18
+ score: 1.0
19
+ }
20
+ ]
21
+ }
22
+ ]
23
+ }}
24
+
25
+ describe "suggestions" do
26
+ subject { SuggestionResponse.new(response).suggestions }
27
+
28
+ it "is an array" do
29
+ expect(subject).to be_an Array
30
+ end
31
+
32
+ describe "first suggestion" do
33
+ describe "#terms" do
34
+ it "is an Array" do
35
+ expect(subject.first.terms).to be_an Array
36
+ end
37
+
38
+ it "counts one" do
39
+ expect(subject.first.terms.count).to eq 1
40
+ end
41
+
42
+ describe "first term" do
43
+ describe "#text" do
44
+ it "equals 'ba'" do
45
+ expect(subject.first.terms.first.text).to eq "ba"
46
+ end
47
+
48
+ describe "#options" do
49
+ it "includes the options" do
50
+ expect(subject.first.terms.first.options).to include({
51
+ text: "bar",
52
+ score: 1.0
53
+ })
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ module ElasticAdapter
4
+ module Responses
5
+ describe ValidationResponse do
6
+ let(:response) {{
7
+ valid: false,
8
+ shards: {
9
+ total: 1,
10
+ successful: 1,
11
+ failed: 0
12
+ },
13
+ explanations: [
14
+ {
15
+ index: "twitter",
16
+ valid: false,
17
+ error: "some error message"
18
+ }
19
+ ]
20
+ }}
21
+
22
+ subject { ValidationResponse.new(response) }
23
+
24
+ describe "#valid?" do
25
+ it "returns false" do
26
+ expect(subject.valid?).to be false
27
+ end
28
+ end
29
+
30
+ describe "#explanations" do
31
+ it "is an Array" do
32
+ expect(subject.explanations).to be_an Array
33
+ end
34
+
35
+ it "includes the explanation" do
36
+ expect(subject.explanations).to include({
37
+ index: "twitter",
38
+ valid: false,
39
+ error: "some error message"
40
+ })
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristopher Bredemeier
@@ -156,24 +156,27 @@ files:
156
156
  - elastic_adapter.gemspec
157
157
  - examples/basic_usage.rb
158
158
  - lib/elastic_adapter.rb
159
- - lib/elastic_adapter/decoration/aggregation_response.rb
160
- - lib/elastic_adapter/decoration/count_response.rb
161
- - lib/elastic_adapter/decoration/decorator.rb
162
- - lib/elastic_adapter/decoration/hit_decorator.rb
163
- - lib/elastic_adapter/decoration/response_decorator_factory.rb
164
- - lib/elastic_adapter/decoration/search_response.rb
165
- - lib/elastic_adapter/decoration/suggestion_response.rb
166
- - lib/elastic_adapter/decoration/validation_response.rb
167
159
  - lib/elastic_adapter/document_type.rb
168
160
  - lib/elastic_adapter/index.rb
169
- - lib/elastic_adapter/response.rb
161
+ - lib/elastic_adapter/responses/aggregation_response.rb
162
+ - lib/elastic_adapter/responses/base_response.rb
163
+ - lib/elastic_adapter/responses/count_response.rb
164
+ - lib/elastic_adapter/responses/get_response.rb
165
+ - lib/elastic_adapter/responses/response_decorator_factory.rb
166
+ - lib/elastic_adapter/responses/sanitized_response.rb
167
+ - lib/elastic_adapter/responses/search_response.rb
168
+ - lib/elastic_adapter/responses/suggestion_response.rb
169
+ - lib/elastic_adapter/responses/validation_response.rb
170
170
  - lib/elastic_adapter/version.rb
171
171
  - spec/cassettes/ElasticAdapter_Index/_aggregate.yml
172
172
  - spec/cassettes/ElasticAdapter_Index/_aggregate/response/is_a_AggregationResponse.yml
173
173
  - spec/cassettes/ElasticAdapter_Index/_count/empty_index.yml
174
+ - spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_fixnum.yml
174
175
  - spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_response.yml
176
+ - spec/cassettes/ElasticAdapter_Index/_count/empty_index/number.yml
175
177
  - spec/cassettes/ElasticAdapter_Index/_count/empty_index/returns_the_amount_of_all_documents.yml
176
178
  - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index.yml
179
+ - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_fixnum.yml
177
180
  - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_response.yml
178
181
  - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/returns_1.yml
179
182
  - spec/cassettes/ElasticAdapter_Index/_create_index/index_is_present.yml
@@ -189,7 +192,7 @@ files:
189
192
  - spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/is_a_Response.yml
190
193
  - spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/is_a_ResponseElasticAdapter_Index/_delete_index/index_present/repsonse.yml
191
194
  - spec/cassettes/ElasticAdapter_Index/_get/document_exists.yml
192
- - spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/contains_the_document.yml
195
+ - spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/document/returns_the_document.yml
193
196
  - spec/cassettes/ElasticAdapter_Index/_index/existing_document.yml
194
197
  - spec/cassettes/ElasticAdapter_Index/_index/existing_document/doesn_t_change_the_document_count.yml
195
198
  - spec/cassettes/ElasticAdapter_Index/_index/existing_document/invokes_to_hash_on_the_document.yml
@@ -206,11 +209,9 @@ files:
206
209
  - spec/cassettes/ElasticAdapter_Index/_suggest/query_ba_/returns_one_result.yml
207
210
  - spec/cassettes/ElasticAdapter_Index/_validate.yml
208
211
  - spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_a_response.yml
209
- - spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_false.yml
212
+ - spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/valid_/is_false.yml
210
213
  - spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_a_response.yml
211
214
  - spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_true.yml
212
- - spec/decoration/aggregation_response_spec.rb
213
- - spec/decoration/response_decorator_factory_spec.rb
214
215
  - spec/document_type_spec.rb
215
216
  - spec/elastic_adapter_spec.rb
216
217
  - spec/index/aggregation_spec.rb
@@ -225,7 +226,15 @@ files:
225
226
  - spec/index/suggest_spec.rb
226
227
  - spec/index/unit_spec.rb
227
228
  - spec/index/validate_spec.rb
228
- - spec/response_spec.rb
229
+ - spec/responses/aggregation_response_spec.rb
230
+ - spec/responses/base_response_spec.rb
231
+ - spec/responses/count_response_spec.rb
232
+ - spec/responses/get_response_spec.rb
233
+ - spec/responses/response_decorator_factory_spec.rb
234
+ - spec/responses/sanitized_response_spec.rb
235
+ - spec/responses/search_response_spec.rb
236
+ - spec/responses/suggestion_response_spec.rb
237
+ - spec/responses/validation_response_spec.rb
229
238
  - spec/spec_helper.rb
230
239
  - spec/support/index_helper.rb
231
240
  homepage: https://github.com/kbredemeier/elastic_adapter
@@ -256,9 +265,12 @@ test_files:
256
265
  - spec/cassettes/ElasticAdapter_Index/_aggregate.yml
257
266
  - spec/cassettes/ElasticAdapter_Index/_aggregate/response/is_a_AggregationResponse.yml
258
267
  - spec/cassettes/ElasticAdapter_Index/_count/empty_index.yml
268
+ - spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_fixnum.yml
259
269
  - spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_response.yml
270
+ - spec/cassettes/ElasticAdapter_Index/_count/empty_index/number.yml
260
271
  - spec/cassettes/ElasticAdapter_Index/_count/empty_index/returns_the_amount_of_all_documents.yml
261
272
  - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index.yml
273
+ - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_fixnum.yml
262
274
  - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_response.yml
263
275
  - spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/returns_1.yml
264
276
  - spec/cassettes/ElasticAdapter_Index/_create_index/index_is_present.yml
@@ -274,7 +286,7 @@ test_files:
274
286
  - spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/is_a_Response.yml
275
287
  - spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/is_a_ResponseElasticAdapter_Index/_delete_index/index_present/repsonse.yml
276
288
  - spec/cassettes/ElasticAdapter_Index/_get/document_exists.yml
277
- - spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/contains_the_document.yml
289
+ - spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/document/returns_the_document.yml
278
290
  - spec/cassettes/ElasticAdapter_Index/_index/existing_document.yml
279
291
  - spec/cassettes/ElasticAdapter_Index/_index/existing_document/doesn_t_change_the_document_count.yml
280
292
  - spec/cassettes/ElasticAdapter_Index/_index/existing_document/invokes_to_hash_on_the_document.yml
@@ -291,11 +303,9 @@ test_files:
291
303
  - spec/cassettes/ElasticAdapter_Index/_suggest/query_ba_/returns_one_result.yml
292
304
  - spec/cassettes/ElasticAdapter_Index/_validate.yml
293
305
  - spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_a_response.yml
294
- - spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_false.yml
306
+ - spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/valid_/is_false.yml
295
307
  - spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_a_response.yml
296
308
  - spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_true.yml
297
- - spec/decoration/aggregation_response_spec.rb
298
- - spec/decoration/response_decorator_factory_spec.rb
299
309
  - spec/document_type_spec.rb
300
310
  - spec/elastic_adapter_spec.rb
301
311
  - spec/index/aggregation_spec.rb
@@ -310,7 +320,15 @@ test_files:
310
320
  - spec/index/suggest_spec.rb
311
321
  - spec/index/unit_spec.rb
312
322
  - spec/index/validate_spec.rb
313
- - spec/response_spec.rb
323
+ - spec/responses/aggregation_response_spec.rb
324
+ - spec/responses/base_response_spec.rb
325
+ - spec/responses/count_response_spec.rb
326
+ - spec/responses/get_response_spec.rb
327
+ - spec/responses/response_decorator_factory_spec.rb
328
+ - spec/responses/sanitized_response_spec.rb
329
+ - spec/responses/search_response_spec.rb
330
+ - spec/responses/suggestion_response_spec.rb
331
+ - spec/responses/validation_response_spec.rb
314
332
  - spec/spec_helper.rb
315
333
  - spec/support/index_helper.rb
316
334
  has_rdoc:
@@ -1,16 +0,0 @@
1
- module ElasticAdapter
2
- module Decoration
3
- # Used to wrap responses from the elasticsearch count api
4
- # After decoration the decorator will point to the actual
5
- # count returned by elasticsearch
6
- class CountResponse < Decorator
7
- # Reduced the hash to the count returned by elasticsearch
8
- #
9
- # @param [Object] object
10
- # @return [Integer] the count returned by elasticsearch
11
- def alter_object(object)
12
- object[:count]
13
- end
14
- end
15
- end
16
- end
@@ -1,37 +0,0 @@
1
- require "delegate"
2
-
3
- module ElasticAdapter
4
- module Decoration
5
- # Abstract base class for response decorators
6
- # @abstract
7
- #
8
- # @attr [Object] original_object the original unmodified object
9
- class Decorator < SimpleDelegator
10
- attr_reader :original_object
11
-
12
- # Takes an object and stores it in `@original_object` and saves a
13
- # altered version as the decorated object
14
- #
15
- # @param [Object] object
16
- def initialize(object)
17
- @original_object = object
18
- __setobj__(alter_object(object))
19
- end
20
-
21
- # Returns the underlaying altered object
22
- #
23
- # @return [Object] the altered object
24
- def object
25
- __getobj__
26
- end
27
-
28
- # Is intended to alter the passed object to change it's interface
29
- #
30
- # @param [Object] object
31
- # @return [Object]
32
- def alter_object(_object)
33
- fail NotImplementedError, "alter_object must be overriden in subclasses"
34
- end
35
- end
36
- end
37
- end