elasticated 2.5.5 → 3.0.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.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -2
  3. data/Rakefile +52 -1
  4. data/elasticated.gemspec +3 -1
  5. data/lib/elasticated.rb +20 -24
  6. data/lib/elasticated/aggregation.rb +3 -6
  7. data/lib/elasticated/aggregations/date_histogram_aggregation.rb +6 -1
  8. data/lib/elasticated/aggregations/filter_aggregation.rb +8 -12
  9. data/lib/elasticated/aggregations/filter_aggregation_evaluator.rb +1 -1
  10. data/lib/elasticated/aggregations/group_aggregation.rb +14 -11
  11. data/lib/elasticated/aggregations/range_aggregation.rb +10 -11
  12. data/lib/elasticated/aggregations/range_aggregation_evaluator.rb +1 -1
  13. data/lib/elasticated/aggregations/ranges_builder.rb +2 -2
  14. data/lib/elasticated/aggregations/safe_date_histogram_aggregation.rb +7 -2
  15. data/lib/elasticated/aggregations/subaggregated.rb +1 -1
  16. data/lib/elasticated/boolean_clause.rb +4 -3
  17. data/lib/elasticated/bulk_actions/create_action.rb +14 -0
  18. data/lib/elasticated/bulk_actions/delete_action.rb +30 -0
  19. data/lib/elasticated/bulk_actions/index_action.rb +35 -0
  20. data/lib/elasticated/bulk_actions/standard_action.rb +22 -0
  21. data/lib/elasticated/bulk_actions/update_action.rb +44 -0
  22. data/lib/elasticated/bulk_actions/upsert_action.rb +14 -0
  23. data/lib/elasticated/bulk_request.rb +58 -0
  24. data/lib/elasticated/bulk_request/response.rb +32 -0
  25. data/lib/elasticated/bulk_request/response_item.rb +39 -0
  26. data/lib/elasticated/client.rb +27 -3
  27. data/lib/elasticated/conditions/custom_condition.rb +3 -3
  28. data/lib/elasticated/conditions/range_condition.rb +5 -2
  29. data/lib/elasticated/conditions/script_condition.rb +3 -3
  30. data/lib/elasticated/conditions/standard_condition.rb +4 -5
  31. data/lib/elasticated/conditions/term_condition.rb +22 -0
  32. data/lib/elasticated/conditions/terms_condition.rb +2 -2
  33. data/lib/elasticated/conditions_builder.rb +19 -4
  34. data/lib/elasticated/delimiters/date_field_delimiter.rb +21 -12
  35. data/lib/elasticated/delimiters/standard_field_delimiter.rb +18 -2
  36. data/lib/elasticated/delimiters/term_field_delimiter.rb +6 -5
  37. data/lib/elasticated/document.rb +20 -1
  38. data/lib/elasticated/enum.rb +17 -0
  39. data/lib/elasticated/index_selector.rb +26 -25
  40. data/lib/elasticated/mapping.rb +2 -4
  41. data/lib/elasticated/mapping/builder.rb +3 -2
  42. data/lib/elasticated/mapping/fields_builder.rb +13 -9
  43. data/lib/elasticated/mapping/object_builder.rb +38 -4
  44. data/lib/elasticated/mapping/type_builder.rb +3 -5
  45. data/lib/elasticated/mixins/block_evaluation.rb +17 -0
  46. data/lib/elasticated/mixins/clonable.rb +60 -0
  47. data/lib/elasticated/mixins/configurable.rb +22 -0
  48. data/lib/elasticated/mixins/inspectionable.rb +16 -0
  49. data/lib/elasticated/partitioned_repository.rb +24 -18
  50. data/lib/elasticated/query.rb +27 -21
  51. data/lib/elasticated/query_aggregations.rb +5 -7
  52. data/lib/elasticated/query_conditions.rb +6 -3
  53. data/lib/elasticated/quick.rb +7 -0
  54. data/lib/elasticated/repository.rb +184 -40
  55. data/lib/elasticated/repository/intelligent_search.rb +3 -3
  56. data/lib/elasticated/repository/normal_search.rb +2 -2
  57. data/lib/elasticated/repository/resumable_search.rb +5 -5
  58. data/lib/elasticated/repository/scan_scroll_search.rb +4 -4
  59. data/lib/elasticated/repository/scroll_search.rb +3 -3
  60. data/lib/elasticated/repository/search.rb +7 -0
  61. data/lib/elasticated/repository/single_page_search.rb +1 -1
  62. data/lib/elasticated/results.rb +14 -0
  63. data/lib/version.rb +18 -25
  64. data/spec/aggregation_spec.rb +95 -16
  65. data/spec/bulk_request_spec.rb +158 -0
  66. data/spec/date_field_delimiter_spec.rb +50 -6
  67. data/spec/document_spec.rb +1 -5
  68. data/spec/integration_spec.rb +7 -7
  69. data/spec/mapping_spec.rb +128 -8
  70. data/spec/partitioned_repository_spec.rb +218 -0
  71. data/spec/query_conditions_spec.rb +98 -45
  72. data/spec/query_spec.rb +21 -28
  73. data/spec/repository_spec.rb +245 -0
  74. data/spec/results_spec.rb +0 -4
  75. data/spec/sample_responses/elasticsearch_bulk_response_1.json +35 -0
  76. data/spec/sample_responses/elasticsearch_bulk_response_2.json +20 -0
  77. data/spec/sample_responses/elasticsearch_count_1.json +8 -0
  78. data/spec/sample_responses/elasticsearch_count_2.json +8 -0
  79. data/spec/sample_responses/elasticsearch_get_response_1.json +10 -0
  80. data/spec/sample_responses/elasticsearch_get_response_2.json +6 -0
  81. data/spec/{elasticsearch_hit_1.json → sample_responses/elasticsearch_hit_1.json} +0 -0
  82. data/spec/sample_responses/elasticsearch_mget_response_1.json +25 -0
  83. data/spec/{elasticsearch_response_1.json → sample_responses/elasticsearch_response_1.json} +0 -0
  84. data/spec/{elasticsearch_response_2.json → sample_responses/elasticsearch_response_2.json} +0 -0
  85. data/spec/{elasticsearch_top_hits_response.json → sample_responses/elasticsearch_top_hits_response.json} +0 -0
  86. data/spec/spec_helper.rb +47 -0
  87. data/spec/spec_helper/fake_index_selector.rb +27 -0
  88. data/spec/term_field_delimiter_spec.rb +8 -8
  89. metadata +80 -26
  90. data/lib/elasticated/block_evaluation.rb +0 -15
  91. data/lib/elasticated/clonable.rb +0 -58
  92. data/lib/elasticated/configurable.rb +0 -20
  93. data/lib/elasticated/date_delimiter_factory.rb +0 -123
  94. data/lib/elasticated/delimiter_visitor.rb +0 -53
  95. data/lib/elasticated/inspectionable.rb +0 -9
  96. data/lib/elasticated/strategy_params_for_query_service.rb +0 -14
  97. data/lib/elasticated/term_delimiter_factory.rb +0 -73
  98. data/spec/delimiter_factory_spec.rb +0 -399
  99. data/spec/strategy_params_for_query_service_spec.rb +0 -387
