search_flip 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +34 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +606 -0
- data/Rakefile +9 -0
- data/irb.rb +7 -0
- data/lib/search_flip/aggregatable.rb +69 -0
- data/lib/search_flip/aggregation.rb +57 -0
- data/lib/search_flip/bulk.rb +152 -0
- data/lib/search_flip/config.rb +21 -0
- data/lib/search_flip/criteria.rb +737 -0
- data/lib/search_flip/filterable.rb +240 -0
- data/lib/search_flip/http_client.rb +49 -0
- data/lib/search_flip/index.rb +545 -0
- data/lib/search_flip/json.rb +18 -0
- data/lib/search_flip/model.rb +21 -0
- data/lib/search_flip/post_filterable.rb +252 -0
- data/lib/search_flip/response.rb +319 -0
- data/lib/search_flip/result.rb +12 -0
- data/lib/search_flip/to_json.rb +31 -0
- data/lib/search_flip/version.rb +5 -0
- data/lib/search_flip.rb +82 -0
- data/search_flip.gemspec +35 -0
- data/test/database.yml +4 -0
- data/test/search_flip/aggregation_test.rb +212 -0
- data/test/search_flip/bulk_test.rb +55 -0
- data/test/search_flip/criteria_test.rb +825 -0
- data/test/search_flip/http_client_test.rb +35 -0
- data/test/search_flip/index_test.rb +350 -0
- data/test/search_flip/model_test.rb +39 -0
- data/test/search_flip/response_test.rb +136 -0
- data/test/search_flip/to_json_test.rb +30 -0
- data/test/search_flip_test.rb +26 -0
- data/test/test_helper.rb +243 -0
- metadata +258 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
|
+
|
4
|
+
class SearchFlip::HTTPClientTest < SearchFlip::TestCase
|
5
|
+
[:get, :put, :delete, :post, :head].each do |method|
|
6
|
+
define_method :"test_#{method}" do
|
7
|
+
stub_request(method, "http://localhost/path").with(body: "body", query: { key: "value" }).to_return(body: "success")
|
8
|
+
|
9
|
+
assert_equal "success", SearchFlip::HTTPClient.send(method, "http://localhost/path", body: "body", params: { key: "value" }).body.to_s
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_headers
|
14
|
+
stub_request(:get, "http://localhost/path").with(headers: { "X-Key" => "Value" }).to_return(body: "success")
|
15
|
+
|
16
|
+
assert_equal "success", SearchFlip::HTTPClient.headers("X-Key" => "Value").get("http://localhost/path").body.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_connection_error
|
20
|
+
stub_request(:get, "http://localhost/path").to_raise(HTTP::ConnectionError)
|
21
|
+
|
22
|
+
assert_raises SearchFlip::ConnectionError do
|
23
|
+
SearchFlip::HTTPClient.get("http://localhost/path")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_response_error
|
28
|
+
stub_request(:get, "http://localhost/path").to_return(status: 500)
|
29
|
+
|
30
|
+
assert_raises SearchFlip::ResponseError do
|
31
|
+
SearchFlip::HTTPClient.get("http://localhost/path")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,350 @@
|
|
1
|
+
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
|
+
|
4
|
+
class SearchFlip::IndexTest < SearchFlip::TestCase
|
5
|
+
should_delegate_methods :profile, :where, :where_not, :filter, :range, :match_all, :exists, :exists_not, :post_where,
|
6
|
+
:post_where_not, :post_filter, :post_range, :post_exists, :post_exists_not, :aggregate, :scroll, :source, :includes,
|
7
|
+
:eager_load, :preload, :sort, :resort, :order, :reorder, :offset, :limit, :paginate, :page, :per, :search,
|
8
|
+
:find_in_batches, :highlight, :suggest, :custom, :find_each, :failsafe, :total_entries, :total_count, :terminate_after,
|
9
|
+
:timeout, to: :criteria, subject: ProductIndex
|
10
|
+
|
11
|
+
def test_create_index
|
12
|
+
assert TestIndex.create_index
|
13
|
+
assert TestIndex.index_exists?
|
14
|
+
|
15
|
+
TestIndex.delete_index
|
16
|
+
|
17
|
+
refute TestIndex.index_exists?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_create_index_with_index_settings
|
21
|
+
TestIndex.stubs(:index_settings).returns(settings: { number_of_shards: 3 })
|
22
|
+
|
23
|
+
assert TestIndex.create_index
|
24
|
+
assert TestIndex.index_exists?
|
25
|
+
|
26
|
+
assert_equal "3", TestIndex.get_index_settings["test"]["settings"]["index"]["number_of_shards"]
|
27
|
+
ensure
|
28
|
+
TestIndex.delete_index if TestIndex.index_exists?
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_update_index_settings
|
32
|
+
assert TestIndex.create_index
|
33
|
+
|
34
|
+
TestIndex.stubs(:index_settings).returns(settings: { number_of_replicas: 3 })
|
35
|
+
|
36
|
+
assert TestIndex.update_index_settings
|
37
|
+
|
38
|
+
assert_equal "3", TestIndex.get_index_settings["test"]["settings"]["index"]["number_of_replicas"]
|
39
|
+
ensure
|
40
|
+
TestIndex.delete_index if TestIndex.index_exists?
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_get_index_settings
|
44
|
+
# Already tested
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_index_exists?
|
48
|
+
# Already tested
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_delete_index
|
52
|
+
# Already tested
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_update_mapping
|
56
|
+
TestIndex.create_index
|
57
|
+
TestIndex.update_mapping
|
58
|
+
|
59
|
+
mapping = TestIndex.get_mapping
|
60
|
+
|
61
|
+
assert mapping["test"]["mappings"]["test"]["properties"]["test_field"]
|
62
|
+
|
63
|
+
TestIndex.delete_index
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_get_mapping
|
67
|
+
# Aready tested
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_refresh
|
71
|
+
assert_difference "ProductIndex.total_entries" do
|
72
|
+
ProductIndex.import create(:product)
|
73
|
+
|
74
|
+
assert ProductIndex.refresh
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_base_url
|
79
|
+
assert_equal "http://127.0.0.1:9200", ProductIndex.base_url
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_index_url
|
83
|
+
assert_equal "http://127.0.0.1:9200/products", ProductIndex.index_url
|
84
|
+
|
85
|
+
ProductIndex.stubs(:type_name).returns("products2")
|
86
|
+
|
87
|
+
assert_equal "http://127.0.0.1:9200/products2", ProductIndex.index_url
|
88
|
+
|
89
|
+
SearchFlip::Config[:index_prefix] = "prefix-"
|
90
|
+
|
91
|
+
assert_equal "http://127.0.0.1:9200/prefix-products2", ProductIndex.index_url
|
92
|
+
|
93
|
+
ProductIndex.stubs(:index_name).returns("products3")
|
94
|
+
|
95
|
+
assert_equal "http://127.0.0.1:9200/prefix-products3", ProductIndex.index_url
|
96
|
+
|
97
|
+
SearchFlip::Config[:index_prefix] = nil
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_type_url
|
101
|
+
assert_equal "http://127.0.0.1:9200/products/products", ProductIndex.type_url
|
102
|
+
|
103
|
+
ProductIndex.stubs(:type_name).returns("products2")
|
104
|
+
|
105
|
+
assert_equal "http://127.0.0.1:9200/products2/products2", ProductIndex.type_url
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_import_object
|
109
|
+
assert_difference "ProductIndex.total_entries" do
|
110
|
+
ProductIndex.import create(:product)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_import_array
|
115
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
116
|
+
ProductIndex.import [create(:product), create(:product)]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_import_scope
|
121
|
+
create_list :product, 2
|
122
|
+
|
123
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
124
|
+
ProductIndex.import Product.all
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_import_with_param_options
|
129
|
+
products = create_list(:product, 2)
|
130
|
+
|
131
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
132
|
+
ProductIndex.import products, {}, version: 1, version_type: "external"
|
133
|
+
end
|
134
|
+
|
135
|
+
assert_no_difference "ProductIndex.total_entries" do
|
136
|
+
ProductIndex.import products, {}, version: 2, version_type: "external"
|
137
|
+
ProductIndex.import products, { ignore_errors: [409] }, version: 2, version_type: "external"
|
138
|
+
|
139
|
+
assert_raises SearchFlip::Bulk::Error do
|
140
|
+
ProductIndex.import products, {}, version: 2, version_type: "external"
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_import_with_class_options
|
146
|
+
products = create_list(:product, 2)
|
147
|
+
|
148
|
+
ProductIndex.stubs(:index_options).returns(version: 3, version_type: "external")
|
149
|
+
|
150
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
151
|
+
ProductIndex.import products
|
152
|
+
end
|
153
|
+
|
154
|
+
assert_equal [3, 3], products.map { |product| ProductIndex.get(product.id)["_version"] }
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_index_array
|
158
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
159
|
+
ProductIndex.index create_list(:product, 2)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_index_scope
|
164
|
+
temp_product_index = Class.new(ProductIndex)
|
165
|
+
|
166
|
+
temp_product_index.define_singleton_method(:index_scope) do |scope|
|
167
|
+
scope.where("price < 80")
|
168
|
+
end
|
169
|
+
|
170
|
+
products = [create(:product, price: 20), create(:product, price: 50), create(:product, price: 100)]
|
171
|
+
|
172
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
173
|
+
temp_product_index.index Product.where(id: products.map(&:id))
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_create_array
|
178
|
+
products = create_list(:product, 2)
|
179
|
+
|
180
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
181
|
+
ProductIndex.create products
|
182
|
+
end
|
183
|
+
|
184
|
+
assert_no_difference "ProductIndex.total_entries" do
|
185
|
+
assert_raises SearchFlip::Bulk::Error do
|
186
|
+
ProductIndex.create products
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_create_scope
|
192
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
193
|
+
ProductIndex.create Product.where(id: create_list(:product, 2).map(&:id))
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_update_array
|
198
|
+
products = create_list(:product, 2)
|
199
|
+
|
200
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
201
|
+
ProductIndex.import products
|
202
|
+
end
|
203
|
+
|
204
|
+
assert_no_difference "ProductIndex.total_entries" do
|
205
|
+
ProductIndex.update products
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_update_scope
|
210
|
+
products = create_list(:product, 2)
|
211
|
+
|
212
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
213
|
+
ProductIndex.import products
|
214
|
+
end
|
215
|
+
|
216
|
+
assert_no_difference "ProductIndex.total_entries" do
|
217
|
+
ProductIndex.update Product.where(id: products.map(&:id))
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_delete_array
|
222
|
+
products = create_list(:product, 2)
|
223
|
+
|
224
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
225
|
+
ProductIndex.import products
|
226
|
+
end
|
227
|
+
|
228
|
+
assert_difference "ProductIndex.total_entries", -2 do
|
229
|
+
ProductIndex.delete products
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_delete_scope
|
234
|
+
products = create_list(:product, 2)
|
235
|
+
|
236
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
237
|
+
ProductIndex.import products
|
238
|
+
end
|
239
|
+
|
240
|
+
assert_difference "ProductIndex.total_entries", -2 do
|
241
|
+
ProductIndex.delete Product.where(id: Product.where(id: products.map(&:id)))
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_create_already_created
|
246
|
+
products = create_list(:product, 2)
|
247
|
+
|
248
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
249
|
+
ProductIndex.create products
|
250
|
+
end
|
251
|
+
|
252
|
+
assert_raises SearchFlip::Bulk::Error do
|
253
|
+
ProductIndex.create products
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_create_with_param_options
|
258
|
+
products = create_list(:product, 2)
|
259
|
+
|
260
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
261
|
+
ProductIndex.create products
|
262
|
+
end
|
263
|
+
|
264
|
+
assert_no_difference "ProductIndex.total_entries" do
|
265
|
+
ProductIndex.create products, ignore_errors: [409]
|
266
|
+
end
|
267
|
+
|
268
|
+
products = create_list(:product, 2)
|
269
|
+
|
270
|
+
if SearchFlip.version.to_i >= 5
|
271
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
272
|
+
ProductIndex.create products, {}, routing: "r1"
|
273
|
+
end
|
274
|
+
|
275
|
+
assert_equal "r1", ProductIndex.get(products.first.id, routing: "r1")["_routing"]
|
276
|
+
else
|
277
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
278
|
+
ProductIndex.create products, {}, version: 2, version_type: "external"
|
279
|
+
end
|
280
|
+
|
281
|
+
assert_equal [2, 2], products.map { |product| ProductIndex.get(product.id)["_version"] }
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_create_with_class_options
|
286
|
+
products = create_list(:product, 2)
|
287
|
+
|
288
|
+
if SearchFlip.version.to_i >= 5
|
289
|
+
ProductIndex.stubs(:index_options).returns(routing: "r1")
|
290
|
+
|
291
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
292
|
+
ProductIndex.create products
|
293
|
+
end
|
294
|
+
|
295
|
+
assert_equal ["r1", "r1"], products.map { |product| ProductIndex.get(product.id, routing: "r1")["_routing"] }
|
296
|
+
else
|
297
|
+
ProductIndex.stubs(:index_options).returns(version: 2, version_type: "external")
|
298
|
+
|
299
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
300
|
+
ProductIndex.create products
|
301
|
+
end
|
302
|
+
|
303
|
+
assert_equal [2, 2], products.map { |product| ProductIndex.get(product.id)["_version"] }
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_get
|
308
|
+
# Already tested
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_scope
|
312
|
+
temp_product_index = Class.new(ProductIndex)
|
313
|
+
|
314
|
+
temp_product_index.scope(:with_title) { |title| where(title: title) }
|
315
|
+
|
316
|
+
expected = create(:product, title: "expected")
|
317
|
+
rejected = create(:product, title: "rejected")
|
318
|
+
|
319
|
+
temp_product_index.import [expected, rejected]
|
320
|
+
|
321
|
+
results = temp_product_index.with_title("expected").records
|
322
|
+
|
323
|
+
assert_includes results, expected
|
324
|
+
refute_includes results, rejected
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_bulk
|
328
|
+
assert_difference "ProductIndex.total_entries", 2 do
|
329
|
+
ProductIndex.bulk do |indexer|
|
330
|
+
indexer.index 1, id: 1
|
331
|
+
indexer.index 2, id: 2
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
assert_no_difference "ProductIndex.total_entries" do
|
336
|
+
assert_raises "SearchFlip::Bulk::Error" do
|
337
|
+
ProductIndex.bulk do |indexer|
|
338
|
+
indexer.index 1, { id: 1 }, version: 1, version_type: "external"
|
339
|
+
indexer.index 2, { id: 2 }, version: 1, version_type: "external"
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
ProductIndex.bulk ignore_errors: [409] do |indexer|
|
344
|
+
indexer.index 1, { id: 1 }, version: 1, version_type: "external"
|
345
|
+
indexer.index 2, { id: 2 }, version: 1, version_type: "external"
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
|
+
|
4
|
+
class SearchFlip::ModelTest < SearchFlip::TestCase
|
5
|
+
class TestProduct < Product
|
6
|
+
include SearchFlip::Model
|
7
|
+
|
8
|
+
notifies_index(ProductIndex)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_save
|
12
|
+
assert_equal 0, ProductIndex.total_count
|
13
|
+
|
14
|
+
TestProduct.create!
|
15
|
+
|
16
|
+
assert_equal 1, ProductIndex.total_count
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_destroy
|
20
|
+
test_product = TestProduct.create!
|
21
|
+
|
22
|
+
assert_equal 1, ProductIndex.total_count
|
23
|
+
|
24
|
+
test_product.destroy
|
25
|
+
|
26
|
+
assert_equal 0, ProductIndex.total_count
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_touch
|
30
|
+
test_product = Timecop.freeze(Time.parse("2016-01-01 12:00:00")) { TestProduct.create! }
|
31
|
+
|
32
|
+
updated_at = ProductIndex.match_all.results.first.updated_at
|
33
|
+
|
34
|
+
Timecop.freeze(Time.parse("2017-01-01 12:00:00")) { test_product.touch }
|
35
|
+
|
36
|
+
refute_equal updated_at, ProductIndex.match_all.results.first.updated_at
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,136 @@
|
|
1
|
+
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
|
+
|
4
|
+
class SearchFlip::ResponseTest < SearchFlip::TestCase
|
5
|
+
def test_total_entries
|
6
|
+
ProductIndex.import create_list(:product, 3)
|
7
|
+
|
8
|
+
assert_equal 3, ProductIndex.total_entries
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_curent_page
|
12
|
+
assert_equal 1, ProductIndex.match_all.current_page
|
13
|
+
|
14
|
+
ProductIndex.import create_list(:product, 3)
|
15
|
+
|
16
|
+
assert_equal 1, ProductIndex.paginate(page: 1, per_page: 2).current_page
|
17
|
+
assert_equal 2, ProductIndex.paginate(page: 2, per_page: 2).current_page
|
18
|
+
assert_equal 3, ProductIndex.paginate(page: 3, per_page: 2).current_page
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_total_pages
|
22
|
+
assert_equal 1, ProductIndex.paginate(page: 1, per_page: 2).total_pages
|
23
|
+
|
24
|
+
ProductIndex.import create_list(:product, 3)
|
25
|
+
|
26
|
+
assert_equal 2, ProductIndex.paginate(page: 1, per_page: 2).total_pages
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_previous_page
|
30
|
+
ProductIndex.import create_list(:product, 3)
|
31
|
+
|
32
|
+
assert_nil ProductIndex.paginate(page: 1, per_page: 2).previous_page
|
33
|
+
assert_equal 1, ProductIndex.paginate(page: 2, per_page: 2).previous_page
|
34
|
+
assert_equal 2, ProductIndex.paginate(page: 3, per_page: 2).previous_page
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_next_page
|
38
|
+
ProductIndex.import create_list(:product, 3)
|
39
|
+
|
40
|
+
assert_equal 2, ProductIndex.paginate(page: 1, per_page: 2).next_page
|
41
|
+
assert_nil ProductIndex.paginate(page: 2, per_page: 2).next_page
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_first_page?
|
45
|
+
ProductIndex.import create(:product)
|
46
|
+
|
47
|
+
assert ProductIndex.paginate(page: 1).first_page?
|
48
|
+
refute ProductIndex.paginate(page: 2).first_page?
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_last_page?
|
52
|
+
ProductIndex.import create_list(:product, 31)
|
53
|
+
|
54
|
+
assert ProductIndex.paginate(page: 2).last_page?
|
55
|
+
refute ProductIndex.paginate(page: 1).last_page?
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_out_of_range?
|
59
|
+
ProductIndex.import create(:product)
|
60
|
+
|
61
|
+
assert ProductIndex.paginate(page: 2).out_of_range?
|
62
|
+
refute ProductIndex.paginate(page: 1).out_of_range?
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_results
|
66
|
+
products = create_list(:product, 3)
|
67
|
+
|
68
|
+
ProductIndex.import products
|
69
|
+
|
70
|
+
assert_equal products.map(&:id).to_set, ProductIndex.match_all.results.map(&:id).to_set
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_hits
|
74
|
+
ProductIndex.import create_list(:product, 3)
|
75
|
+
|
76
|
+
response = ProductIndex.match_all.response
|
77
|
+
|
78
|
+
assert_present response.hits
|
79
|
+
assert_equal response.raw_response["hits"], response.hits
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_scroll_id
|
83
|
+
ProductIndex.import create_list(:product, 3)
|
84
|
+
|
85
|
+
response = ProductIndex.scroll.response
|
86
|
+
|
87
|
+
assert_present response.scroll_id
|
88
|
+
assert_equal response.raw_response["_scroll_id"], response.scroll_id
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_records
|
92
|
+
products = create_list(:product, 3)
|
93
|
+
|
94
|
+
ProductIndex.import products
|
95
|
+
|
96
|
+
assert_equal products.to_set, ProductIndex.match_all.records.to_set
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_ids
|
100
|
+
products = create_list(:product, 3)
|
101
|
+
|
102
|
+
ProductIndex.import products
|
103
|
+
|
104
|
+
response = ProductIndex.match_all.response
|
105
|
+
|
106
|
+
assert_equal products.map(&:id).map(&:to_s).to_set, response.ids.to_set
|
107
|
+
assert_equal response.raw_response["hits"]["hits"].map { |hit| hit["_id"] }, response.ids
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_took
|
111
|
+
ProductIndex.import create_list(:product, 3)
|
112
|
+
|
113
|
+
response = ProductIndex.match_all.response
|
114
|
+
|
115
|
+
assert_present response.took
|
116
|
+
assert_equal response.raw_response["took"], response.took
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_aggregations
|
120
|
+
product1 = create(:product, price: 10, category: "category1")
|
121
|
+
product2 = create(:product, price: 20, category: "category2")
|
122
|
+
product3 = create(:product, price: 30, category: "category1")
|
123
|
+
|
124
|
+
ProductIndex.import [product1, product2, product3]
|
125
|
+
|
126
|
+
query = ProductIndex.aggregate(:category) do |aggregation|
|
127
|
+
aggregation.aggregate(price_sum: { sum: { field: "price" }})
|
128
|
+
end
|
129
|
+
|
130
|
+
assert_equal Hash["category1" => 2, "category2" => 1], query.aggregations(:category).each_with_object({}) { |(key, agg), hash| hash[key] = agg.doc_count }
|
131
|
+
|
132
|
+
assert_equal 40, query.aggregations(:category)["category1"].price_sum.value
|
133
|
+
assert_equal 20, query.aggregations(:category)["category2"].price_sum.value
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
|
+
require "search_flip/to_json"
|
4
|
+
|
5
|
+
class SearchFlip::ToJsonTest < SearchFlip::TestCase
|
6
|
+
def test_time
|
7
|
+
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
8
|
+
assert_equal "\"2018-01-01T12:00:00.000000Z\"", Time.now.utc.to_json
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_date
|
13
|
+
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
14
|
+
assert_equal "\"2018-01-01\"", Date.today.to_json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_date_time
|
19
|
+
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
20
|
+
assert_equal "\"2018-01-01T12:00:00.000000Z\"", Time.now.utc.to_json
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_time_with_zone
|
25
|
+
Timecop.freeze Time.parse("2018-01-01 12:00:00 UTC") do
|
26
|
+
assert_equal "\"2018-01-01T12:00:00.000000Z\"", Time.find_zone("UTC").now.to_json
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
require File.expand_path("../test_helper", __FILE__)
|
3
|
+
|
4
|
+
class SearchFlipTest < SearchFlip::TestCase
|
5
|
+
def test_msearch
|
6
|
+
ProductIndex.import create(:product)
|
7
|
+
CommentIndex.import create(:comment)
|
8
|
+
|
9
|
+
responses = SearchFlip.msearch([ProductIndex.match_all, CommentIndex.match_all])
|
10
|
+
|
11
|
+
assert_equal 2, responses.size
|
12
|
+
assert_equal 1, responses[0].total_entries
|
13
|
+
assert_equal 1, responses[1].total_entries
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_aliases
|
17
|
+
assert SearchFlip.aliases(actions: [
|
18
|
+
add: { index: "products", alias: "alias1" }
|
19
|
+
])
|
20
|
+
|
21
|
+
assert SearchFlip.aliases(actions: [
|
22
|
+
remove: { index: "products", alias: "alias1" }
|
23
|
+
])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|