elasticsearch-model 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -1
- data/README.md +1 -1
- data/examples/activerecord_mapping_completion.rb +69 -0
- data/examples/mongoid_article.rb +2 -2
- data/lib/elasticsearch/model.rb +1 -1
- data/lib/elasticsearch/model/adapters/mongoid.rb +2 -12
- data/lib/elasticsearch/model/adapters/multiple.rb +8 -6
- data/lib/elasticsearch/model/importing.rb +3 -0
- data/lib/elasticsearch/model/indexing.rb +62 -8
- data/lib/elasticsearch/model/proxy.rb +2 -1
- data/lib/elasticsearch/model/response.rb +6 -0
- data/lib/elasticsearch/model/response/pagination.rb +25 -6
- data/lib/elasticsearch/model/response/results.rb +1 -1
- data/lib/elasticsearch/model/version.rb +1 -1
- data/test/integration/active_record_basic_test.rb +29 -4
- data/test/integration/multiple_models_test.rb +44 -26
- data/test/support/model.json +1 -0
- data/test/support/model.yml +2 -0
- data/test/unit/adapter_mongoid_test.rb +3 -1
- data/test/unit/importing_test.rb +39 -12
- data/test/unit/indexing_test.rb +111 -22
- data/test/unit/response_pagination_kaminari_test.rb +208 -8
- data/test/unit/response_pagination_will_paginate_test.rb +204 -14
- data/test/unit/response_test.rb +11 -1
- metadata +7 -2
@@ -1,16 +1,17 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Elasticsearch::Model::ResponsePaginationKaminariTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
include ::Kaminari::ConfigurationMethods
|
4
|
+
class ModelClass
|
5
|
+
include ::Kaminari::ConfigurationMethods
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def self.index_name; 'foo'; end
|
8
|
+
def self.document_type; 'bar'; end
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'},
|
12
|
+
'hits' => { 'total' => 100, 'hits' => (1..100).to_a.map { |i| { _id: i } } } }
|
13
|
+
|
14
|
+
context "Response pagination" do
|
14
15
|
|
15
16
|
setup do
|
16
17
|
@search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*'
|
@@ -86,6 +87,205 @@ class Elasticsearch::Model::ResponsePaginationKaminariTest < Test::Unit::TestCas
|
|
86
87
|
end
|
87
88
|
end
|
88
89
|
|
90
|
+
context "limit setter" do
|
91
|
+
setup do
|
92
|
+
@response.records
|
93
|
+
@response.results
|
94
|
+
end
|
95
|
+
|
96
|
+
should "set the values" do
|
97
|
+
@response.limit(35)
|
98
|
+
assert_equal 35, @response.search.definition[:size]
|
99
|
+
end
|
100
|
+
|
101
|
+
should "reset the variables" do
|
102
|
+
@response.limit(35)
|
103
|
+
|
104
|
+
assert_nil @response.instance_variable_get(:@response)
|
105
|
+
assert_nil @response.instance_variable_get(:@records)
|
106
|
+
assert_nil @response.instance_variable_get(:@results)
|
107
|
+
end
|
108
|
+
|
109
|
+
should 'coerce string parameters' do
|
110
|
+
@response.limit("35")
|
111
|
+
assert_equal 35, @response.search.definition[:size]
|
112
|
+
end
|
113
|
+
|
114
|
+
should 'ignore invalid string parameters' do
|
115
|
+
@response.limit(35)
|
116
|
+
@response.limit("asdf")
|
117
|
+
assert_equal 35, @response.search.definition[:size]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "with the page() and limit() methods" do
|
122
|
+
setup do
|
123
|
+
@response.records
|
124
|
+
@response.results
|
125
|
+
end
|
126
|
+
|
127
|
+
should "set the values" do
|
128
|
+
@response.page(3).limit(35)
|
129
|
+
assert_equal 35, @response.search.definition[:size]
|
130
|
+
assert_equal 70, @response.search.definition[:from]
|
131
|
+
end
|
132
|
+
|
133
|
+
should "set the values when limit is called first" do
|
134
|
+
@response.limit(35).page(3)
|
135
|
+
assert_equal 35, @response.search.definition[:size]
|
136
|
+
assert_equal 70, @response.search.definition[:from]
|
137
|
+
end
|
138
|
+
|
139
|
+
should "reset the instance variables" do
|
140
|
+
@response.page(3).limit(35)
|
141
|
+
|
142
|
+
assert_nil @response.instance_variable_get(:@response)
|
143
|
+
assert_nil @response.instance_variable_get(:@records)
|
144
|
+
assert_nil @response.instance_variable_get(:@results)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "offset setter" do
|
149
|
+
setup do
|
150
|
+
@response.records
|
151
|
+
@response.results
|
152
|
+
end
|
153
|
+
|
154
|
+
should "set the values" do
|
155
|
+
@response.offset(15)
|
156
|
+
assert_equal 15, @response.search.definition[:from]
|
157
|
+
end
|
158
|
+
|
159
|
+
should "reset the variables" do
|
160
|
+
@response.offset(35)
|
161
|
+
|
162
|
+
assert_nil @response.instance_variable_get(:@response)
|
163
|
+
assert_nil @response.instance_variable_get(:@records)
|
164
|
+
assert_nil @response.instance_variable_get(:@results)
|
165
|
+
end
|
166
|
+
|
167
|
+
should 'coerce string parameters' do
|
168
|
+
@response.offset("35")
|
169
|
+
assert_equal 35, @response.search.definition[:from]
|
170
|
+
end
|
171
|
+
|
172
|
+
should 'coerce invalid string parameters' do
|
173
|
+
@response.offset(35)
|
174
|
+
@response.offset("asdf")
|
175
|
+
assert_equal 0, @response.search.definition[:from]
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
context "total" do
|
180
|
+
should "return the number of hits" do
|
181
|
+
@response.expects(:results).returns(mock('results', total: 100))
|
182
|
+
assert_equal 100, @response.total_count
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context "results" do
|
187
|
+
setup do
|
188
|
+
@search.stubs(:execute!).returns RESPONSE
|
189
|
+
end
|
190
|
+
|
191
|
+
should "return current page and total count" do
|
192
|
+
assert_equal 1, @response.page(1).results.current_page
|
193
|
+
assert_equal 100, @response.results.total_count
|
194
|
+
|
195
|
+
assert_equal 5, @response.page(5).results.current_page
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context "records" do
|
200
|
+
setup do
|
201
|
+
@search.stubs(:execute!).returns RESPONSE
|
202
|
+
end
|
203
|
+
|
204
|
+
should "return current page and total count" do
|
205
|
+
assert_equal 1, @response.page(1).records.current_page
|
206
|
+
assert_equal 100, @response.records.total_count
|
207
|
+
|
208
|
+
assert_equal 5, @response.page(5).records.current_page
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
context "Multimodel response pagination" do
|
214
|
+
setup do
|
215
|
+
@multimodel = Elasticsearch::Model::Multimodel.new(ModelClass)
|
216
|
+
@search = Elasticsearch::Model::Searching::SearchRequest.new @multimodel, '*'
|
217
|
+
@response = Elasticsearch::Model::Response::Response.new @multimodel, @search, RESPONSE
|
218
|
+
@response.klass.stubs(:client).returns mock('client')
|
219
|
+
end
|
220
|
+
|
221
|
+
should "have pagination methods" do
|
222
|
+
assert_respond_to @response, :page
|
223
|
+
assert_respond_to @response, :limit_value
|
224
|
+
assert_respond_to @response, :offset_value
|
225
|
+
assert_respond_to @response, :limit
|
226
|
+
assert_respond_to @response, :offset
|
227
|
+
assert_respond_to @response, :total_count
|
228
|
+
end
|
229
|
+
|
230
|
+
context "#page method" do
|
231
|
+
should "advance the from/size" do
|
232
|
+
@response.klass.client
|
233
|
+
.expects(:search)
|
234
|
+
.with do |definition|
|
235
|
+
assert_equal 25, definition[:from]
|
236
|
+
assert_equal 25, definition[:size]
|
237
|
+
true
|
238
|
+
end
|
239
|
+
.returns(RESPONSE)
|
240
|
+
|
241
|
+
assert_nil @response.search.definition[:from]
|
242
|
+
assert_nil @response.search.definition[:size]
|
243
|
+
|
244
|
+
@response.page(2).to_a
|
245
|
+
assert_equal 25, @response.search.definition[:from]
|
246
|
+
assert_equal 25, @response.search.definition[:size]
|
247
|
+
end
|
248
|
+
|
249
|
+
should "advance the from/size further" do
|
250
|
+
@response.klass.client
|
251
|
+
.expects(:search)
|
252
|
+
.with do |definition|
|
253
|
+
assert_equal 75, definition[:from]
|
254
|
+
assert_equal 25, definition[:size]
|
255
|
+
true
|
256
|
+
end
|
257
|
+
.returns(RESPONSE)
|
258
|
+
|
259
|
+
@response.page(4).to_a
|
260
|
+
assert_equal 75, @response.search.definition[:from]
|
261
|
+
assert_equal 25, @response.search.definition[:size]
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
context "limit/offset readers" do
|
266
|
+
should "return the default" do
|
267
|
+
assert_equal Kaminari.config.default_per_page, @response.limit_value
|
268
|
+
assert_equal 0, @response.offset_value
|
269
|
+
end
|
270
|
+
|
271
|
+
should "return the value from URL parameters" do
|
272
|
+
search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*', size: 10, from: 50
|
273
|
+
@response = Elasticsearch::Model::Response::Response.new ModelClass, search, RESPONSE
|
274
|
+
|
275
|
+
assert_equal 10, @response.limit_value
|
276
|
+
assert_equal 50, @response.offset_value
|
277
|
+
end
|
278
|
+
|
279
|
+
should "ignore the value from request body" do
|
280
|
+
search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass,
|
281
|
+
{ query: { match_all: {} }, from: 333, size: 999 }
|
282
|
+
@response = Elasticsearch::Model::Response::Response.new ModelClass, search, RESPONSE
|
283
|
+
|
284
|
+
assert_equal Kaminari.config.default_per_page, @response.limit_value
|
285
|
+
assert_equal 0, @response.offset_value
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
89
289
|
context "limit setter" do
|
90
290
|
setup do
|
91
291
|
@response.records
|
@@ -3,24 +3,25 @@ require 'will_paginate'
|
|
3
3
|
require 'will_paginate/collection'
|
4
4
|
|
5
5
|
class Elasticsearch::Model::ResponsePaginationWillPaginateTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def self.document_type; 'bar'; end
|
6
|
+
class ModelClass
|
7
|
+
def self.index_name; 'foo'; end
|
8
|
+
def self.document_type; 'bar'; end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
10
|
+
# WillPaginate adds this method to models (see WillPaginate::PerPage module)
|
11
|
+
def self.per_page
|
12
|
+
33
|
15
13
|
end
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
# Subsclass Response so we can include WillPaginate module without conflicts with Kaminari.
|
17
|
+
class WillPaginateResponse < Elasticsearch::Model::Response::Response
|
18
|
+
include Elasticsearch::Model::Response::Pagination::WillPaginate
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
21
|
+
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'},
|
22
|
+
'hits' => { 'total' => 100, 'hits' => (1..100).to_a.map { |i| { _id: i } } } }
|
23
|
+
|
24
|
+
context "Response pagination" do
|
24
25
|
|
25
26
|
setup do
|
26
27
|
@search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*'
|
@@ -153,6 +154,18 @@ class Elasticsearch::Model::ResponsePaginationWillPaginateTest < Test::Unit::Tes
|
|
153
154
|
assert_equal 0, @response.search.definition[:from]
|
154
155
|
assert_equal 33, @response.search.definition[:size]
|
155
156
|
end
|
157
|
+
|
158
|
+
should "use the param_name" do
|
159
|
+
@response.klass.client
|
160
|
+
.expects(:search)
|
161
|
+
.with do |definition|
|
162
|
+
assert_equal 10, definition[:from]
|
163
|
+
true
|
164
|
+
end
|
165
|
+
.returns(RESPONSE)
|
166
|
+
|
167
|
+
@response.paginate(my_page: 2, per_page: 10, param_name: :my_page).to_a
|
168
|
+
end
|
156
169
|
end
|
157
170
|
|
158
171
|
context "#page and #per_page shorthand methods" do
|
@@ -205,4 +218,181 @@ class Elasticsearch::Model::ResponsePaginationWillPaginateTest < Test::Unit::Tes
|
|
205
218
|
end
|
206
219
|
end
|
207
220
|
end
|
221
|
+
|
222
|
+
context "Multimodel response pagination" do
|
223
|
+
setup do
|
224
|
+
@multimodel = Elasticsearch::Model::Multimodel.new ModelClass
|
225
|
+
@search = Elasticsearch::Model::Searching::SearchRequest.new @multimodel, '*'
|
226
|
+
@response = WillPaginateResponse.new @multimodel, @search, RESPONSE
|
227
|
+
@response.klass.stubs(:client).returns mock('client')
|
228
|
+
|
229
|
+
@expected_methods = [
|
230
|
+
# methods needed by WillPaginate::CollectionMethods
|
231
|
+
:current_page,
|
232
|
+
:offset,
|
233
|
+
:per_page,
|
234
|
+
:total_entries,
|
235
|
+
:length,
|
236
|
+
|
237
|
+
# methods defined by WillPaginate::CollectionMethods
|
238
|
+
:total_pages,
|
239
|
+
:previous_page,
|
240
|
+
:next_page,
|
241
|
+
:out_of_bounds?,
|
242
|
+
]
|
243
|
+
end
|
244
|
+
|
245
|
+
should "have pagination methods" do
|
246
|
+
assert_respond_to @response, :paginate
|
247
|
+
|
248
|
+
@expected_methods.each do |method|
|
249
|
+
assert_respond_to @response, method
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
context "response.results" do
|
254
|
+
should "have pagination methods" do
|
255
|
+
@expected_methods.each do |method|
|
256
|
+
assert_respond_to @response.results, method
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context "#offset method" do
|
262
|
+
should "calculate offset using current_page and per_page" do
|
263
|
+
@response.per_page(3).page(3)
|
264
|
+
assert_equal 6, @response.offset
|
265
|
+
end
|
266
|
+
end
|
267
|
+
context "#length method" do
|
268
|
+
should "return count of paginated results" do
|
269
|
+
@response.per_page(3).page(3)
|
270
|
+
assert_equal 3, @response.length
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context "#paginate method" do
|
275
|
+
should "set from/size using WillPaginate defaults, ignoring aggregated models configuration" do
|
276
|
+
@response.klass.client
|
277
|
+
.expects(:search)
|
278
|
+
.with do |definition|
|
279
|
+
assert_equal 0, definition[:from]
|
280
|
+
assert_equal ::WillPaginate.per_page, definition[:size]
|
281
|
+
true
|
282
|
+
end
|
283
|
+
.returns(RESPONSE)
|
284
|
+
|
285
|
+
assert_nil @response.search.definition[:from]
|
286
|
+
assert_nil @response.search.definition[:size]
|
287
|
+
|
288
|
+
@response.paginate(page: nil).to_a
|
289
|
+
assert_equal 0, @response.search.definition[:from]
|
290
|
+
assert_equal ::WillPaginate.per_page, @response.search.definition[:size]
|
291
|
+
end
|
292
|
+
|
293
|
+
should "set from/size using default per_page, ignoring aggregated models' configuration" do
|
294
|
+
@response.klass.client
|
295
|
+
.expects(:search)
|
296
|
+
.with do |definition|
|
297
|
+
assert_equal ::WillPaginate.per_page, definition[:from]
|
298
|
+
assert_equal ::WillPaginate.per_page, definition[:size]
|
299
|
+
true
|
300
|
+
end
|
301
|
+
.returns(RESPONSE)
|
302
|
+
|
303
|
+
assert_nil @response.search.definition[:from]
|
304
|
+
assert_nil @response.search.definition[:size]
|
305
|
+
|
306
|
+
@response.paginate(page: 2).to_a
|
307
|
+
assert_equal ::WillPaginate.per_page, @response.search.definition[:from]
|
308
|
+
assert_equal ::WillPaginate.per_page, @response.search.definition[:size]
|
309
|
+
end
|
310
|
+
|
311
|
+
should "set from/size using custom page and per_page" do
|
312
|
+
@response.klass.client
|
313
|
+
.expects(:search)
|
314
|
+
.with do |definition|
|
315
|
+
assert_equal 18, definition[:from]
|
316
|
+
assert_equal 9, definition[:size]
|
317
|
+
true
|
318
|
+
end
|
319
|
+
.returns(RESPONSE)
|
320
|
+
|
321
|
+
assert_nil @response.search.definition[:from]
|
322
|
+
assert_nil @response.search.definition[:size]
|
323
|
+
|
324
|
+
@response.paginate(page: 3, per_page: 9).to_a
|
325
|
+
assert_equal 18, @response.search.definition[:from]
|
326
|
+
assert_equal 9, @response.search.definition[:size]
|
327
|
+
end
|
328
|
+
|
329
|
+
should "search for first page if specified page is < 1" do
|
330
|
+
@response.klass.client
|
331
|
+
.expects(:search)
|
332
|
+
.with do |definition|
|
333
|
+
assert_equal 0, definition[:from]
|
334
|
+
assert_equal ::WillPaginate.per_page, definition[:size]
|
335
|
+
true
|
336
|
+
end
|
337
|
+
.returns(RESPONSE)
|
338
|
+
|
339
|
+
assert_nil @response.search.definition[:from]
|
340
|
+
assert_nil @response.search.definition[:size]
|
341
|
+
|
342
|
+
@response.paginate(page: "-1").to_a
|
343
|
+
assert_equal 0, @response.search.definition[:from]
|
344
|
+
assert_equal ::WillPaginate.per_page, @response.search.definition[:size]
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
context "#page and #per_page shorthand methods" do
|
349
|
+
should "set from/size using default per_page" do
|
350
|
+
@response.page(5)
|
351
|
+
assert_equal 120, @response.search.definition[:from]
|
352
|
+
assert_equal ::WillPaginate.per_page, @response.search.definition[:size]
|
353
|
+
end
|
354
|
+
|
355
|
+
should "set from/size when calling #page then #per_page" do
|
356
|
+
@response.page(5).per_page(3)
|
357
|
+
assert_equal 12, @response.search.definition[:from]
|
358
|
+
assert_equal 3, @response.search.definition[:size]
|
359
|
+
end
|
360
|
+
|
361
|
+
should "set from/size when calling #per_page then #page" do
|
362
|
+
@response.per_page(3).page(5)
|
363
|
+
assert_equal 12, @response.search.definition[:from]
|
364
|
+
assert_equal 3, @response.search.definition[:size]
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
context "#current_page method" do
|
369
|
+
should "return 1 by default" do
|
370
|
+
@response.paginate({})
|
371
|
+
assert_equal 1, @response.current_page
|
372
|
+
end
|
373
|
+
|
374
|
+
should "return current page number" do
|
375
|
+
@response.paginate(page: 3, per_page: 9)
|
376
|
+
assert_equal 3, @response.current_page
|
377
|
+
end
|
378
|
+
|
379
|
+
should "return nil if not pagination set" do
|
380
|
+
assert_equal nil, @response.current_page
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
context "#per_page method" do
|
385
|
+
should "return value set in paginate call" do
|
386
|
+
@response.paginate(per_page: 8)
|
387
|
+
assert_equal 8, @response.per_page
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
context "#total_entries method" do
|
392
|
+
should "return total from response" do
|
393
|
+
@response.expects(:results).returns(mock('results', total: 100))
|
394
|
+
assert_equal 100, @response.total_entries
|
395
|
+
end
|
396
|
+
end
|
397
|
+
end
|
208
398
|
end
|