elastic_adapter 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,30 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://localhost:9200/test_index/_count
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"query":{"match_all":{}}}'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.9.1
12
+ Accept-Encoding:
13
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
+ Accept:
15
+ - "*/*"
16
+ response:
17
+ status:
18
+ code: 404
19
+ message: Not Found
20
+ headers:
21
+ Content-Type:
22
+ - application/json; charset=UTF-8
23
+ Content-Length:
24
+ - '68'
25
+ body:
26
+ encoding: UTF-8
27
+ string: '{"error":"IndexMissingException[[test_index] missing]","status":404}'
28
+ http_version:
29
+ recorded_at: Mon, 09 Mar 2015 18:08:41 GMT
30
+ recorded_with: VCR 2.9.3
@@ -26,7 +26,7 @@ http_interactions:
26
26
  encoding: UTF-8
27
27
  string: '{"acknowledged":true}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:23 GMT
29
+ recorded_at: Mon, 09 Mar 2015 17:55:27 GMT
30
30
  - request:
31
31
  method: put
32
32
  uri: http://localhost:9200/test_index/test_doc/1
@@ -53,7 +53,7 @@ http_interactions:
53
53
  encoding: UTF-8
54
54
  string: '{"_index":"test_index","_type":"test_doc","_id":"1","_version":1,"created":true}'
55
55
  http_version:
56
- recorded_at: Sat, 07 Feb 2015 19:44:26 GMT
56
+ recorded_at: Mon, 09 Mar 2015 17:55:27 GMT
57
57
  - request:
58
58
  method: delete
59
59
  uri: http://localhost:9200/test_index
@@ -80,5 +80,5 @@ http_interactions:
80
80
  encoding: UTF-8
81
81
  string: '{"acknowledged":true}'
82
82
  http_version:
83
- recorded_at: Sat, 07 Feb 2015 19:44:29 GMT
83
+ recorded_at: Mon, 09 Mar 2015 17:55:38 GMT
84
84
  recorded_with: VCR 2.9.3
@@ -26,5 +26,5 @@ http_interactions:
26
26
  encoding: UTF-8
27
27
  string: '{"_index":"test_index","_type":"test_doc","_id":"1","_version":1,"found":true,"_source":{"foo":"bar"}}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:29 GMT
29
+ recorded_at: Mon, 09 Mar 2015 17:55:30 GMT
30
30
  recorded_with: VCR 2.9.3
@@ -15,18 +15,18 @@ http_interactions:
15
15
  - "*/*"
16
16
  response:
17
17
  status:
18
- code: 200
19
- message: OK
18
+ code: 400
19
+ message: Bad Request
20
20
  headers:
21
21
  Content-Type:
22
22
  - application/json; charset=UTF-8
23
23
  Content-Length:
24
- - '21'
24
+ - '81'
25
25
  body:
26
26
  encoding: UTF-8
27
- string: '{"acknowledged":true}'
27
+ string: '{"error":"IndexAlreadyExistsException[[test_index] already exists]","status":400}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:40 GMT
29
+ recorded_at: Mon, 09 Mar 2015 18:47:12 GMT
30
30
  - request:
31
31
  method: put
32
32
  uri: http://localhost:9200/test_index/test_doc/1
@@ -42,18 +42,18 @@ http_interactions:
42
42
  - "*/*"
43
43
  response:
44
44
  status:
45
- code: 201
46
- message: Created
45
+ code: 200
46
+ message: OK
47
47
  headers:
48
48
  Content-Type:
49
49
  - application/json; charset=UTF-8
50
50
  Content-Length:
51
- - '80'
51
+ - '81'
52
52
  body:
53
53
  encoding: UTF-8
54
- string: '{"_index":"test_index","_type":"test_doc","_id":"1","_version":1,"created":true}'
54
+ string: '{"_index":"test_index","_type":"test_doc","_id":"1","_version":2,"created":false}'
55
55
  http_version:
56
- recorded_at: Sat, 07 Feb 2015 19:44:43 GMT
56
+ recorded_at: Mon, 09 Mar 2015 18:47:15 GMT
57
57
  - request:
