elasticsearch-model 0.1.9 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -2
  3. data/Gemfile +22 -0
  4. data/LICENSE.txt +199 -10
  5. data/README.md +96 -43
  6. data/Rakefile +49 -35
  7. data/elasticsearch-model.gemspec +53 -41
  8. data/examples/activerecord_article.rb +18 -1
  9. data/examples/activerecord_associations.rb +86 -17
  10. data/examples/activerecord_custom_analyzer.rb +152 -0
  11. data/examples/activerecord_mapping_completion.rb +33 -16
  12. data/examples/activerecord_mapping_edge_ngram.rb +118 -0
  13. data/examples/couchbase_article.rb +17 -0
  14. data/examples/datamapper_article.rb +28 -1
  15. data/examples/mongoid_article.rb +17 -0
  16. data/examples/ohm_article.rb +17 -0
  17. data/examples/riak_article.rb +17 -0
  18. data/gemfiles/3.0.gemfile +23 -1
  19. data/gemfiles/4.0.gemfile +25 -1
  20. data/gemfiles/5.0.gemfile +35 -0
  21. data/gemfiles/6.0.gemfile +36 -0
  22. data/lib/elasticsearch/model/adapter.rb +17 -0
  23. data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
  24. data/lib/elasticsearch/model/adapters/default.rb +17 -0
  25. data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
  26. data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
  27. data/lib/elasticsearch/model/callbacks.rb +17 -0
  28. data/lib/elasticsearch/model/client.rb +17 -0
  29. data/lib/elasticsearch/model/ext/active_record.rb +17 -0
  30. data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
  31. data/lib/elasticsearch/model/importing.rb +61 -17
  32. data/lib/elasticsearch/model/indexing.rb +87 -49
  33. data/lib/elasticsearch/model/multimodel.rb +17 -0
  34. data/lib/elasticsearch/model/naming.rb +33 -2
  35. data/lib/elasticsearch/model/proxy.rb +61 -18
  36. data/lib/elasticsearch/model/response/aggregations.rb +55 -0
  37. data/lib/elasticsearch/model/response/base.rb +25 -3
  38. data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
  39. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
  40. data/lib/elasticsearch/model/response/pagination.rb +19 -192
  41. data/lib/elasticsearch/model/response/records.rb +17 -1
  42. data/lib/elasticsearch/model/response/result.rb +19 -2
  43. data/lib/elasticsearch/model/response/results.rb +17 -0
  44. data/lib/elasticsearch/model/response/suggestions.rb +20 -1
  45. data/lib/elasticsearch/model/response.rb +28 -10
  46. data/lib/elasticsearch/model/searching.rb +17 -0
  47. data/lib/elasticsearch/model/serializing.rb +17 -0
  48. data/lib/elasticsearch/model/version.rb +18 -1
  49. data/lib/elasticsearch/model.rb +36 -39
  50. data/spec/elasticsearch/model/adapter_spec.rb +136 -0
  51. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
  52. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
  53. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
  54. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
  55. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
  56. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
  57. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
  58. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
  59. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
  60. data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
  61. data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
  62. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
  63. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
  64. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
  65. data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
  66. data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
  67. data/spec/elasticsearch/model/client_spec.rb +83 -0
  68. data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
  69. data/spec/elasticsearch/model/importing_spec.rb +243 -0
  70. data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
  71. data/spec/elasticsearch/model/module_spec.rb +94 -0
  72. data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
  73. data/spec/elasticsearch/model/naming_spec.rb +203 -0
  74. data/spec/elasticsearch/model/proxy_spec.rb +124 -0
  75. data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
  76. data/spec/elasticsearch/model/response/base_spec.rb +107 -0
  77. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
  78. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
  79. data/spec/elasticsearch/model/response/records_spec.rb +135 -0
  80. data/spec/elasticsearch/model/response/response_spec.rb +148 -0
  81. data/spec/elasticsearch/model/response/result_spec.rb +139 -0
  82. data/spec/elasticsearch/model/response/results_spec.rb +73 -0
  83. data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
  84. data/spec/elasticsearch/model/searching_spec.rb +66 -0
  85. data/spec/elasticsearch/model/serializing_spec.rb +39 -0
  86. data/spec/spec_helper.rb +193 -0
  87. data/spec/support/app/answer.rb +50 -0
  88. data/spec/support/app/article.rb +39 -0
  89. data/spec/support/app/article_for_pagination.rb +29 -0
  90. data/spec/support/app/article_no_type.rb +37 -0
  91. data/spec/support/app/article_with_custom_serialization.rb +30 -0
  92. data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
  93. data/spec/support/app/author.rb +26 -0
  94. data/spec/support/app/authorship.rb +21 -0
  95. data/spec/support/app/category.rb +20 -0
  96. data/spec/support/app/comment.rb +20 -0
  97. data/spec/support/app/episode.rb +28 -0
  98. data/spec/support/app/image.rb +36 -0
  99. data/spec/support/app/import_article.rb +29 -0
  100. data/spec/support/app/mongoid_article.rb +38 -0
  101. data/spec/support/app/namespaced_book.rb +27 -0
  102. data/spec/support/app/parent_and_child_searchable.rb +41 -0
  103. data/spec/support/app/post.rb +31 -0
  104. data/spec/support/app/question.rb +44 -0
  105. data/spec/support/app/searchable.rb +65 -0
  106. data/spec/support/app/series.rb +28 -0
  107. data/spec/support/app.rb +46 -0
  108. data/spec/support/model.json +1 -0
  109. data/{test → spec}/support/model.yml +0 -0
  110. metadata +175 -121
  111. data/test/integration/active_record_associations_parent_child.rb +0 -139
  112. data/test/integration/active_record_associations_test.rb +0 -326
  113. data/test/integration/active_record_basic_test.rb +0 -234
  114. data/test/integration/active_record_custom_serialization_test.rb +0 -62
  115. data/test/integration/active_record_import_test.rb +0 -109
  116. data/test/integration/active_record_namespaced_model_test.rb +0 -49
  117. data/test/integration/active_record_pagination_test.rb +0 -145
  118. data/test/integration/dynamic_index_name_test.rb +0 -47
  119. data/test/integration/mongoid_basic_test.rb +0 -177
  120. data/test/integration/multiple_models_test.rb +0 -172
  121. data/test/support/model.json +0 -1
  122. data/test/test_helper.rb +0 -93
  123. data/test/unit/adapter_active_record_test.rb +0 -157
  124. data/test/unit/adapter_default_test.rb +0 -41
  125. data/test/unit/adapter_mongoid_test.rb +0 -104
  126. data/test/unit/adapter_multiple_test.rb +0 -106
  127. data/test/unit/adapter_test.rb +0 -69
  128. data/test/unit/callbacks_test.rb +0 -31
  129. data/test/unit/client_test.rb +0 -27
  130. data/test/unit/importing_test.rb +0 -203
  131. data/test/unit/indexing_test.rb +0 -650
  132. data/test/unit/module_test.rb +0 -57
  133. data/test/unit/multimodel_test.rb +0 -38
  134. data/test/unit/naming_test.rb +0 -103
  135. data/test/unit/proxy_test.rb +0 -100
  136. data/test/unit/response_base_test.rb +0 -40
  137. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  138. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  139. data/test/unit/response_records_test.rb +0 -91
  140. data/test/unit/response_result_test.rb +0 -90
  141. data/test/unit/response_results_test.rb +0 -31
  142. data/test/unit/response_test.rb +0 -104
  143. data/test/unit/searching_search_request_test.rb +0 -78
  144. data/test/unit/searching_test.rb +0 -41
  145. data/test/unit/serializing_test.rb +0 -17