@@ -14,15 +14,59 @@ module Elasticated
14
14
  end
15
15
 
16
16
  it "should delimit by one term" do
17
- completed_delimiter = delimiter.completed_with conditions: [{created_at: '2016-07-19'}]
18
- params = completed_delimiter.build_strategy_params
17
+ delimiter.add_term :created_at, '2016-07-19'
18
+ params = delimiter.build_strategy_params
19
19
  expect(params).to eq date: '2016-07-19'
20
20
  end
21
21
 
22
- it "should delimit by multiple terms" do
23
- completed_delimiter = delimiter.completed_with conditions: [{created_at: '2017-03-30'},{created_at: '2017-03-25'},{created_at: '2017-03-27'}]
24
- params = completed_delimiter.build_strategy_params
25
- expect(params).to eq date_since: '2017-03-25', date_until: '2017-03-30'
22
+ it "should raise when delimit by multiple terms" do
23
+ delimiter.add_term :created_at, 'nombre1'
24
+ delimiter.add_term :created_at, 'nombre2'
25
+ expect{ delimiter.build_strategy_params }.to raise_error RuntimeError
26
+ end
27
+
28
+ it "should delimit by a min date" do
29
+ delimiter.set_minimum :created_at, '2016-07-19'
30
+ params = delimiter.build_strategy_params
31
+ expect(params).to eq date_since: '2016-07-19'
32
+ end
33
+
34
+ it "should delimit by a max date" do
35
+ delimiter.set_maximum :created_at, '2016-07-19'
36
+ params = delimiter.build_strategy_params
37
+ expect(params).to eq date_until: '2016-07-19'
38
+ end
39
+
40
+ it "should delimit by a range of dates" do
41
+ delimiter.set_minimum :created_at, '2016-07-19'
42
+ delimiter.set_maximum :created_at, '2016-07-21'
43
+ params = delimiter.build_strategy_params
44
+ expect(params).to eq date_since: '2016-07-19', date_until: '2016-07-21'
45
+ end
46
+
47
+ it "should delimit by the most restricted range of dates" do
48
+ delimiter.set_minimum :created_at, '2016-07-19'
49
+ delimiter.set_maximum :created_at, '2016-07-25'
50
+ delimiter.set_minimum :created_at, '2016-07-17' # ignored minimum
51
+ delimiter.set_maximum :created_at, '2016-07-24'
52
+ delimiter.set_maximum :created_at, '2016-07-26' # ignored maximum
53
+ params = delimiter.build_strategy_params
54
+ expect(params).to eq date_since: '2016-07-19', date_until: '2016-07-24'
55
+ end
56
+
57
+ it "should prefer a term over a range of dates" do
58
+ delimiter.set_minimum :created_at, '2016-07-19'
59
+ delimiter.set_maximum :created_at, '2016-07-21'
60
+ delimiter.add_term :created_at, '2016-07-20'
61
+ params = delimiter.build_strategy_params
62
+ expect(params).to eq date: '2016-07-20'
63
+ end
64
+
65
+ it "should delimit only by the minimum date" do
66
+ delimiter.set_minimum :created_at, '2016-07-19'
67
+ delimiter.set_maximum :created_at, :now
68
+ params = delimiter.build_strategy_params
69
+ expect(params).to eq date_since: '2016-07-19'
26
70
  end
