elastic_adapter 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +8 -0
  3. data/lib/elastic_adapter/index.rb +16 -15
  4. data/lib/elastic_adapter/response.rb +25 -0
  5. data/lib/elastic_adapter/responses/with_aggregations.rb +9 -0
  6. data/lib/elastic_adapter/responses/with_count.rb +9 -0
  7. data/lib/elastic_adapter/responses/with_exception.rb +9 -0
  8. data/lib/elastic_adapter/responses/with_hit.rb +24 -0
  9. data/lib/elastic_adapter/responses/with_hits.rb +18 -0
  10. data/lib/elastic_adapter/responses/with_suggestions.rb +9 -0
  11. data/lib/elastic_adapter/responses/with_validations.rb +13 -0
  12. data/lib/elastic_adapter/version.rb +1 -1
  13. data/lib/elastic_adapter.rb +8 -9
  14. data/spec/cassettes/ElasticAdapter_Index/_aggregate/response/{is_a_AggregationResponse.yml → has_a_aggregation.yml} +3 -3
  15. data/spec/cassettes/ElasticAdapter_Index/_aggregate/response/is_a_WithAggregation.yml +30 -0
  16. data/spec/cassettes/ElasticAdapter_Index/_aggregate.yml +8 -8
  17. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_fixnum.yml +5 -5
  18. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/returns_the_amount_of_all_documents.yml +1 -1
  19. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index.yml +2 -2
  20. data/spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_fixnum.yml +5 -5
  21. data/spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/returns_1.yml +1 -1
  22. data/spec/cassettes/ElasticAdapter_Index/_count/not_empty_index.yml +4 -4
  23. data/spec/cassettes/ElasticAdapter_Index/_create_index/index_is_present/response/has_an_exception.yml +1 -1
  24. data/spec/cassettes/ElasticAdapter_Index/_create_index/index_is_present/response/is_a_Response.yml +1 -1
  25. data/spec/cassettes/ElasticAdapter_Index/_create_index/index_is_present.yml +2 -2
  26. data/spec/cassettes/ElasticAdapter_Index/_create_index/index_not_present/response/has_no_exception.yml +1 -1
  27. data/spec/cassettes/ElasticAdapter_Index/_create_index/index_not_present/response/is_a_Response.yml +1 -1
  28. data/spec/cassettes/ElasticAdapter_Index/_create_index/index_not_present/response.yml +1 -1
  29. data/spec/cassettes/ElasticAdapter_Index/_delete_index/index_not_present/repsonse/has_an_exception.yml +1 -1
  30. data/spec/cassettes/ElasticAdapter_Index/_delete_index/index_not_present/repsonse/is_a_Response.yml +1 -1
  31. data/spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/has_no_exception.yml +1 -1
  32. data/spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/has_no_exceptionElasticAdapter_Index/_delete_index/index_present/repsonse.yml +1 -1
  33. data/spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/is_a_Response.yml +1 -1
  34. data/spec/cassettes/ElasticAdapter_Index/_delete_index/index_present/repsonse/is_a_ResponseElasticAdapter_Index/_delete_index/index_present/repsonse.yml +1 -1
  35. data/spec/cassettes/ElasticAdapter_Index/_get/document_exists/response/document/returns_the_document.yml +1 -1
  36. data/spec/cassettes/ElasticAdapter_Index/_get/document_exists.yml +3 -3
  37. data/spec/cassettes/ElasticAdapter_Index/_index/existing_document/doesn_t_change_the_document_count.yml +1 -55
  38. data/spec/cassettes/ElasticAdapter_Index/_index/existing_document/invokes_to_hash_on_the_document.yml +2 -2
  39. data/spec/cassettes/ElasticAdapter_Index/_index/existing_document/updates_the_document.yml +2 -2
  40. data/spec/cassettes/ElasticAdapter_Index/_index/existing_document.yml +3 -3
  41. data/spec/cassettes/ElasticAdapter_Index/_index/new_document/indexes_a_document.yml +3 -3
  42. data/spec/cassettes/ElasticAdapter_Index/_index/new_document/invokes_to_hash_on_the_document.yml +2 -2
  43. data/spec/cassettes/ElasticAdapter_Index/_index/new_document.yml +2 -2
  44. data/spec/cassettes/ElasticAdapter_Index/_search/match_all/returns_all_documents.yml +2 -2
  45. data/spec/cassettes/ElasticAdapter_Index/_search/zoo/returns_one_document.yml +3 -3
  46. data/spec/cassettes/ElasticAdapter_Index/_search/zoo/returns_the_wanted_document.yml +2 -2
  47. data/spec/cassettes/ElasticAdapter_Index/_search.yml +16 -16
  48. data/spec/cassettes/ElasticAdapter_Index/_suggest/query_ba_/returns_bar.yml +1 -1
  49. data/spec/cassettes/ElasticAdapter_Index/_suggest/query_ba_/returns_one_result.yml +1 -1
  50. data/spec/cassettes/ElasticAdapter_Index/_suggest.yml +4 -4
  51. data/spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/is_a_response.yml +1 -1
  52. data/spec/cassettes/ElasticAdapter_Index/_validate/invalid_query/valid_/is_false.yml +1 -1
  53. data/spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_a_response.yml +1 -1
  54. data/spec/cassettes/ElasticAdapter_Index/_validate/valid_query/is_true.yml +1 -1
  55. data/spec/cassettes/ElasticAdapter_Index/_validate.yml +2 -2
  56. data/spec/{index → integration/index}/aggregation_spec.rb +10 -2
  57. data/spec/{index → integration/index}/count_spec.rb +5 -10
  58. data/spec/{index → integration/index}/create_index_spec.rb +0 -0
  59. data/spec/{index → integration/index}/delete_index_spec.rb +0 -0
  60. data/spec/{index → integration/index}/get_spec.rb +8 -1
  61. data/spec/integration/index/index_spec.rb +28 -0
  62. data/spec/{index → integration/index}/search_spec.rb +2 -2
  63. data/spec/{index → integration/index}/shared_context.rb +0 -0
  64. data/spec/{index → integration/index}/shared_examples.rb +2 -2
  65. data/spec/{index → integration/index}/suggest_spec.rb +2 -6
  66. data/spec/{index → integration/index}/unit_spec.rb +0 -0
  67. data/spec/{index → integration/index}/validate_spec.rb +1 -16
  68. data/spec/{document_type_spec.rb → unit/document_type_spec.rb} +0 -0
  69. data/spec/{elastic_adapter_spec.rb → unit/elastic_adapter_spec.rb} +0 -0
  70. data/spec/unit/index_spec.rb +218 -0
  71. data/spec/unit/response_spec.rb +28 -0
  72. data/spec/unit/responses/with_aggregations_spec.rb +30 -0
  73. data/spec/unit/responses/with_count_spec.rb +22 -0
  74. data/spec/unit/responses/with_exception_spec.rb +17 -0
  75. data/spec/unit/responses/with_hit_spec.rb +49 -0
  76. data/spec/unit/responses/with_hits_spec.rb +79 -0
  77. data/spec/unit/responses/with_suggestions_spec.rb +33 -0
  78. data/spec/unit/responses/with_validations_spec.rb +34 -0
  79. metadata +61 -65
  80. data/lib/elastic_adapter/responses/aggregation_response.rb +0 -28
  81. data/lib/elastic_adapter/responses/base_response.rb +0 -28
  82. data/lib/elastic_adapter/responses/count_response.rb +0 -13
  83. data/lib/elastic_adapter/responses/get_response.rb +0 -13
  84. data/lib/elastic_adapter/responses/response_decorator_factory.rb +0 -28
  85. data/lib/elastic_adapter/responses/sanitized_response.rb +0 -46
  86. data/lib/elastic_adapter/responses/search_response.rb +0 -33
  87. data/lib/elastic_adapter/responses/suggestion_response.rb +0 -46
  88. data/lib/elastic_adapter/responses/validation_response.rb +0 -18
  89. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/is_a_response.yml +0 -30
  90. data/spec/cassettes/ElasticAdapter_Index/_count/empty_index/number.yml +0 -57
  91. data/spec/cassettes/ElasticAdapter_Index/_count/not_empty_index/is_a_response.yml +0 -30
  92. data/spec/index/index_spec.rb +0 -60
  93. data/spec/responses/aggregation_response_spec.rb +0 -57
  94. data/spec/responses/base_response_spec.rb +0 -19
  95. data/spec/responses/count_response_spec.rb +0 -24
  96. data/spec/responses/get_response_spec.rb +0 -29
  97. data/spec/responses/response_decorator_factory_spec.rb +0 -126
  98. data/spec/responses/sanitized_response_spec.rb +0 -36
  99. data/spec/responses/search_response_spec.rb +0 -122
  100. data/spec/responses/suggestion_response_spec.rb +0 -63
  101. data/spec/responses/validation_response_spec.rb +0 -45
