search_flip 2.0.0.beta2 → 2.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +10 -3
- data/README.md +22 -1
- data/UPDATING.md +21 -5
- data/lib/search_flip/bulk.rb +5 -2
- data/lib/search_flip/connection.rb +29 -19
- data/lib/search_flip/criteria.rb +48 -18
- data/lib/search_flip/http_client.rb +27 -26
- data/lib/search_flip/index.rb +6 -4
- data/lib/search_flip/response.rb +9 -1
- data/lib/search_flip/version.rb +1 -1
- data/search_flip.gemspec +7 -2
- data/spec/delegate_matcher.rb +32 -0
- data/spec/search_flip/aggregation_spec.rb +265 -0
- data/spec/search_flip/bulk_spec.rb +78 -0
- data/spec/search_flip/connection_spec.rb +211 -0
- data/spec/search_flip/criteria_spec.rb +1028 -0
- data/spec/search_flip/http_client_spec.rb +67 -0
- data/spec/search_flip/index_spec.rb +455 -0
- data/spec/search_flip/model_spec.rb +44 -0
- data/spec/search_flip/response_spec.rb +185 -0
- data/spec/search_flip/to_json_spec.rb +29 -0
- data/{test/test_helper.rb → spec/spec_helper.rb} +13 -76
- metadata +30 -39
- data/test/search_flip/aggregation_test.rb +0 -230
- data/test/search_flip/bulk_test.rb +0 -55
- data/test/search_flip/connection_test.rb +0 -161
- data/test/search_flip/criteria_test.rb +0 -879
- data/test/search_flip/http_client_test.rb +0 -35
- data/test/search_flip/index_test.rb +0 -451
- data/test/search_flip/model_test.rb +0 -39
- data/test/search_flip/response_test.rb +0 -137
- data/test/search_flip/to_json_test.rb +0 -30
@@ -1,879 +0,0 @@
|
|
1
|
-
|
2
|
-
require File.expand_path("../test_helper", __dir__)
|
3
|
-
|
4
|
-
class SearchFlip::CriteriaTest < SearchFlip::TestCase
|
5
|
-
should_delegate_methods :total_entries, :current_page, :previous_page, :prev_page, :next_page,
|
6
|
-
:first_page?, :last_page?, :out_of_range?, :total_pages, :hits, :ids, :count, :size, :length,
|
7
|
-
:took, :aggregations, :suggestions, :scope, :results, :records, :scroll_id, :raw_response,
|
8
|
-
to: :response, subject: SearchFlip::Criteria.new(target: ProductIndex)
|
9
|
-
|
10
|
-
def test_merge
|
11
|
-
product1 = create(:product, price: 100, category: "category1")
|
12
|
-
product2 = create(:product, price: 200, category: "category2")
|
13
|
-
product3 = create(:product, price: 300, category: "category1")
|
14
|
-
|
15
|
-
ProductIndex.import [product1, product2, product3]
|
16
|
-
|
17
|
-
query = ProductIndex.where(price: 50..250).aggregate(:category).merge(ProductIndex.where(category: "category1"))
|
18
|
-
|
19
|
-
assert_includes query.records, product1
|
20
|
-
refute_includes query.records, product2
|
21
|
-
refute_includes query.records, product3
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_criteria
|
25
|
-
criteria = ProductIndex.criteria
|
26
|
-
|
27
|
-
assert_equal criteria.criteria.object_id, criteria.object_id
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_timeout
|
31
|
-
query = ProductIndex.timeout("1s")
|
32
|
-
|
33
|
-
assert_equal "1s", query.request[:timeout]
|
34
|
-
|
35
|
-
query.execute
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_terminate_after
|
39
|
-
query = ProductIndex.terminate_after(1)
|
40
|
-
|
41
|
-
assert_equal 1, query.request[:terminate_after]
|
42
|
-
|
43
|
-
query.execute
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_where
|
47
|
-
product1 = create(:product, price: 100, category: "category1")
|
48
|
-
product2 = create(:product, price: 200, category: "category2")
|
49
|
-
product3 = create(:product, price: 300, category: "category1")
|
50
|
-
|
51
|
-
ProductIndex.import [product1, product2, product3]
|
52
|
-
|
53
|
-
query1 = ProductIndex.where(price: 100..200)
|
54
|
-
query2 = query1.where(category: "category1")
|
55
|
-
|
56
|
-
assert_includes query1.records, product1
|
57
|
-
assert_includes query1.records, product2
|
58
|
-
refute_includes query1.records, product3
|
59
|
-
|
60
|
-
assert_includes query2.records, product1
|
61
|
-
refute_includes query2.records, product2
|
62
|
-
refute_includes query2.records, product3
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_with_settings
|
66
|
-
stub_request(:post, "http://127.0.0.1:9200/new_user_index/products/_search")
|
67
|
-
.to_return(status: 200, body: "{}", headers: { content_type: "application/json" })
|
68
|
-
|
69
|
-
ProductIndex.where(id: 1).with_settings(index_name: "new_user_index").execute
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_where_with_array
|
73
|
-
expected1 = create(:product, title: "expected1")
|
74
|
-
expected2 = create(:product, title: "expected2")
|
75
|
-
rejected = create(:product, title: "rejected")
|
76
|
-
|
77
|
-
ProductIndex.import [expected1, expected2, rejected]
|
78
|
-
|
79
|
-
records = ProductIndex.where(title: ["expected1", "expected2"]).records
|
80
|
-
|
81
|
-
assert_includes records, expected1
|
82
|
-
assert_includes records, expected2
|
83
|
-
refute_includes records, rejected
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_where_with_range
|
87
|
-
expected1 = create(:product, price: 100)
|
88
|
-
expected2 = create(:product, price: 200)
|
89
|
-
rejected = create(:product, price: 300)
|
90
|
-
|
91
|
-
ProductIndex.import [expected1, expected2, rejected]
|
92
|
-
|
93
|
-
records = ProductIndex.where(price: 100..200).records
|
94
|
-
|
95
|
-
assert_includes records, expected1
|
96
|
-
assert_includes records, expected2
|
97
|
-
refute_includes records, rejected
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_where_with_nil
|
101
|
-
expected = create(:product, price: nil)
|
102
|
-
rejected = create(:product, price: 100)
|
103
|
-
|
104
|
-
ProductIndex.import [expected, rejected]
|
105
|
-
|
106
|
-
records = ProductIndex.where(price: nil).records
|
107
|
-
|
108
|
-
assert_includes records, expected
|
109
|
-
refute_includes records, rejected
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_where_not
|
113
|
-
product1 = create(:product, price: 100, category: "category1")
|
114
|
-
product2 = create(:product, price: 200, category: "category2")
|
115
|
-
product3 = create(:product, price: 300, category: "category1")
|
116
|
-
|
117
|
-
ProductIndex.import [product1, product2, product3]
|
118
|
-
|
119
|
-
query1 = ProductIndex.where_not(price: 250..350)
|
120
|
-
query2 = query1.where_not(category: "category2")
|
121
|
-
|
122
|
-
assert_includes query1.records, product1
|
123
|
-
assert_includes query1.records, product2
|
124
|
-
refute_includes query1.records, product3
|
125
|
-
|
126
|
-
assert_includes query2.records, product1
|
127
|
-
refute_includes query2.records, product2
|
128
|
-
refute_includes query2.records, product3
|
129
|
-
end
|
130
|
-
|
131
|
-
def test_where_not_with_array
|
132
|
-
expected = create(:product, title: "expected")
|
133
|
-
rejected1 = create(:product, title: "rejected1")
|
134
|
-
rejected2 = create(:product, title: "rejected2")
|
135
|
-
|
136
|
-
ProductIndex.import [expected, rejected1, rejected2]
|
137
|
-
|
138
|
-
records = ProductIndex.where_not(title: ["rejected1", "rejected2"]).records
|
139
|
-
|
140
|
-
assert_includes records, expected
|
141
|
-
refute_includes records, rejected1
|
142
|
-
refute_includes records, rejected2
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_where_not_with_range
|
146
|
-
expected = create(:product, price: 100)
|
147
|
-
rejected1 = create(:product, price: 200)
|
148
|
-
rejected2 = create(:product, price: 300)
|
149
|
-
|
150
|
-
ProductIndex.import [expected, rejected1, rejected2]
|
151
|
-
|
152
|
-
records = ProductIndex.where_not(price: 200..300).records
|
153
|
-
|
154
|
-
assert_includes records, expected
|
155
|
-
refute_includes records, rejected1
|
156
|
-
refute_includes records, rejected2
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_where_not_with_nil
|
160
|
-
expected = create(:product, price: 100)
|
161
|
-
rejected = create(:product, price: nil)
|
162
|
-
|
163
|
-
ProductIndex.import [expected, rejected]
|
164
|
-
|
165
|
-
records = ProductIndex.where_not(price: nil).records
|
166
|
-
|
167
|
-
assert_includes records, expected
|
168
|
-
refute_includes records, rejected
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_filter
|
172
|
-
product1 = create(:product, price: 100, category: "category1")
|
173
|
-
product2 = create(:product, price: 200, category: "category2")
|
174
|
-
product3 = create(:product, price: 300, category: "category1")
|
175
|
-
|
176
|
-
ProductIndex.import [product1, product2, product3]
|
177
|
-
|
178
|
-
query1 = ProductIndex.filter(range: { price: { gte: 100, lte: 200 } })
|
179
|
-
query2 = query1.filter(term: { category: "category1" })
|
180
|
-
|
181
|
-
assert_includes query1.records, product1
|
182
|
-
assert_includes query1.records, product2
|
183
|
-
refute_includes query1.records, product3
|
184
|
-
|
185
|
-
assert_includes query2.records, product1
|
186
|
-
refute_includes query2.records, product2
|
187
|
-
refute_includes query2.records, product3
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_range
|
191
|
-
product1 = create(:product, price: 100)
|
192
|
-
product2 = create(:product, price: 200)
|
193
|
-
product3 = create(:product, price: 300)
|
194
|
-
|
195
|
-
ProductIndex.import [product1, product2, product3]
|
196
|
-
|
197
|
-
query1 = ProductIndex.range(:price, gte: 100, lte: 200)
|
198
|
-
query2 = query1.range(:price, gte: 200, lte: 300)
|
199
|
-
|
200
|
-
assert_includes query1.records, product1
|
201
|
-
assert_includes query1.records, product2
|
202
|
-
refute_includes query1.records, product3
|
203
|
-
|
204
|
-
refute_includes query2.records, product1
|
205
|
-
assert_includes query2.records, product2
|
206
|
-
refute_includes query2.records, product3
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_match_all
|
210
|
-
expected1 = create(:product)
|
211
|
-
expected2 = create(:product)
|
212
|
-
|
213
|
-
ProductIndex.import [expected1, expected2]
|
214
|
-
|
215
|
-
records = ProductIndex.match_all.records
|
216
|
-
|
217
|
-
assert_includes records, expected1
|
218
|
-
assert_includes records, expected2
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_exists
|
222
|
-
product1 = create(:product, title: "title1", description: "description1")
|
223
|
-
product2 = create(:product, title: "title2", description: nil)
|
224
|
-
product3 = create(:product, title: nil, description: "description2")
|
225
|
-
|
226
|
-
ProductIndex.import [product1, product2, product3]
|
227
|
-
|
228
|
-
query1 = ProductIndex.exists(:title)
|
229
|
-
query2 = query1.exists(:description)
|
230
|
-
|
231
|
-
assert_includes query1.records, product1
|
232
|
-
assert_includes query1.records, product2
|
233
|
-
refute_includes query1.records, product3
|
234
|
-
|
235
|
-
assert_includes query2.records, product1
|
236
|
-
refute_includes query2.records, product2
|
237
|
-
refute_includes query2.records, product3
|
238
|
-
end
|
239
|
-
|
240
|
-
def test_exists_not
|
241
|
-
product1 = create(:product, title: nil, description: nil)
|
242
|
-
product2 = create(:product, title: nil, description: "description2")
|
243
|
-
product3 = create(:product, title: "title3", description: "description3")
|
244
|
-
|
245
|
-
ProductIndex.import [product1, product2, product3]
|
246
|
-
|
247
|
-
query1 = ProductIndex.exists_not(:title)
|
248
|
-
query2 = query1.exists_not(:description)
|
249
|
-
|
250
|
-
assert_includes query1.records, product1
|
251
|
-
assert_includes query1.records, product2
|
252
|
-
refute_includes query1.records, product3
|
253
|
-
|
254
|
-
assert_includes query2.records, product1
|
255
|
-
refute_includes query2.records, product2
|
256
|
-
refute_includes query2.records, product3
|
257
|
-
end
|
258
|
-
|
259
|
-
def test_post_search
|
260
|
-
return if ProductIndex.connection.version.to_i < 2
|
261
|
-
|
262
|
-
product1 = create(:product, title: "title1", category: "category1")
|
263
|
-
product2 = create(:product, title: "title2", category: "category2")
|
264
|
-
product3 = create(:product, title: "title3", category: "category1")
|
265
|
-
|
266
|
-
ProductIndex.import [product1, product2, product3]
|
267
|
-
|
268
|
-
query1 = ProductIndex.aggregate(:category).post_search("title1 OR title2")
|
269
|
-
query2 = query1.post_search("category1")
|
270
|
-
|
271
|
-
assert_includes query1.records, product1
|
272
|
-
assert_includes query1.records, product2
|
273
|
-
refute_includes query1.records, product3
|
274
|
-
|
275
|
-
assert_includes query2.records, product1
|
276
|
-
refute_includes query2.records, product2
|
277
|
-
refute_includes query2.records, product3
|
278
|
-
|
279
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
280
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
281
|
-
|
282
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
283
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
284
|
-
end
|
285
|
-
|
286
|
-
def test_post_where
|
287
|
-
product1 = create(:product, price: 100, category: "category1")
|
288
|
-
product2 = create(:product, price: 200, category: "category2")
|
289
|
-
product3 = create(:product, price: 300, category: "category1")
|
290
|
-
|
291
|
-
ProductIndex.import [product1, product2, product3]
|
292
|
-
|
293
|
-
query1 = ProductIndex.aggregate(:category).post_where(price: 100..200)
|
294
|
-
query2 = query1.post_where(category: "category1")
|
295
|
-
|
296
|
-
assert_includes query1.records, product1
|
297
|
-
assert_includes query1.records, product2
|
298
|
-
refute_includes query1.records, product3
|
299
|
-
|
300
|
-
assert_includes query2.records, product1
|
301
|
-
refute_includes query2.records, product2
|
302
|
-
refute_includes query2.records, product3
|
303
|
-
|
304
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
305
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
306
|
-
|
307
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
308
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
309
|
-
end
|
310
|
-
|
311
|
-
def test_post_where_with_array
|
312
|
-
expected1 = create(:product, title: "expected1", category: "category1")
|
313
|
-
expected2 = create(:product, title: "expected2", category: "category2")
|
314
|
-
rejected = create(:product, title: "rejected", category: "category1")
|
315
|
-
|
316
|
-
ProductIndex.import [expected1, expected2, rejected]
|
317
|
-
|
318
|
-
query = ProductIndex.aggregate(:category).post_where(title: ["expected1", "expected2"])
|
319
|
-
|
320
|
-
assert_includes query.records, expected1
|
321
|
-
assert_includes query.records, expected2
|
322
|
-
refute_includes query.records, rejected
|
323
|
-
|
324
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
325
|
-
query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
326
|
-
end
|
327
|
-
|
328
|
-
def test_post_where_with_range
|
329
|
-
expected1 = create(:product, price: 100, category: "category1")
|
330
|
-
expected2 = create(:product, price: 200, category: "category2")
|
331
|
-
rejected = create(:product, price: 300, category: "category1")
|
332
|
-
|
333
|
-
ProductIndex.import [expected1, expected2, rejected]
|
334
|
-
|
335
|
-
query = ProductIndex.aggregate(:category).post_where(price: 100..200)
|
336
|
-
|
337
|
-
assert_includes query.records, expected1
|
338
|
-
assert_includes query.records, expected2
|
339
|
-
refute_includes query.records, rejected
|
340
|
-
|
341
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
342
|
-
query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
343
|
-
end
|
344
|
-
|
345
|
-
def test_post_where_not
|
346
|
-
product1 = create(:product, price: 100, category: "category1")
|
347
|
-
product2 = create(:product, price: 200, category: "category2")
|
348
|
-
product3 = create(:product, price: 300, category: "category1")
|
349
|
-
|
350
|
-
ProductIndex.import [product1, product2, product3]
|
351
|
-
|
352
|
-
query1 = ProductIndex.aggregate(:category).post_where_not(price: 250..350)
|
353
|
-
query2 = query1.post_where_not(category: "category2")
|
354
|
-
|
355
|
-
assert_includes query1.records, product1
|
356
|
-
assert_includes query1.records, product2
|
357
|
-
refute_includes query1.records, product3
|
358
|
-
|
359
|
-
assert_includes query2.records, product1
|
360
|
-
refute_includes query2.records, product2
|
361
|
-
refute_includes query2.records, product3
|
362
|
-
|
363
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
364
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
365
|
-
|
366
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
367
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
368
|
-
end
|
369
|
-
|
370
|
-
def test_post_where_not_with_array
|
371
|
-
expected = create(:product, title: "expected", category: "category1")
|
372
|
-
rejected1 = create(:product, title: "rejected1", category: "category2")
|
373
|
-
rejected2 = create(:product, title: "rejected2", category: "category1")
|
374
|
-
|
375
|
-
ProductIndex.import [expected, rejected1, rejected2]
|
376
|
-
|
377
|
-
query = ProductIndex.aggregate(:category).post_where_not(title: ["rejected1", "rejected2"])
|
378
|
-
|
379
|
-
assert_includes query.records, expected
|
380
|
-
refute_includes query.records, rejected1
|
381
|
-
refute_includes query.records, rejected2
|
382
|
-
|
383
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
384
|
-
query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
385
|
-
end
|
386
|
-
|
387
|
-
def test_post_where_not_with_range
|
388
|
-
expected = create(:product, price: 100, category: "category1")
|
389
|
-
rejected1 = create(:product, price: 200, category: "category2")
|
390
|
-
rejected2 = create(:product, price: 300, category: "category1")
|
391
|
-
|
392
|
-
ProductIndex.import [expected, rejected1, rejected2]
|
393
|
-
|
394
|
-
query = ProductIndex.aggregate(:category).post_where_not(price: 200..300)
|
395
|
-
|
396
|
-
assert_includes query.records, expected
|
397
|
-
refute_includes query.records, rejected1
|
398
|
-
refute_includes query.records, rejected2
|
399
|
-
|
400
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
401
|
-
query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
402
|
-
end
|
403
|
-
|
404
|
-
def test_post_filter
|
405
|
-
product1 = create(:product, price: 100, category: "category1")
|
406
|
-
product2 = create(:product, price: 200, category: "category2")
|
407
|
-
product3 = create(:product, price: 300, category: "category1")
|
408
|
-
|
409
|
-
ProductIndex.import [product1, product2, product3]
|
410
|
-
|
411
|
-
query1 = ProductIndex.aggregate(:category).post_filter(range: { price: { gte: 100, lte: 200 } })
|
412
|
-
query2 = query1.post_filter(term: { category: "category1" })
|
413
|
-
|
414
|
-
assert_includes query1.records, product1
|
415
|
-
assert_includes query1.records, product2
|
416
|
-
refute_includes query1.records, product3
|
417
|
-
|
418
|
-
assert_includes query2.records, product1
|
419
|
-
refute_includes query2.records, product2
|
420
|
-
refute_includes query2.records, product3
|
421
|
-
|
422
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
423
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
424
|
-
|
425
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
426
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
427
|
-
end
|
428
|
-
|
429
|
-
def test_post_range
|
430
|
-
product1 = create(:product, price: 100, category: "category1")
|
431
|
-
product2 = create(:product, price: 200, category: "category2")
|
432
|
-
product3 = create(:product, price: 300, category: "category1")
|
433
|
-
|
434
|
-
ProductIndex.import [product1, product2, product3]
|
435
|
-
|
436
|
-
query1 = ProductIndex.aggregate(:category).post_range(:price, gte: 100, lte: 200)
|
437
|
-
query2 = query1.post_range(:price, gte: 200, lte: 300)
|
438
|
-
|
439
|
-
assert_includes query1.records, product1
|
440
|
-
assert_includes query1.records, product2
|
441
|
-
refute_includes query1.records, product3
|
442
|
-
|
443
|
-
refute_includes query2.records, product1
|
444
|
-
assert_includes query2.records, product2
|
445
|
-
refute_includes query2.records, product3
|
446
|
-
|
447
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
448
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
449
|
-
|
450
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
451
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
452
|
-
end
|
453
|
-
|
454
|
-
def test_post_exists
|
455
|
-
product1 = create(:product, title: "title1", description: "description1", category: "category1")
|
456
|
-
product2 = create(:product, title: "title2", description: nil, category: "category2")
|
457
|
-
product3 = create(:product, title: nil, description: "description2", category: "category1")
|
458
|
-
|
459
|
-
ProductIndex.import [product1, product2, product3]
|
460
|
-
|
461
|
-
query1 = ProductIndex.aggregate(:category).post_exists(:title)
|
462
|
-
query2 = query1.post_exists(:description)
|
463
|
-
|
464
|
-
assert_includes query1.records, product1
|
465
|
-
assert_includes query1.records, product2
|
466
|
-
refute_includes query1.records, product3
|
467
|
-
|
468
|
-
assert_includes query2.records, product1
|
469
|
-
refute_includes query2.records, product2
|
470
|
-
refute_includes query2.records, product3
|
471
|
-
|
472
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
473
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
474
|
-
|
475
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
476
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
477
|
-
end
|
478
|
-
|
479
|
-
def test_post_exists_not
|
480
|
-
product1 = create(:product, title: nil, description: nil, category: "category1")
|
481
|
-
product2 = create(:product, title: nil, description: "description2", category: "category2")
|
482
|
-
product3 = create(:product, title: "title3", description: "description3", category: "category1")
|
483
|
-
|
484
|
-
ProductIndex.import [product1, product2, product3]
|
485
|
-
|
486
|
-
query1 = ProductIndex.aggregate(:category).post_exists_not(:title)
|
487
|
-
query2 = query1.post_exists_not(:description)
|
488
|
-
|
489
|
-
assert_includes query1.records, product1
|
490
|
-
assert_includes query1.records, product2
|
491
|
-
refute_includes query1.records, product3
|
492
|
-
|
493
|
-
assert_includes query2.records, product1
|
494
|
-
refute_includes query2.records, product2
|
495
|
-
refute_includes query2.records, product3
|
496
|
-
|
497
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
498
|
-
query1.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
499
|
-
|
500
|
-
assert_equal Hash["category1" => 2, "category2" => 1],
|
501
|
-
query2.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
502
|
-
end
|
503
|
-
|
504
|
-
def test_aggregate
|
505
|
-
ProductIndex.import create_list(:product, 3, category: "category1", price: 10)
|
506
|
-
ProductIndex.import create_list(:product, 2, category: "category2", price: 20)
|
507
|
-
ProductIndex.import create_list(:product, 1, category: "category3", price: 30)
|
508
|
-
|
509
|
-
query = ProductIndex.aggregate(:category, size: 2).aggregate(price_sum: { sum: { field: "price" } })
|
510
|
-
|
511
|
-
category_aggregations = query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
512
|
-
price_aggregation = query.aggregations(:price_sum).value
|
513
|
-
|
514
|
-
assert_equal Hash["category1" => 3, "category2" => 2], category_aggregations
|
515
|
-
assert_equal 100, price_aggregation
|
516
|
-
end
|
517
|
-
|
518
|
-
def test_aggregate_with_hash
|
519
|
-
ProductIndex.import create_list(:product, 3, category: "category1")
|
520
|
-
ProductIndex.import create_list(:product, 2, category: "category2")
|
521
|
-
ProductIndex.import create_list(:product, 1, category: "category3")
|
522
|
-
|
523
|
-
query = ProductIndex.aggregate(category: { terms: { field: :category } })
|
524
|
-
aggregations = query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
525
|
-
|
526
|
-
assert_equal Hash["category1" => 3, "category2" => 2, "category3" => 1], aggregations
|
527
|
-
end
|
528
|
-
|
529
|
-
def test_aggregate_with_subaggregation
|
530
|
-
ProductIndex.import create_list(:product, 3, category: "category1", price: 15)
|
531
|
-
ProductIndex.import create_list(:product, 2, category: "category2", price: 20)
|
532
|
-
ProductIndex.import create_list(:product, 1, category: "category3", price: 25)
|
533
|
-
|
534
|
-
query = ProductIndex.aggregate(:category) do |aggregation|
|
535
|
-
aggregation.aggregate(price_sum: { sum: { field: "price" } })
|
536
|
-
end
|
537
|
-
|
538
|
-
category_aggregations = query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
539
|
-
price_sum_aggregations = query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.price_sum.value }
|
540
|
-
|
541
|
-
assert_equal Hash["category1" => 3, "category2" => 2, "category3" => 1], category_aggregations
|
542
|
-
assert_equal Hash["category1" => 45, "category2" => 40, "category3" => 25], price_sum_aggregations
|
543
|
-
end
|
544
|
-
|
545
|
-
def test_profile
|
546
|
-
return if ProductIndex.connection.version.to_i < 2
|
547
|
-
|
548
|
-
assert_not_nil ProductIndex.profile(true).raw_response["profile"]
|
549
|
-
end
|
550
|
-
|
551
|
-
def test_scroll
|
552
|
-
products = create_list(:product, 15)
|
553
|
-
|
554
|
-
ProductIndex.import products
|
555
|
-
|
556
|
-
criteria = ProductIndex.limit(10).scroll(timeout: "1m")
|
557
|
-
|
558
|
-
result = []
|
559
|
-
iterations = 0
|
560
|
-
|
561
|
-
until criteria.records.empty?
|
562
|
-
result += criteria.records
|
563
|
-
iterations += 1
|
564
|
-
|
565
|
-
criteria = criteria.scroll(id: criteria.scroll_id, timeout: "1m")
|
566
|
-
end
|
567
|
-
|
568
|
-
assert_equal result.to_set, products.to_set
|
569
|
-
assert_equal 2, iterations
|
570
|
-
end
|
571
|
-
|
572
|
-
def test_delete
|
573
|
-
product1, product2, product3 = create_list(:product, 3)
|
574
|
-
|
575
|
-
ProductIndex.import [product1, product2, product3]
|
576
|
-
|
577
|
-
assert_difference "ProductIndex.total_entries", -2 do
|
578
|
-
ProductIndex.where(id: [product1.id, product2.id]).delete
|
579
|
-
end
|
580
|
-
end
|
581
|
-
|
582
|
-
def test_source
|
583
|
-
product = create(:product, title: "Title", price: 10)
|
584
|
-
|
585
|
-
ProductIndex.import product
|
586
|
-
|
587
|
-
results = ProductIndex.where(id: product.id).results
|
588
|
-
|
589
|
-
assert_present results.first.id
|
590
|
-
assert_equal "Title", results.first.title
|
591
|
-
assert_equal 10, results.first.price
|
592
|
-
|
593
|
-
results = ProductIndex.where(id: product.id).source([:id, :price]).results
|
594
|
-
|
595
|
-
assert_present results.first.id
|
596
|
-
assert_blank results.first.title
|
597
|
-
assert_present results.first.price
|
598
|
-
end
|
599
|
-
|
600
|
-
def test_includes
|
601
|
-
user = create(:user)
|
602
|
-
comments = create_list(:comment, 2)
|
603
|
-
product = create(:product, user: user, comments: comments)
|
604
|
-
|
605
|
-
ProductIndex.import product
|
606
|
-
|
607
|
-
record = ProductIndex.includes(:user).includes(:comments).records.first
|
608
|
-
|
609
|
-
assert_not_nil record
|
610
|
-
assert_equal user, record.user
|
611
|
-
assert_equal comments.to_set, record.comments.to_set
|
612
|
-
end
|
613
|
-
|
614
|
-
def test_eager_load
|
615
|
-
user = create(:user)
|
616
|
-
comments = create_list(:comment, 2)
|
617
|
-
product = create(:product, user: user, comments: comments)
|
618
|
-
|
619
|
-
ProductIndex.import product
|
620
|
-
|
621
|
-
record = ProductIndex.eager_load(:user).eager_load(:comments).records.first
|
622
|
-
|
623
|
-
assert_not_nil record
|
624
|
-
assert_equal user, record.user
|
625
|
-
assert_equal comments.to_set, record.comments.to_set
|
626
|
-
end
|
627
|
-
|
628
|
-
def test_preload
|
629
|
-
user = create(:user)
|
630
|
-
comments = create_list(:comment, 2)
|
631
|
-
product = create(:product, user: user, comments: comments)
|
632
|
-
|
633
|
-
ProductIndex.import product
|
634
|
-
|
635
|
-
record = ProductIndex.preload(:user).preload(:comments).records.first
|
636
|
-
|
637
|
-
assert_not_nil record
|
638
|
-
assert_equal user, record.user
|
639
|
-
assert_equal comments.to_set, record.comments.to_set
|
640
|
-
end
|
641
|
-
|
642
|
-
def test_sort
|
643
|
-
product1 = create(:product, rank: 2, price: 100)
|
644
|
-
product2 = create(:product, rank: 2, price: 90)
|
645
|
-
product3 = create(:product, rank: 1, price: 120)
|
646
|
-
product4 = create(:product, rank: 0, price: 110)
|
647
|
-
|
648
|
-
ProductIndex.import [product1, product2, product3, product4]
|
649
|
-
|
650
|
-
assert_equal [product2, product1, product3, product4], ProductIndex.sort({ rank: :desc }, { price: :asc }).records
|
651
|
-
assert_equal [product2, product1, product3, product4], ProductIndex.sort(rank: :desc).sort(:price).records
|
652
|
-
assert_equal [product2, product1, product4, product3], ProductIndex.sort(:price).sort(rank: :desc).records
|
653
|
-
end
|
654
|
-
|
655
|
-
def test_resort
|
656
|
-
product1 = create(:product, rank: 2, price: 100)
|
657
|
-
product2 = create(:product, rank: 2, price: 90)
|
658
|
-
product3 = create(:product, rank: 1, price: 120)
|
659
|
-
product4 = create(:product, rank: 0, price: 110)
|
660
|
-
|
661
|
-
ProductIndex.import [product1, product2, product3, product4]
|
662
|
-
|
663
|
-
assert_equal [product2, product1, product3, product4], ProductIndex.sort(:price).resort({ rank: :desc }, { price: :asc }).records
|
664
|
-
assert_equal [product2, product1, product4, product3], ProductIndex.sort(rank: :desc).resort(:price).records
|
665
|
-
end
|
666
|
-
|
667
|
-
def test_offset
|
668
|
-
product1 = create(:product, rank: 1)
|
669
|
-
product2 = create(:product, rank: 2)
|
670
|
-
product3 = create(:product, rank: 3)
|
671
|
-
|
672
|
-
ProductIndex.import [product1, product2, product3]
|
673
|
-
|
674
|
-
query = ProductIndex.sort(:rank).offset(1)
|
675
|
-
|
676
|
-
assert_equal [product2, product3], query.records
|
677
|
-
assert_equal [product3], query.offset(2).records
|
678
|
-
end
|
679
|
-
|
680
|
-
def test_limit
|
681
|
-
product1 = create(:product, rank: 1)
|
682
|
-
product2 = create(:product, rank: 2)
|
683
|
-
product3 = create(:product, rank: 3)
|
684
|
-
|
685
|
-
ProductIndex.import [product1, product2, product3]
|
686
|
-
|
687
|
-
query = ProductIndex.sort(:rank).limit(1)
|
688
|
-
|
689
|
-
assert_equal [product1], query.records
|
690
|
-
assert_equal [product1, product2], query.limit(2).records
|
691
|
-
end
|
692
|
-
|
693
|
-
def test_paginate
|
694
|
-
product1 = create(:product, rank: 1)
|
695
|
-
product2 = create(:product, rank: 2)
|
696
|
-
product3 = create(:product, rank: 3)
|
697
|
-
|
698
|
-
ProductIndex.import [product1, product2, product3]
|
699
|
-
|
700
|
-
query = ProductIndex.sort(:rank).paginate(page: 1, per_page: 2)
|
701
|
-
|
702
|
-
assert_equal [product1, product2], query.records
|
703
|
-
assert_equal [product3], query.paginate(page: 2, per_page: 2).records
|
704
|
-
end
|
705
|
-
|
706
|
-
def test_page
|
707
|
-
assert_equal 0, ProductIndex.page(1).offset_value
|
708
|
-
assert_equal 30, ProductIndex.page(2).offset_value
|
709
|
-
assert_equal 100, ProductIndex.page(3).per(50).offset_value
|
710
|
-
end
|
711
|
-
|
712
|
-
def test_per
|
713
|
-
assert_equal 50, ProductIndex.per(50).limit_value
|
714
|
-
end
|
715
|
-
|
716
|
-
def test_search
|
717
|
-
product1 = create(:product, title: "Title1", description: "Description1", price: 10)
|
718
|
-
product2 = create(:product, title: "Title2", description: "Description2", price: 20)
|
719
|
-
product3 = create(:product, title: "Title3", description: "Description2", price: 30)
|
720
|
-
|
721
|
-
ProductIndex.import [product1, product2, product3]
|
722
|
-
|
723
|
-
assert_equal [product1, product3].to_set, ProductIndex.search("Title1 OR Title3").records.to_set
|
724
|
-
assert_equal [product1, product3].to_set, ProductIndex.search("Title1 Title3", default_operator: :OR).records.to_set
|
725
|
-
assert_equal [product1], ProductIndex.search("Title1 OR Title2").search("Title1 OR Title3").records
|
726
|
-
assert_equal [product1], ProductIndex.search("Title1 OR Title3").where(price: 5..15).records
|
727
|
-
end
|
728
|
-
|
729
|
-
def test_unscope
|
730
|
-
product1 = create(:product, title: "Title1", description: "Description1", price: 10)
|
731
|
-
product2 = create(:product, title: "Title2", description: "Description2", price: 20)
|
732
|
-
product3 = create(:product, title: "Title3", description: "Description2", price: 30)
|
733
|
-
|
734
|
-
ProductIndex.import [product1, product2, product3]
|
735
|
-
|
736
|
-
assert_equal [product1], ProductIndex.search("Title1 OR Title2").search("Title1 OR Title3").records
|
737
|
-
assert_equal [product1, product3].to_set, ProductIndex.search("Title1 OR Title2").unscope(:search).search("Title1 OR Title3").records.to_set
|
738
|
-
end
|
739
|
-
|
740
|
-
def test_highlight
|
741
|
-
product1 = create(:product, title: "Title1 highlight", description: "Description1 highlight")
|
742
|
-
product2 = create(:product, title: "Title2 highlight", description: "Description2 highlight")
|
743
|
-
|
744
|
-
ProductIndex.import [product1, product2]
|
745
|
-
|
746
|
-
results = ProductIndex.sort(:id).highlight([:title, :description]).search("title:highlight description:highlight").results
|
747
|
-
|
748
|
-
assert_equal ["Title1 <em>highlight</em>"], results[0].highlight.title
|
749
|
-
assert_equal ["Description1 <em>highlight</em>"], results[0].highlight.description
|
750
|
-
|
751
|
-
assert_equal ["Title2 <em>highlight</em>"], results[1].highlight.title
|
752
|
-
assert_equal ["Description2 <em>highlight</em>"], results[1].highlight.description
|
753
|
-
|
754
|
-
results = ProductIndex.sort(:id).highlight([:title, :description], require_field_match: false).search("highlight").results
|
755
|
-
|
756
|
-
assert_equal ["Title1 <em>highlight</em>"], results[0].highlight.title
|
757
|
-
assert_equal ["Description1 <em>highlight</em>"], results[0].highlight.description
|
758
|
-
|
759
|
-
assert_equal ["Title2 <em>highlight</em>"], results[1].highlight.title
|
760
|
-
assert_equal ["Description2 <em>highlight</em>"], results[1].highlight.description
|
761
|
-
|
762
|
-
query = ProductIndex.sort(:id).search("title:highlight")
|
763
|
-
query = query.highlight(:title, require_field_match: true).highlight(:description, require_field_match: true)
|
764
|
-
|
765
|
-
results = query.results
|
766
|
-
|
767
|
-
assert_equal ["Title1 <em>highlight</em>"], results[0].highlight.title
|
768
|
-
assert_nil results[0].highlight.description
|
769
|
-
|
770
|
-
assert_equal ["Title2 <em>highlight</em>"], results[1].highlight.title
|
771
|
-
assert_nil results[1].highlight.description
|
772
|
-
end
|
773
|
-
|
774
|
-
def test_suggest
|
775
|
-
product = create(:product, title: "Title", description: "Description")
|
776
|
-
|
777
|
-
ProductIndex.import product
|
778
|
-
|
779
|
-
assert_equal "description",
|
780
|
-
ProductIndex.suggest(:suggestion, text: "Desciption", term: { field: "description" }).suggestions(:suggestion).first["text"]
|
781
|
-
end
|
782
|
-
|
783
|
-
def test_find_in_batches
|
784
|
-
expected1 = create(:product, title: "expected", rank: 1)
|
785
|
-
expected2 = create(:product, title: "expected", rank: 2)
|
786
|
-
expected3 = create(:product, title: "expected", rank: 3)
|
787
|
-
rejected = create(:product, title: "rejected")
|
788
|
-
|
789
|
-
create :product, title: "rejected"
|
790
|
-
|
791
|
-
ProductIndex.import [expected1, expected2, expected3, rejected]
|
792
|
-
|
793
|
-
assert_equal [[expected1, expected2], [expected3]],
|
794
|
-
ProductIndex.where(title: "expected").sort(:rank).find_in_batches(batch_size: 2).to_a
|
795
|
-
end
|
796
|
-
|
797
|
-
def test_find_results_in_batches
|
798
|
-
expected1 = create(:product, title: "expected", rank: 1)
|
799
|
-
expected2 = create(:product, title: "expected", rank: 2)
|
800
|
-
expected3 = create(:product, title: "expected", rank: 3)
|
801
|
-
rejected = create(:product, title: "rejected")
|
802
|
-
|
803
|
-
create :product, title: "rejected"
|
804
|
-
|
805
|
-
ProductIndex.import [expected1, expected2, expected3, rejected]
|
806
|
-
|
807
|
-
actual = ProductIndex.where(title: "expected").sort(:rank).find_results_in_batches(batch_size: 2).map { |batch| batch.map(&:id) }
|
808
|
-
|
809
|
-
assert_equal [[expected1.id, expected2.id], [expected3.id]], actual
|
810
|
-
end
|
811
|
-
|
812
|
-
def test_find_each
|
813
|
-
expected1 = create(:product, title: "expected", rank: 1)
|
814
|
-
expected2 = create(:product, title: "expected", rank: 2)
|
815
|
-
expected3 = create(:product, title: "expected", rank: 3)
|
816
|
-
rejected = create(:product, title: "rejected")
|
817
|
-
|
818
|
-
create :product, title: "rejected"
|
819
|
-
|
820
|
-
ProductIndex.import [expected1, expected2, expected3, rejected]
|
821
|
-
|
822
|
-
assert_equal [expected1, expected2, expected3], ProductIndex.where(title: "expected").sort(:rank).find_each(batch_size: 2).to_a
|
823
|
-
end
|
824
|
-
|
825
|
-
def test_failsafe
|
826
|
-
assert_raises SearchFlip::ResponseError do
|
827
|
-
ProductIndex.search("syntax/error").records
|
828
|
-
end
|
829
|
-
|
830
|
-
query = ProductIndex.failsafe(true).search("syntax/error")
|
831
|
-
|
832
|
-
assert_equal [], query.records
|
833
|
-
assert_equal 0, query.total_entries
|
834
|
-
end
|
835
|
-
|
836
|
-
def test_fresh
|
837
|
-
create :product
|
838
|
-
|
839
|
-
query = ProductIndex.criteria.tap(&:records)
|
840
|
-
|
841
|
-
assert_not_nil query.instance_variable_get(:@response)
|
842
|
-
|
843
|
-
refute_equal query.fresh.object_id, query.object_id
|
844
|
-
assert_nil query.fresh.instance_variable_get(:@response)
|
845
|
-
end
|
846
|
-
|
847
|
-
def test_respond_to?
|
848
|
-
temp_index = Class.new(ProductIndex)
|
849
|
-
|
850
|
-
refute temp_index.criteria.respond_to?(:test_scope)
|
851
|
-
|
852
|
-
temp_index.scope(:test_scope) { match_all }
|
853
|
-
|
854
|
-
assert temp_index.criteria.respond_to?(:test_scope)
|
855
|
-
end
|
856
|
-
|
857
|
-
def test_method_missing
|
858
|
-
temp_index = Class.new(ProductIndex)
|
859
|
-
|
860
|
-
expected = create(:product, title: "expected")
|
861
|
-
rejected = create(:product, title: "rejected")
|
862
|
-
|
863
|
-
temp_index.import [expected, rejected]
|
864
|
-
|
865
|
-
temp_index.scope(:with_title) { |title| where(title: title) }
|
866
|
-
|
867
|
-
records = temp_index.criteria.with_title("expected").records
|
868
|
-
|
869
|
-
assert_includes records, expected
|
870
|
-
refute_includes records, rejected
|
871
|
-
end
|
872
|
-
|
873
|
-
def test_custom
|
874
|
-
request = ProductIndex.custom(custom_key: "custom_value").request
|
875
|
-
|
876
|
-
assert_equal "custom_value", request[:custom_key]
|
877
|
-
end
|
878
|
-
end
|
879
|
-
|