27
71
 
28
72
  end
@@ -3,12 +3,8 @@ require_relative 'spec_helper'
3
3
  module Elasticated
4
4
  describe Document do
5
5
 
6
- def open_hit(name)
7
- JSON.parse File.read "spec/#{name}.json"
8
- end
9
-
10
6
  let :hit do
11
- open_hit 'elasticsearch_hit_1'
7
+ open_response 'elasticsearch_hit_1'
12
8
  end
13
9
 
14
10
  it "should parse an elasticsearch hit" do
@@ -21,13 +21,13 @@ module Elasticated
21
21
  query: {
22
22
  filtered: {
23
23
  filter: {
24
- terms: { age: [23] }
24
+ term: { age: 23 }
25
25
  },
26
26
  query: {
27
27
  bool: {
28
28
  must: [
29
- { terms: { first_name: [:pablo] } },
30
- { terms: { last_name: [:fernandez] } }
29
+ { term: { first_name: :pablo } },
30
+ { term: { last_name: :fernandez } }
31
31
  ]
32
32
  }
33
33
  }
@@ -88,7 +88,7 @@ module Elasticated
88
88
  terms: { field: :user, size: 0 },
89
89
  aggs: {
90
90
  element_id: {
91
- filter: { terms: { element_id: [2] } }
91
+ filter: { term: { element_id: 2 } }
92
92
  }
93
93
  }
94
94
  },
@@ -130,13 +130,13 @@ module Elasticated
130
130
  query: {
131
131
  filtered: {
132
132
  filter: {
133
- terms: { age: [23] }
133
+ term: { age: 23 }
134
134
  },
135
135
  query: {
136
136
  bool: {
137
137
  must: [
138
- { terms: { first_name: [:pablo] } },
139
- { terms: { last_name: [:fernandez] } }
138
+ { term: { first_name: :pablo } },
139
+ { term: { last_name: :fernandez } }
140
140
  ]
141
141
  }
142
142
  }
data/spec/mapping_spec.rb CHANGED
@@ -3,6 +3,52 @@ require_relative 'spec_helper'
3
3
  module Elasticated
4
4
  describe Mapping do
5
5
 
6
+ context "the dynamic attribute" do
7
+
8
+ it "should be setted to 'true'" do
9
+ result = Mapping.build do
10
+ type :a_type do
11
+ accept_new_fields
12
+ end
13
+ end
14
+ expected_result = {
15
+ a_type: {
16
+ dynamic: true
17
+ }
18
+ }
19
+ expect(result.to_h).to eq expected_result
20
+ end
21
+
22
+ it "should be setted to 'false'" do
23
+ result = Mapping.build do
24
+ type :a_type do
25
+ ignore_new_fields
26
+ end
27
+ end
28
+ expected_result = {
29
+ a_type: {
30
+ dynamic: false
31
+ }
32
+ }
33
+ expect(result.to_h).to eq expected_result
34
+ end
35
+
36
+ it "should be setted to 'strict'" do
37
+ result = Mapping.build do
38
+ type :a_type do
39
+ strict
40
+ end
41
+ end
42
+ expected_result = {
43
+ a_type: {
44
+ dynamic: 'strict'
45
+ }
46
+ }
47
+ expect(result.to_h).to eq expected_result
48
+ end
49
+
50
+ end
51
+
6
52
  it "should build a single integer property" do
7
53
  result = Mapping.build do |mapping|
8
54
  mapping.type :a_type do |type|
@@ -16,7 +62,7 @@ module Elasticated
16
62
  }
17
63
  }
18
64
  }