@@ -1,57 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ElasticAdapter
4
- module Responses
5
- describe AggregationResponse do
6
- let(:response){{
7
- aggregations: {
8
- products: {
9
- doc_count_error_upper_bound: 46,
10
- buckets: [
11
- {
12
- key: "Product A",
13
- doc_count: 100
14
- },
15
- {
16
- key: "Product Z",
17
- doc_count: 52
18
- }
19
- ]
20
- }
21
- }
22
- }}
23
-
24
- subject { AggregationResponse.new(response) }
25
-
26
- describe "#aggregations" do
27
- it "counts one" do
28
- expect(subject.aggregations.count).to eq 1
29
- end
30
-
31
- it "contains products" do
32
- expect(subject.aggregations).to have_key :products
33
- end
34
-
35
- describe "products" do
36
- it "counts two" do
37
- expect(subject.aggregations[:products].count).to eq 2
38
- end
39
-
40
- it "contains Product A with it's count" do
41
- expect(subject.aggregations[:products]).to include({
42
- term: "Product A",
43
- count: 100
44
- })
45
- end
46
-
47
- it "contains Product Z with it's count" do
48
- expect(subject.aggregations[:products]).to include({
49
- term: "Product Z",
50
- count: 52
51
- })
52
- end
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,19 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ElasticAdapter
4
- module Responses
5
- describe BaseResponse do
6
- let(:object) { Class.new }
7
-
8
- subject { BaseResponse.new(object) }
9
-
10
- describe "delegation" do
11
- describe "#object" do
12
- it "returns the object" do
13
- expect(subject.object).to be object
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,24 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ElasticAdapter
4
- module Responses
5
- describe CountResponse do
6
- let(:response) {{
7
- count: 1,
8
- shards: {
9
- total: 5,
10
- successful: 5,
11
- failed: 0
12
- }
13
- }}
14
-
15
- subject { CountResponse.new(response) }
16
-
17
- describe "#count" do
18
- it "equals 1" do
19
- expect(subject.count).to eq 1
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ElasticAdapter
4
- module Responses
5
- describe GetResponse do
6
- let(:response) {{
7
- index: "twitter",
8
- type: "tweet",
9
- id: "1",
10
- version: 1,
11
- found: true,
12
- source: {
13
- user: "kimchy",
14
- postDate: "2009-11-15T14:12:12",
15
- message: "trying out Elasticsearch"
16
- }
17
- }}
18
-
19
- subject { GetResponse.new(response) }
20
-
21
- describe "#document" do
22
- it "returns the source" do
23
- expect(subject.document).to eq response[:source].merge(id: response[:id])
24
- end
25
- end
26
-
27
- end
28
- end
29
- end
@@ -1,126 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ElasticAdapter
4
- module Responses
5
- describe ResponseDecoratorFactory do
6
- describe "class methods" do
7
- describe "#decorate" do
8
- context "count" do
9
- let(:response) {{ acknowledged: true }}
10
- subject { ResponseDecoratorFactory.decorate(response, :count) }
11
-
12
- it "returns a CountResponse" do
13
- expect(subject).to be_a CountResponse
14
- end
15
- end
16
-
17
- context "get" do
18
- let(:response) {{ source: {} }}
19
- subject { ResponseDecoratorFactory.decorate(response, :get) }
20
-
21
- it "returns a GetResponse" do
22
- expect(subject).to be_a GetResponse
23
- end
24
- end
25
-
26
- context "search" do
27
- let(:response) {{ hits: {hits: []} }}
28
- subject { ResponseDecoratorFactory.decorate(response, :search) }
29
-
30
- it "returns a SearchResponse" do
31
- expect(subject).to be_a SearchResponse
32
- end
33
- end
34
-
35
- context "validation" do
36
- let(:response) {{ valid: true }}
37
- subject { ResponseDecoratorFactory.decorate(response, :validation) }
38
-
39
- it "returns a ValidationResponse" do
40
- expect(subject).to be_a ValidationResponse
41
- end
42
- end
43
-
44
- context "suggestion" do
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
- }}
61
- subject { ResponseDecoratorFactory.decorate(response, :suggestion) }
62
-
63
- it "returns a SuggestionResponse" do
64
- expect(subject).to be_a SuggestionResponse
65
- end
66
- end
67
-
68
- context "aggregation" do
69
- let(:response){{
70
- aggregations: {
71
- products: {
72
- doc_count_error_upper_bound: 46,
73
- buckets: [
74
- {
75
- key: "Product A",
76
- doc_count: 100
77
- }
78
- ]
79
- }
80
- }
81
- }}
82
-
83
- subject { ResponseDecoratorFactory.decorate(response, :aggregation) }
84
-
85
- it "returns a AggregationResponse" do
86
- expect(subject).to be_a AggregationResponse
87
- end
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
122
- end
123
- end
124
- end
125
- end
126
- end
@@ -1,36 +0,0 @@
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
@@ -1,122 +0,0 @@
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
- aggregations: {
28
- products: {
29
- doc_count_error_upper_bound: 46,
30
- buckets: [
31
- {
32
- key: "Product A",
33
- doc_count: 100
34
- },
35
- {
36
- key: "Product Z",
37
- doc_count: 52
38
- }
39
- ]
40
- }
41
- },
42
- suggest: {
43
- foo_suggest: [
44
- {
45
- text: "ba",
46
- offset: 0,
47
- length: 2,
48
- options: [
49
- {
50
- text: "bar",
51
- score: 1.0
52
- }
53
- ]
54
- }
55
- ]
56
- }
57
- }}
58
-
59
- subject { SearchResponse.new(response) }
60
-
61
- context "empty response" do
62
- let(:response) {{}}
63
-
64
- it "should not fail" do
65
- expect { subject }.not_to raise_error
66
- end
67
- end
68
-
69
- describe "hits" do
70
- it "returns the hits" do
71
- expect(subject.hits).to include({
72
- id: "1",
73
- user: "kimchy",
74
- postDate: "2009-11-15T14:12:12",
75
- message: "trying out Elasticsearch"
76
- })
77
- end
78
- end
79
-
80
- describe "#aggregations" do
81
- it "returns the aggregations" do
82
- expect(subject.aggregations).to eq response[:aggregations]
83
- end
84
- end
85
-
86
- describe "suggestions" do
87
- it "is an array" do
88
- expect(subject.suggestions).to be_an Array
89
- end
90
-
91
- describe "first suggestion" do
92
- describe "#terms" do
93
- it "is an Array" do
94
- expect(subject.suggestions.first.terms).to be_an Array
95
- end
96
-
97
- it "counts one" do
98
- expect(subject.suggestions.first.terms.count).to eq 1
99
- end
100
-
101
- describe "first term" do
102
- describe "#text" do
103
- it "equals 'ba'" do
104
- expect(subject.suggestions.first.terms.first.text).to eq "ba"
105
- end
106
-
107
- describe "#options" do
108
- it "includes the options" do
109
- expect(subject.suggestions.first.terms.first.options).to include({
110
- text: "bar",
111
- score: 1.0
112
- })
113
- end
114
- end
115
- end
116
- end
117
- end
118
- end
119
- end
120
- end
121
- end
122
- end
@@ -1,63 +0,0 @@
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
@@ -1,45 +0,0 @@
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