58
58
  method: put
59
59
  uri: http://localhost:9200/test_index/test_doc/2
@@ -69,18 +69,18 @@ http_interactions:
69
69
  - "*/*"
70
70
  response:
71
71
  status:
72
- code: 201
73
- message: Created
72
+ code: 200
73
+ message: OK
74
74
  headers:
75
75
  Content-Type:
76
76
  - application/json; charset=UTF-8
77
77
  Content-Length:
78
- - '80'
78
+ - '81'
79
79
  body:
80
80
  encoding: UTF-8
81
- string: '{"_index":"test_index","_type":"test_doc","_id":"2","_version":1,"created":true}'
81
+ string: '{"_index":"test_index","_type":"test_doc","_id":"2","_version":2,"created":false}'
82
82
  http_version:
83
- recorded_at: Sat, 07 Feb 2015 19:44:46 GMT
83
+ recorded_at: Mon, 09 Mar 2015 18:47:18 GMT
84
84
  - request:
85
85
  method: delete
86
86
  uri: http://localhost:9200/test_index
@@ -107,5 +107,5 @@ http_interactions:
107
107
  encoding: UTF-8
108
108
  string: '{"acknowledged":true}'
109
109
  http_version:
110
- recorded_at: Sat, 07 Feb 2015 19:44:49 GMT
110
+ recorded_at: Mon, 09 Mar 2015 18:47:23 GMT
111
111
  recorded_with: VCR 2.9.3
@@ -24,7 +24,7 @@ http_interactions:
24
24
  - '360'
25
25
  body:
26
26
  encoding: UTF-8
27
- string: '{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test_index","_type":"test_doc","_id":"1","_score":1.0,"_source":{"foo":"bar","foo_suggest":{"input":"bar"}}},{"_index":"test_index","_type":"test_doc","_id":"2","_score":1.0,"_source":{"foo":"zoo","foo_suggest":{"input":"zoo"}}}]}}'
27
+ string: '{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test_index","_type":"test_doc","_id":"1","_score":1.0,"_source":{"foo":"bar","foo_suggest":{"input":"bar"}}},{"_index":"test_index","_type":"test_doc","_id":"2","_score":1.0,"_source":{"foo":"zoo","foo_suggest":{"input":"zoo"}}}]}}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:49 GMT
29
+ recorded_at: Mon, 09 Mar 2015 18:47:21 GMT
30
30
  recorded_with: VCR 2.9.3
@@ -26,5 +26,5 @@ http_interactions:
26
26
  encoding: UTF-8
27
27
  string: '{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"test_index","_type":"test_doc","_id":"2","_score":0.30685282,"_source":{"foo":"zoo","foo_suggest":{"input":"zoo"}}}]}}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:49 GMT
29
+ recorded_at: Mon, 09 Mar 2015 18:47:23 GMT
30
30
  recorded_with: VCR 2.9.3
@@ -24,7 +24,7 @@ http_interactions:
24
24
  - '254'
25
25
  body:
26
26
  encoding: UTF-8
27
- string: '{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"test_index","_type":"test_doc","_id":"2","_score":0.30685282,"_source":{"foo":"zoo","foo_suggest":{"input":"zoo"}}}]}}'
27
+ string: '{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"test_index","_type":"test_doc","_id":"2","_score":0.30685282,"_source":{"foo":"zoo","foo_suggest":{"input":"zoo"}}}]}}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:49 GMT
29
+ recorded_at: Mon, 09 Mar 2015 18:47:23 GMT
30
30
  recorded_with: VCR 2.9.3
@@ -26,7 +26,7 @@ http_interactions:
26
26
  encoding: UTF-8
27
27
  string: '{"acknowledged":true}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:44:58 GMT
29
+ recorded_at: Mon, 09 Mar 2015 20:01:50 GMT
30
30
  - request:
31
31
  method: delete
32
32
  uri: http://localhost:9200/test_index
@@ -53,5 +53,5 @@ http_interactions:
53
53
  encoding: UTF-8