@@ -0,0 +1,472 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require 'spec_helper'
19
+
20
+ describe 'Elasticsearch::Model::Response::Response Kaminari' do
21
+
22
+ before(:all) do
23
+ class ModelClass
24
+ include ::Kaminari::ConfigurationMethods
25
+ def self.index_name; 'foo'; end
26
+ def self.document_type; 'bar'; end
27
+ end
28
+ end
29
+
30
+ after(:all) do
31
+ remove_classes(ModelClass)
32
+ end
33
+
34
+ let(:search) do
35
+ Elasticsearch::Model::Searching::SearchRequest.new(model, '*')
36
+ end
37
+
38
+ let(:response) do
39
+ allow(model).to receive(:client).and_return(client)
40
+ Elasticsearch::Model::Response::Response.new(model, search, response_document).tap do |resp|
41
+ allow(resp).to receive(:client).and_return(client)
42
+ end
43
+ end
44
+
45
+ let(:client) do
46
+ double('client')
47
+ end
48
+
49
+ shared_examples_for 'a search request that can be paginated' do
50
+
51
+ describe '#page' do
52
+
53
+ it 'does not set an initial from and size on the search definition' do
54
+ expect(response.search.definition[:from]).to be(nil)
55
+ expect(response.search.definition[:size]).to be(nil)
56
+ end
57
+
58
+ context 'when page is called once' do
59
+
60
+ let(:search_request) do
61
+ { index: index_field, from: 25, size: 25, q: '*', type: type_field}
62
+ end
63
+
64
+ before do
65
+ expect(client).to receive(:search).with(search_request).and_return(response_document)
66
+ response.page(2).to_a
67
+ end
68
+
69
+ it 'advances the from/size in the search request' do
70
+ expect(response.search.definition[:from]).to be(25)
71
+ expect(response.search.definition[:size]).to be(25)
72
+ end
73
+ end
74
+
75
+ context 'when page is called more than once' do
76
+
77
+ let(:search_request_one) do
78
+ { index: index_field, from: 25, size: 25, q: '*', type: type_field}
79
+ end
80
+
81
+ let(:search_request_two) do
82
+ { index: index_field, from: 75, size: 25, q: '*', type: type_field}
83
+ end
84
+
85
+ before do
86
+ expect(client).to receive(:search).with(search_request_one).and_return(response_document)
87
+ response.page(2).to_a
88
+ expect(client).to receive(:search).with(search_request_two).and_return(response_document)
89
+ response.page(4).to_a
90
+ end
91
+
92
+ it 'advances the from/size in the search request' do
93
+ expect(response.search.definition[:from]).to be(75)
94
+ expect(response.search.definition[:size]).to be(25)
95
+ end
96
+ end
97
+
98
+ context 'when limit is also set' do
99
+
100
+ before do
101
+ response.records
102
+ response.results
103
+ end
104
+
105
+ context 'when page is called before limit' do
106
+
107
+ before do
108
+ response.page(3).limit(35)
109
+ end
110
+
111
+ it 'sets the correct values' do
112
+ expect(response.search.definition[:size]).to eq(35)
113
+ expect(response.search.definition[:from]).to eq(70)
114
+ end
115
+
116
+ it 'resets the instance variables' do
117
+ expect(response.instance_variable_get(:@response)).to be(nil)
118
+ expect(response.instance_variable_get(:@records)).to be(nil)
119
+ expect(response.instance_variable_get(:@results)).to be(nil)
120
+ end
121
+ end
122
+
123
+ context 'when limit is called before page' do
124
+
125
+ before do
126
+ response.limit(35).page(3)
127
+ end
128
+
129
+ it 'sets the correct values' do
130
+ expect(response.search.definition[:size]).to eq(35)
131
+ expect(response.search.definition[:from]).to eq(70)
132
+ end
133
+
134
+ it 'resets the instance variables' do
135
+ expect(response.instance_variable_get(:@response)).to be(nil)
136
+ expect(response.instance_variable_get(:@records)).to be(nil)
137
+ expect(response.instance_variable_get(:@results)).to be(nil)
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ describe '#limit_value' do
144
+
145
+ context 'when there is no default set' do
146
+
147
+ it 'uses the limit value from the Kaminari configuration' do
148
+ expect(response.limit_value).to eq(Kaminari.config.default_per_page)
149
+ end
150
+ end
151
+
152
+ context 'when there is a limit in the search definition' do
153
+
154
+ let(:search) do
155
+ Elasticsearch::Model::Searching::SearchRequest.new(model, '*', size: 10)
156
+ end
157
+
158
+ it 'gets the limit from the search definition' do
159
+ expect(response.limit_value).to eq(10)
160
+ end
161
+ end
162
+
163
+ context 'when there is a limit in the search body' do
164
+
165
+ let(:search) do
166
+ Elasticsearch::Model::Searching::SearchRequest.new(model, { query: { match_all: {} }, size: 999 })
167
+ end
168
+
169
+ it 'does not use the limit' do
170
+ expect(response.limit_value).to be(Kaminari.config.default_per_page)
171
+ end
172
+ end
173
+ end
174
+
175
+ describe '#offset_value' do
176
+
177
+ context 'when there is no default set' do
178
+
179
+ it 'uses an offset of 0' do
180
+ expect(response.offset_value).to eq(0)
181
+ end
182
+ end
183
+
184
+ context 'when there is an offset in the search definition' do
185
+
186
+ let(:search) do
187
+ Elasticsearch::Model::Searching::SearchRequest.new(model, '*', from: 50)
188
+ end
189
+
190
+ it 'gets the limit from the search definition' do
191
+ expect(response.offset_value).to eq(50)
192
+ end
193
+ end
194
+
195
+ context 'when there is an offset in the search body' do
196
+
197
+ let(:search) do
198
+ Elasticsearch::Model::Searching::SearchRequest.new(model, { query: { match_all: {} }, from: 333 })
199
+ end
200
+
201
+ it 'does not use the offset' do
202
+ expect(response.offset_value).to be(0)
203
+ end
204
+ end
205
+ end
206
+
207
+ describe '#limit' do
208
+
209
+ context 'when a limit is set' do
210
+
211
+ before do
212
+ response.records
213
+ response.results
214
+ response.limit(35)
215
+ end
216
+
217
+ it 'sets the limit on the search defintiion' do
218
+ expect(response.search.definition[:size]).to eq(35)
219
+ end
220
+
221
+ it 'resets the instance variables' do
222
+ expect(response.instance_variable_get(:@response)).to be(nil)
223
+ expect(response.instance_variable_get(:@records)).to be(nil)
224
+ expect(response.instance_variable_get(:@results)).to be(nil)
225
+ end
226
+
227
+ context 'when the limit is provided as a string' do
228
+
229
+ before do
230
+ response.limit('35')
231
+ end
232
+
233
+ it 'coerces the string to an integer' do
234
+ expect(response.search.definition[:size]).to eq(35)
235
+ end
236
+ end
237
+
238
+ context 'when the limit is an invalid type' do
239
+
240
+ before do
241
+ response.limit('asdf')
242
+ end
243
+
244
+ it 'does not apply the setting' do
245
+ expect(response.search.definition[:size]).to eq(35)
246
+ end
247
+ end
248
+ end
249
+ end
250
+
251
+ describe '#offset' do
252
+
253
+ context 'when an offset is set' do
254
+
255
+ before do
256
+ response.records
257
+ response.results
258
+ response.offset(15)
259
+ end
260
+
261
+ it 'sets the limit on the search defintiion' do
262
+ expect(response.search.definition[:from]).to eq(15)
263
+ end
264
+
265
+ it 'resets the instance variables' do
266
+ expect(response.instance_variable_get(:@response)).to be(nil)
267
+ expect(response.instance_variable_get(:@records)).to be(nil)
268
+ expect(response.instance_variable_get(:@results)).to be(nil)
269
+ end
270
+
271
+ context 'when the offset is provided as a string' do
272
+
273
+ before do
274
+ response.offset('15')
275
+ end
276
+
277
+ it 'coerces the string to an integer' do
278
+ expect(response.search.definition[:from]).to eq(15)
279
+ end
280
+ end
281
+
282
+ context 'when the offset is an invalid type' do
283
+
284
+ before do
285
+ response.offset('asdf')
286
+ end
287
+
288
+ it 'does not apply the setting' do
289
+ expect(response.search.definition[:from]).to eq(0)
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ describe '#total' do
296
+
297
+ before do
298
+ allow(response.results).to receive(:total).and_return(100)
299
+ end
300
+
301
+ it 'returns the total number of hits' do
302
+ expect(response.total_count).to eq(100)
303
+ end
304
+ end
305
+
306
+ context 'results' do
307
+
308
+ before do
309
+ allow(search).to receive(:execute!).and_return(response_document)
310
+ end
311
+
312
+ describe '#current_page' do
313
+
314
+ it 'returns the current page' do
315
+ expect(response.results.current_page).to eq(1)
316
+ end
317
+
318
+ context 'when a particular page is accessed' do
319
+
320
+ it 'returns the correct current page' do
321
+ expect(response.page(5).results.current_page).to eq(5)
322
+ end
323
+ end
324
+ end
325
+
326
+ describe '#prev_page' do
327
+
328
+ it 'returns the previous page' do
329
+ expect(response.page(1).results.prev_page).to be(nil)
330
+ expect(response.page(2).results.prev_page).to be(1)
331
+ expect(response.page(3).results.prev_page).to be(2)
332
+ expect(response.page(4).results.prev_page).to be(3)
333
+ end
334
+ end
335
+
336
+ describe '#next_page' do
337
+
338
+ it 'returns the previous page' do
339
+ expect(response.page(1).results.next_page).to be(2)
340
+ expect(response.page(2).results.next_page).to be(3)
341
+ expect(response.page(3).results.next_page).to be(4)
342
+ expect(response.page(4).results.next_page).to be(nil)
343
+ end
344
+ end
345
+ end
346
+
347
+ context 'records' do
348
+
349
+ before do
350
+ allow(search).to receive(:execute!).and_return(response_document)
351
+ end
352
+
353
+ describe '#current_page' do
354
+
355
+ it 'returns the current page' do
356
+ expect(response.records.current_page).to eq(1)
357
+ end
358
+
359
+ context 'when a particular page is accessed' do
360
+
361
+ it 'returns the correct current page' do
362
+ expect(response.page(5).records.current_page).to eq(5)
363
+ end
364
+ end
365
+ end
366
+
367
+ describe '#prev_page' do
368
+
369
+ it 'returns the previous page' do
370
+ expect(response.page(1).records.prev_page).to be(nil)
371
+ expect(response.page(2).records.prev_page).to be(1)
372
+ expect(response.page(3).records.prev_page).to be(2)
373
+ expect(response.page(4).records.prev_page).to be(3)
374
+ end
375
+ end
376
+
377
+ describe '#next_page' do
378
+
379
+ it 'returns the previous page' do
380
+ expect(response.page(1).records.next_page).to be(2)
381
+ expect(response.page(2).records.next_page).to be(3)
382
+ expect(response.page(3).records.next_page).to be(4)
383
+ expect(response.page(4).records.next_page).to be(nil)
384
+ end
385
+ end
386
+ end
387
+ end
388
+
389
+ context 'when Elasticsearch version is < 7.0' do
390
+
391
+ let(:response_document) do
392
+ { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'},
393
+ 'hits' => { 'total' => 100, 'hits' => (1..100).to_a.map { |i| { _id: i } } } }
394
+ end
395
+
396
+ context 'when the model is a single one' do
397
+
398
+ let(:model) do
399
+ ModelClass
400
+ end
401
+
402
+ let(:type_field) do
403
+ 'bar'
404
+ end
405
+
406
+ let(:index_field) do
407
+ 'foo'
408
+ end
409
+
410
+ it_behaves_like 'a search request that can be paginated'
411
+ end
412
+
413
+ context 'when the model is a multimodel' do
414
+
415
+ let(:model) do
416
+ Elasticsearch::Model::Multimodel.new(ModelClass)
417
+ end
418
+
419
+ let(:type_field) do
420
+ ['bar']
421
+ end
422
+
423
+ let(:index_field) do
424
+ ['foo']
425
+ end
426
+
427
+ it_behaves_like 'a search request that can be paginated'
428
+ end
429
+ end
430
+
431
+ context 'when Elasticsearch version is >= 7.0' do
432
+
433
+ let(:response_document) do
434
+ { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'},
435
+ 'hits' => { 'total' => { 'value' => 100, 'relation' => 'eq' }, 'hits' => (1..100).to_a.map { |i| { _id: i } } } }
436
+ end
437
+
438
+ context 'when the model is a single one' do
439
+
440
+ let(:model) do
441
+ ModelClass
442
+ end
443
+
444
+ let(:type_field) do
445
+ 'bar'
446
+ end
447
+
448
+ let(:index_field) do
449
+ 'foo'
450
+ end
451
+
452
+ it_behaves_like 'a search request that can be paginated'
453
+ end
454
+
455
+ context 'when the model is a multimodel' do
456
+
457
+ let(:model) do
458
+ Elasticsearch::Model::Multimodel.new(ModelClass)
459
+ end
460
+
461
+ let(:type_field) do
462
+ ['bar']
463
+ end
464
+
465
+ let(:index_field) do
466
+ ['foo']
467
+ end
468
+
469
+ it_behaves_like 'a search request that can be paginated'
470
+ end
471
+ end
472
+ end