19
- expect(result).to eq expected_result
65
+ expect(result.to_h).to eq expected_result
20
66
  end
21
67
 
22
68
  it "should build a single long property" do
@@ -32,7 +78,39 @@ module Elasticated
32
78
  }
33
79
  }
34
80
  }
35
- expect(result).to eq expected_result
81
+ expect(result.to_h).to eq expected_result
82
+ end
83
+
84
+ it "should build a single double property" do
85
+ result = Mapping.build do |mapping|
86
+ mapping.type :a_type do |type|
87
+ type.double :a_double
88
+ end
89
+ end
90
+ expected_result = {
91
+ a_type: {
92
+ properties: {
93
+ a_double: { type: :double, fielddata: { format: :doc_values } }
94
+ }
95
+ }
96
+ }
97
+ expect(result.to_h).to eq expected_result
98
+ end
99
+
100
+ it "should build a single float property" do
101
+ result = Mapping.build do |mapping|
102
+ mapping.type :a_type do |type|
103
+ type.float :a_float
104
+ end
105
+ end
106
+ expected_result = {
107
+ a_type: {
108
+ properties: {
109
+ a_float: { type: :float, fielddata: { format: :doc_values } }
110
+ }
111
+ }
112
+ }
113
+ expect(result.to_h).to eq expected_result
36
114
  end
37
115
 
38
116
  it "should build a single date property" do
@@ -48,7 +126,7 @@ module Elasticated
48
126
  }
49
127
  }
50
128
  }
51
- expect(result).to eq expected_result
129
+ expect(result.to_h).to eq expected_result
52
130
  end
53
131
 
54
132
  it "should build a single string property" do
@@ -64,7 +142,7 @@ module Elasticated
64
142
  }
65
143
  }
66
144
  }
67
- expect(result).to eq expected_result
145
+ expect(result.to_h).to eq expected_result
68
146
  end
69
147
 
70
148
  it "should build a single analyzed string property" do