54
54
  string: '{"acknowledged":true}'
55
55
  http_version:
56
- recorded_at: Sat, 07 Feb 2015 19:45:01 GMT
56
+ recorded_at: Mon, 09 Mar 2015 20:01:53 GMT
57
57
  recorded_with: VCR 2.9.3
@@ -27,5 +27,5 @@ http_interactions:
27
27
  string: '{"valid":false,"_shards":{"total":1,"successful":1,"failed":0},"explanations":[{"index":"test_index","valid":false,"error":"org.elasticsearch.index.query.QueryParsingException:
28
28
  [test_index] request does not support [asd]"}]}'
29
29
  http_version:
30
- recorded_at: Sat, 07 Feb 2015 19:45:01 GMT
30
+ recorded_at: Mon, 09 Mar 2015 20:01:53 GMT
31
31
  recorded_with: VCR 2.9.3
@@ -27,5 +27,5 @@ http_interactions:
27
27
  string: '{"valid":false,"_shards":{"total":1,"successful":1,"failed":0},"explanations":[{"index":"test_index","valid":false,"error":"org.elasticsearch.index.query.QueryParsingException:
28
28
  [test_index] request does not support [asd]"}]}'
29
29
  http_version:
30
- recorded_at: Sat, 07 Feb 2015 19:45:01 GMT
30
+ recorded_at: Mon, 09 Mar 2015 20:01:53 GMT
31
31
  recorded_with: VCR 2.9.3
@@ -26,5 +26,5 @@ http_interactions:
26
26
  encoding: UTF-8
27
27
  string: '{"valid":true,"_shards":{"total":1,"successful":1,"failed":0},"explanations":[{"index":"test_index","valid":true,"explanation":"ConstantScore(*:*)"}]}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:45:01 GMT
29
+ recorded_at: Mon, 09 Mar 2015 20:01:53 GMT
30
30
  recorded_with: VCR 2.9.3
@@ -26,5 +26,5 @@ http_interactions:
26
26
  encoding: UTF-8
27
27
  string: '{"valid":true,"_shards":{"total":1,"successful":1,"failed":0},"explanations":[{"index":"test_index","valid":true,"explanation":"ConstantScore(*:*)"}]}'
28
28
  http_version:
29
- recorded_at: Sat, 07 Feb 2015 19:45:01 GMT
29
+ recorded_at: Mon, 09 Mar 2015 20:01:53 GMT
30
30
  recorded_with: VCR 2.9.3
@@ -26,7 +26,7 @@ module ElasticAdapter
26
26
  }}
27
27
 
28
28
  it "is a AggregationResponse" do
29
- expect(subject.aggregate(query)).to be_a Decoration::AggregationResponse
29
+ expect(subject.aggregate(query)).to be_a Responses::AggregationResponse
30
30
  end
31
31
  end
32
32
  end
@@ -18,8 +18,8 @@ module ElasticAdapter
18
18
  expect(subject.count).to eq 0
19
19
  end
20
20
 
21
- it "is a response" do
22
- expect(subject.count).to be_a Decoration::CountResponse
21
+ it "is a fixnum" do
22
+ expect(subject.count).to be_a Fixnum
23
23
  end
24
24
  end
25
25
 
@@ -37,8 +37,8 @@ module ElasticAdapter
37
37
  expect(subject.count).to eq 1
38
38
  end
39
39
 
40
- it "is a response" do
41
- expect(subject.count).to be_a Decoration::CountResponse
40
+ it "is a fixnum" do
41
+ expect(subject.count).to be_a Fixnum
42
42
  end
43
43
  end
44
44
  end
@@ -19,8 +19,10 @@ module ElasticAdapter
19
19
  let(:response) { subject.get(1) }
20
20
 
21
21
  describe "response" do
22
- it "contains the document" do
23
- expect(response).to eq document
22
+ describe "document" do
23
+ it "returns the document" do
24
+ expect(response.document).to eq document
25
+ end
24
26
  end
25
27
  end
26
28
  end
@@ -52,7 +52,7 @@ module ElasticAdapter
52
52
  end