@@ -86,7 +164,7 @@ module Elasticated
86
164
  }
87
165
  }
88
166
  }
89
- expect(result).to eq expected_result
167
+ expect(result.to_h).to eq expected_result
90
168
  end
91
169
 
92
170
  it "should build a single bool property" do
@@ -102,7 +180,7 @@ module Elasticated
102
180
  }
103
181
  }
104
182
  }
105
- expect(result).to eq expected_result
183
+ expect(result.to_h).to eq expected_result
106
184
  end
107
185
 
108
186
  it "should build a single object" do
@@ -125,7 +203,28 @@ module Elasticated
125
203
  }
126
204
  }
127
205
  }
128
- expect(result).to eq expected_result
206
+ expect(result.to_h).to eq expected_result
207
+ end
208
+
209
+ it "should build a single object with dynamic mapping" do
210
+ result = Mapping.build do |mapping|
211
+ mapping.type :a_type do |type|
212
+ type.object :an_object do |object|
213
+ object.accept_new_fields
214
+ end
215
+ end
216
+ end
217
+ expected_result = {
218
+ a_type: {
219
+ properties: {
220
+ an_object: {
221
+ type: :object,
222
+ dynamic: true
223
+ }
224
+ }
225
+ }
226
+ }
227
+ expect(result.to_h).to eq expected_result
129
228
  end
130
229
 
131
230
  it "should build a single nested" do
@@ -150,7 +249,28 @@ module Elasticated
150
249
  }
151
250
  }
152
251
  }
153
- expect(result).to eq expected_result
252
+ expect(result.to_h).to eq expected_result
253
+ end
254
+
255
+ it "should build a partial" do
256
+ partial = Mapping.partial do |type|
257
+ type.string :b_name
258
+ end
259
+ result = Mapping.build do |mapping|
260
+ mapping.type :my_type do |type|
261
+ type.string :a_name
262
+ type.partial partial
263
+ end
264
+ end
265
+ expected_result = {
266
+ my_type: {
267
+ properties: {
268
+ a_name: { type: :string, index: :not_analyzed, fielddata: { format: :doc_values } },
269
+ b_name: { type: :string, index: :not_analyzed, fielddata: { format: :doc_values } }
270
+ }
271
+ }
272
+ }
273
+ expect(result.to_h).to eq expected_result
154
274
  end
155
275
 
156
276
  it "should build a complex mapping" do