53
53
 
54
54
  it "updates the document" do
55
- expect(subject.get(1)[:foo]).to eq "baz"
55
+ expect(subject.get(1).document[:foo]).to eq "baz"
56
56
  end
57
57
  end
58
58
  end
@@ -17,19 +17,19 @@ module ElasticAdapter
17
17
 
18
18
  context "match_all" do
19
19
  it "returns all documents" do
20
- expect(subject.search({query: {match_all: {}}}).count).to eq 2
20
+ expect(subject.search({query: {match_all: {}}}).hits.count).to eq 2
21
21
  end
22
22
  end
23
23
 
24
24
  context "zoo" do
25
25
  let(:response) { subject.search(query: {match: {foo: "zoo"}})}
26
26
  it "returns one document" do
27
- expect(response.count).to eq 1
27
+ expect(response.hits.count).to eq 1
28
28
  end
29
29
 
30
30
  it "returns the wanted document" do
31
- expect(response[:hits].first[:id]).to eq "2"
32
- expect(response[:hits].first[:foo]).to eq "zoo"
31
+ expect(response.hits.first[:id]).to eq "2"
32
+ expect(response.hits.first[:foo]).to eq "zoo"
33
33
  end
34
34
  end
35
35
  end
@@ -1,6 +1,6 @@
1
1
  RSpec.shared_examples "response without exception" do
2
2
  it "is a Response" do
3
- expect(response).to be_a ElasticAdapter::Response
3
+ expect(response).to be_a ElasticAdapter::Responses::SanitizedResponse
4
4
  end
5
5
 
6
6
  it "has no exception" do
@@ -10,7 +10,7 @@ end
10
10
 
11
11
  RSpec.shared_examples "response with exception" do
12
12
  it "is a Response" do
13
- expect(response).to be_a ElasticAdapter::Response
13
+ expect(response).to be_a ElasticAdapter::Responses::SanitizedResponse
14
14
  end
15
15
 
16
16
  it "has an exception" do
@@ -31,13 +31,12 @@ module ElasticAdapter
31
31
  let(:response) { subject.suggest(query)}
32
32
 
33
33
  it "returns one result" do
34
- expect(response.count).to eq 1
34
+ expect(response.suggestions.first.terms.first.options.count).to eq 1
35
35
  end
36
36
 
37
37
  it "returns bar" do
38
- expect(response[:options].first).to include text: "bar"
38
+ expect(response.suggestions.first.terms.first.options.first[:text]).to eq "bar"
39
39
  end
40
-
41
40
  end
42
41
  end
43
42
  end
@@ -22,12 +22,14 @@ module ElasticAdapter
22
22
 
23
23
  let(:response) { subject.validate(query) }
24
24
 
25
- it "is false" do
26
- expect(response).to eq false
25
+ describe "valid?" do
26
+ it "is false" do
27
+ expect(response.valid?).to be false
28
+ end
27
29
  end
28
30
 
29
31
  it "is a response" do
30
- expect(response).to be_a Decoration::ValidationResponse
32
+ expect(response).to be_a Responses::ValidationResponse
31
33
  end
32
34
  end
33
35
 
@@ -43,11 +45,11 @@ module ElasticAdapter
43
45
  let(:response) { subject.validate(query) }
44
46
 
45
47
  it "is true" do
46
- expect(response).to eq true
48
+ expect(response.valid?).to eq true
47
49
  end
48
50
 
49
51
  it "is a response" do
50
- expect(response).to be_a Decoration::ValidationResponse
52
+ expect(response).to be_a Responses::ValidationResponse
51
53
  end
52
54
  end
53
55
  end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  module ElasticAdapter
4
- module Decoration
4
+ module Responses
5
5
  describe AggregationResponse do
6
6
  let(:response){{
7
7
  aggregations: {
@@ -52,7 +52,6 @@ module ElasticAdapter
52
52
  end
53
53
  end
54
54
  end
55
-
56
55
  end
57
56
  end
58
57
  end
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,24 @@
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
@@ -0,0 +1,29 @@
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