@@ -0,0 +1,218 @@
1
+ require_relative 'spec_helper'
2
+
3
+ module Elasticated
4
+ describe PartitionedRepository do
5
+
6
+ let(:transport){ double }
7
+ let(:repository){ PartitionedRepository.new SpecHelper::FakeIndexSelector.new }
8
+ let(:query){ Query.new }
9
+
10
+ before :each do
11
+ allow(repository.client).to receive(:transport).and_return transport
12
+ end
13
+
14
+ context "on an index refresh" do
15
+
16
+ let(:indices){ double }
17
+
18
+ before :each do
19
+ allow(transport).to receive(:indices).and_return indices
20
+ end
21
+
22
+ it "should affect the general alias" do
23
+ query = Query.new
24
+ expect(indices).to receive(:refresh).with(index: 'fakeindex')
25
+ repository.refresh_indices_for(query)
26
+ end
27
+
28
+ it "should affect just one partition" do
29
+ query = Query.build{ terms :created_at, '2017-01-05' }
30
+ expect(indices).to receive(:refresh).with(index: 'fakeindex-2017-01-05')
31
+ repository.refresh_indices_for(query)
32
+ end
33
+
34
+ it "should affect multiple partitions" do
35
+ query = Query.build{ range :created_at, '2017-01-04', '2017-01-05' }
36
+ expect(indices).to receive(:refresh).with(index: 'fakeindex-2017-01-04,fakeindex-2017-01-05')
37
+ repository.refresh_indices_for(query)
38
+ end
39
+
40
+ end
41
+
42
+ context "on a search" do
43
+
44
+ let(:response){ open_response 'elasticsearch_response_1' }
45
+
46
+ it "should affect the general alias" do
47
+ query = Query.new
48
+ expect(transport).to receive(:search).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex').and_return response
49
+ repository.execute_search query, type: 'faketype', as: :single_page
50
+ end
51
+
52
+ it "should affect just one partition" do
53
+ query = Query.build{ terms :created_at, '2017-01-05' }
54
+ expect(transport).to receive(:search).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex-2017-01-05').and_return response
55
+ repository.execute_search query, type: 'faketype', as: :single_page
56
+ end
57
+
58
+ it "should affect multiple partitions" do
59
+ query = Query.build{ range :created_at, '2017-01-04', '2017-01-05' }
60
+ expect(transport).to receive(:search).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex-2017-01-04,fakeindex-2017-01-05').and_return response
61
+ repository.execute_search query, type: 'faketype', as: :single_page
62
+ end
63
+
64
+ end
65
+
66
+ context "on a delete by query" do
67
+
68
+ it "should affect the general alias" do
69
+ query = Query.new
70
+ expect(transport).to receive(:delete_by_query).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex')
71
+ repository.delete_by query, type: 'faketype'
72
+ end
73
+
74
+ it "should affect just one partition" do
75
+ query = Query.build{ terms :created_at, '2017-01-05' }
76
+ expect(transport).to receive(:delete_by_query).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex-2017-01-05')
77
+ repository.delete_by query, type: 'faketype'
78
+ end
79
+
80
+ it "should affect multiple partitions" do
81
+ query = Query.build{ range :created_at, '2017-01-04', '2017-01-05' }
82
+ expect(transport).to receive(:delete_by_query).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex-2017-01-04,fakeindex-2017-01-05')
83
+ repository.delete_by query, type: 'faketype'
84
+ end
85
+
86
+ end
87
+
88
+ context "on a count" do
89
+
90
+ let(:response){ open_response 'elasticsearch_count_1' }
91
+
92
+ it "should affect the general alias" do
93
+ query = Query.new
94
+ expect(transport).to receive(:count).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex').and_return response
95
+ repository.execute_count query, type: 'faketype'
96
+ end
97
+
98
+ it "should affect just one partition" do
99
+ query = Query.build{ terms :created_at, '2017-01-05' }
100
+ expect(transport).to receive(:count).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex-2017-01-05').and_return response
101
+ repository.execute_count query, type: 'faketype'
102
+ end
103
+
104
+ it "should affect multiple partitions" do
105
+ query = Query.build{ range :created_at, '2017-01-04', '2017-01-05' }
106
+ expect(transport).to receive(:count).with(body: query.build_for_search, type: 'faketype', index: 'fakeindex-2017-01-04,fakeindex-2017-01-05').and_return response
107
+ repository.execute_count query, type: 'faketype'
108
+ end
109
+
110
+ end
111
+
112
+ context "on an upsert" do
113
+
114
+ let(:source){ { 'name' => 'Pablo', 'created_at' => '2017-01-05' } }
115
+ let(:document){ Document.create id: 'my_id', type: 'my_type', index: 'my_index', source: source }
116
+
117
+ it "should select the correct index on an indexation" do
118
+ expect(transport).to receive(:index).with(id: 'my_id', type: 'my_type', index: 'fakeindex-2017-01-05', body: source)
119
+ repository.index_document document
120
+ end
121
+
122
+ end
123
+
124
+ context "on a partial update" do
125
+
126
+ let(:source){ { 'some_field' => 'some_value' } }
127
+
128
+ it "should select the correct index based on the user params" do
129
+ expect(transport).to receive(:update).with(id: 'my_id', type: 'my_type', index: 'fakeindex-2017-01-05', body: { doc: source })
130
+ repository.update_document 'my_id', type: 'my_type', source: source, index_for: { date: '2017-01-05' }
131
+ end
132
+
133
+ it "should raise if the user doesnt give the index selection params" do
134
+ expect{ repository.update_document 'my_id', type: 'my_type', source: source }.to raise_error RuntimeError
135
+ end
136
+
137
+ it "should raise if the user give params affecting multiple indices" do
138
+ expect{ repository.update_document 'my_id', type: 'my_type', source: source, index_for: { date_since: '2017-01-04', date_until: '2017-01-05' } }.to raise_error RuntimeError
139
+ end
140
+
141
+ end
142
+
143
+ context "on a get" do
144
+
145
+ it "should select the correct index based on the user params (on a get)" do
146
+ response = open_response 'elasticsearch_get_response_1'
147
+ expect(transport).to receive(:get).with(id: 'my_doc_id', index: 'fakeindex-2017-01-05').and_return response
148
+ repository.get_document 'my_doc_id', index_for: { date: '2017-01-05' }
149
+ end
150
+
151
+ it "should raise if the user doesnt give the index selection params (on a get)" do
152
+ expect{ repository.get_document 'my_doc_id' }.to raise_error RuntimeError
153
+ end
154
+
155
+ it "should raise if the user give params affecting multiple indices (on a get)" do
156
+ expect{ repository.get_document 'my_doc_id', index_for: { date_since: '2017-01-04', date_until: '2017-01-05' } }.to raise_error RuntimeError
157
+ end
158
+
159
+ end
160
+
161
+ context "on a delete" do
162
+
163
+ it "should select the correct index based on the user params (on a delete)" do
164
+ expect(transport).to receive(:delete).with(id: 'my_doc_id', index: 'fakeindex-2017-01-05')
165
+ repository.delete_document 'my_doc_id', index_for: { date: '2017-01-05' }
166
+ end
167
+
168
+ it "should raise if the user doesnt give the index selection params (on a delete)" do
169
+ expect{ repository.delete_document 'my_doc_id' }.to raise_error RuntimeError
170
+ end
171
+
172
+ it "should raise if the user give params affecting multiple indices (on a delete)" do
173
+ expect{ repository.delete_document 'my_doc_id', index_for: { date_since: '2017-01-04', date_until: '2017-01-05' } }.to raise_error RuntimeError
174
+ end
175
+
176
+ end
177
+
178
+ context "on a mget" do
179
+
180
+ it "should select the correct index based on the user params (on a mget)" do
181
+ response = open_response 'elasticsearch_mget_response_1'
182
+ expect(transport).to receive(:mget).with(body: { ids: ['id1', 'id2'] }, index: 'fakeindex-2017-01-05').and_return response
183
+ repository.get_document ['id1', 'id2'], index_for: { date: '2017-01-05' }
184
+ end
185
+
186
+ it "should raise if the user doesnt give the index selection params (on a mget)" do
187
+ expect{ repository.get_document ['id1', 'id2'] }.to raise_error RuntimeError
188
+ end
189
+
190
+ it "should raise if the user give params affecting multiple indices (on a mget)" do
191
+ expect{ repository.get_document ['id1', 'id2'], index_for: { date_since: '2017-01-04', date_until: '2017-01-05' } }.to raise_error RuntimeError
192
+ end
193
+
194
+ end
195
+
196
+ context "on a percolation" do
197
+
198
+ let(:query){ Query.new }
199
+
200
+ before :each do
201
+ allow(DateTime).to receive(:now).and_return DateTime.parse '2017-01-05T00:00:00.000Z'
202
+ end
203
+
204
+ it "should select the 'DateTime.now' index" do
205
+ expect(transport).to receive(:index).with(index: 'fakeindex-2017-01-05', type: '.percolator', id: 'my_id', body: { query: query.build_for_count })
206
+ repository.create_percolator query, id: 'my_id'
207
+ end
208
+
209
+ it "should check a percolator" do
210
+ document = Document.new user: 'Pablo', created_at: '2016-12-15'
211
+ expect(transport).to receive(:percolate).with(index: 'fakeindex-2017-01-05', type: 'my_type', body: { doc: { user: 'Pablo', created_at: '2016-12-15' } })
212
+ repository.percolate_document document, type: 'my_type'
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+ end