searchkick-hooopo 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +35 -0
- data/CHANGELOG.md +491 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +1908 -0
- data/Rakefile +20 -0
- data/benchmark/Gemfile +23 -0
- data/benchmark/benchmark.rb +97 -0
- data/lib/searchkick/bulk_reindex_job.rb +17 -0
- data/lib/searchkick/index.rb +500 -0
- data/lib/searchkick/index_options.rb +333 -0
- data/lib/searchkick/indexer.rb +28 -0
- data/lib/searchkick/logging.rb +242 -0
- data/lib/searchkick/middleware.rb +12 -0
- data/lib/searchkick/model.rb +156 -0
- data/lib/searchkick/process_batch_job.rb +23 -0
- data/lib/searchkick/process_queue_job.rb +23 -0
- data/lib/searchkick/query.rb +901 -0
- data/lib/searchkick/reindex_queue.rb +38 -0
- data/lib/searchkick/reindex_v2_job.rb +39 -0
- data/lib/searchkick/results.rb +216 -0
- data/lib/searchkick/tasks.rb +33 -0
- data/lib/searchkick/version.rb +3 -0
- data/lib/searchkick.rb +215 -0
- data/searchkick.gemspec +28 -0
- data/test/aggs_test.rb +197 -0
- data/test/autocomplete_test.rb +75 -0
- data/test/boost_test.rb +175 -0
- data/test/callbacks_test.rb +59 -0
- data/test/ci/before_install.sh +17 -0
- data/test/errors_test.rb +19 -0
- data/test/gemfiles/activerecord31.gemfile +7 -0
- data/test/gemfiles/activerecord32.gemfile +7 -0
- data/test/gemfiles/activerecord40.gemfile +8 -0
- data/test/gemfiles/activerecord41.gemfile +8 -0
- data/test/gemfiles/activerecord42.gemfile +7 -0
- data/test/gemfiles/activerecord50.gemfile +7 -0
- data/test/gemfiles/apartment.gemfile +8 -0
- data/test/gemfiles/cequel.gemfile +8 -0
- data/test/gemfiles/mongoid2.gemfile +7 -0
- data/test/gemfiles/mongoid3.gemfile +6 -0
- data/test/gemfiles/mongoid4.gemfile +7 -0
- data/test/gemfiles/mongoid5.gemfile +7 -0
- data/test/gemfiles/mongoid6.gemfile +8 -0
- data/test/gemfiles/nobrainer.gemfile +8 -0
- data/test/gemfiles/parallel_tests.gemfile +8 -0
- data/test/geo_shape_test.rb +172 -0
- data/test/highlight_test.rb +78 -0
- data/test/index_test.rb +153 -0
- data/test/inheritance_test.rb +83 -0
- data/test/marshal_test.rb +8 -0
- data/test/match_test.rb +276 -0
- data/test/misspellings_test.rb +56 -0
- data/test/model_test.rb +42 -0
- data/test/multi_search_test.rb +22 -0
- data/test/multi_tenancy_test.rb +22 -0
- data/test/order_test.rb +46 -0
- data/test/pagination_test.rb +53 -0
- data/test/partial_reindex_test.rb +58 -0
- data/test/query_test.rb +35 -0
- data/test/records_test.rb +10 -0
- data/test/reindex_test.rb +52 -0
- data/test/reindex_v2_job_test.rb +32 -0
- data/test/routing_test.rb +23 -0
- data/test/should_index_test.rb +32 -0
- data/test/similar_test.rb +28 -0
- data/test/sql_test.rb +198 -0
- data/test/suggest_test.rb +85 -0
- data/test/synonyms_test.rb +67 -0
- data/test/test_helper.rb +527 -0
- data/test/where_test.rb +223 -0
- metadata +250 -0
data/test/match_test.rb
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class MatchTest < Minitest::Test
|
4
|
+
# exact
|
5
|
+
|
6
|
+
def test_match
|
7
|
+
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
|
8
|
+
assert_search "milk", ["Milk", "Whole Milk", "Fat Free Milk"]
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_case
|
12
|
+
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
|
13
|
+
assert_search "MILK", ["Milk", "Whole Milk", "Fat Free Milk"]
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_cheese_space_in_index
|
17
|
+
store_names ["Pepper Jack Cheese Skewers"]
|
18
|
+
assert_search "pepperjack cheese skewers", ["Pepper Jack Cheese Skewers"]
|
19
|
+
end
|
20
|
+
|
21
|
+
# def test_cheese_space_in_query
|
22
|
+
# store_names ["Pepperjack Cheese Skewers"]
|
23
|
+
# assert_search "pepper jack cheese skewers", ["Pepperjack Cheese Skewers"]
|
24
|
+
# end
|
25
|
+
|
26
|
+
def test_middle_token
|
27
|
+
store_names ["Dish Washer Amazing Organic Soap"]
|
28
|
+
assert_search "dish soap", ["Dish Washer Amazing Organic Soap"]
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_middle_token_wine
|
32
|
+
store_names ["Beringer Wine Founders Estate Chardonnay"]
|
33
|
+
assert_search "beringer chardonnay", ["Beringer Wine Founders Estate Chardonnay"]
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_percent
|
37
|
+
# Note: "2% Milk" doesn't get matched in ES below 5.1.1
|
38
|
+
# This could be a bug since it has an edit distance of 1
|
39
|
+
store_names ["1% Milk", "Whole Milk"]
|
40
|
+
assert_search "1%", ["1% Milk"]
|
41
|
+
end
|
42
|
+
|
43
|
+
# ascii
|
44
|
+
|
45
|
+
def test_jalapenos
|
46
|
+
store_names ["Jalapeño"]
|
47
|
+
assert_search "jalapeno", ["Jalapeño"]
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_swedish
|
51
|
+
store_names ["ÅÄÖ"]
|
52
|
+
assert_search "aao", ["ÅÄÖ"]
|
53
|
+
end
|
54
|
+
|
55
|
+
# stemming
|
56
|
+
|
57
|
+
def test_stemming
|
58
|
+
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
|
59
|
+
assert_search "milks", ["Milk", "Whole Milk", "Fat Free Milk"]
|
60
|
+
end
|
61
|
+
|
62
|
+
# fuzzy
|
63
|
+
|
64
|
+
def test_misspelling_sriracha
|
65
|
+
store_names ["Sriracha"]
|
66
|
+
assert_search "siracha", ["Sriracha"]
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_misspelling_multiple
|
70
|
+
store_names ["Greek Yogurt", "Green Onions"]
|
71
|
+
assert_search "greed", ["Greek Yogurt", "Green Onions"]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_short_word
|
75
|
+
store_names ["Finn"]
|
76
|
+
assert_search "fin", ["Finn"]
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_edit_distance_two
|
80
|
+
store_names ["Bingo"]
|
81
|
+
assert_search "bin", []
|
82
|
+
assert_search "bingooo", []
|
83
|
+
assert_search "mango", []
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_edit_distance_one
|
87
|
+
store_names ["Bingo"]
|
88
|
+
assert_search "bing", ["Bingo"]
|
89
|
+
assert_search "bingoo", ["Bingo"]
|
90
|
+
assert_search "ringo", ["Bingo"]
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_edit_distance_long_word
|
94
|
+
store_names ["thisisareallylongword"]
|
95
|
+
assert_search "thisisareallylongwor", ["thisisareallylongword"] # missing letter
|
96
|
+
assert_search "thisisareelylongword", [] # edit distance = 2
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_misspelling_tabasco
|
100
|
+
store_names ["Tabasco"]
|
101
|
+
assert_search "tobasco", ["Tabasco"]
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_misspelling_zucchini
|
105
|
+
store_names ["Zucchini"]
|
106
|
+
assert_search "zuchini", ["Zucchini"]
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_misspelling_ziploc
|
110
|
+
store_names ["Ziploc"]
|
111
|
+
assert_search "zip lock", ["Ziploc"]
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_misspelling_zucchini_transposition
|
115
|
+
store_names ["zucchini"]
|
116
|
+
assert_search "zuccihni", ["zucchini"]
|
117
|
+
assert_search "zuccihni", [], misspellings: {transpositions: false}
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_misspelling_lasagna
|
121
|
+
store_names ["lasagna"]
|
122
|
+
assert_search "lasanga", ["lasagna"], misspellings: {transpositions: true}
|
123
|
+
assert_search "lasgana", ["lasagna"], misspellings: {transpositions: true}
|
124
|
+
assert_search "lasaang", [], misspellings: {transpositions: true} # triple transposition, shouldn't work
|
125
|
+
assert_search "lsagana", [], misspellings: {transpositions: true} # triple transposition, shouldn't work
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_misspelling_lasagna_pasta
|
129
|
+
store_names ["lasagna pasta"]
|
130
|
+
assert_search "lasanga", ["lasagna pasta"], misspellings: {transpositions: true}
|
131
|
+
assert_search "lasanga pasta", ["lasagna pasta"], misspellings: {transpositions: true}
|
132
|
+
assert_search "lasanga pasat", ["lasagna pasta"], misspellings: {transpositions: true} # both words misspelled with a transposition should still work
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_misspellings_word_start
|
136
|
+
store_names ["Sriracha"]
|
137
|
+
assert_search "siracha", ["Sriracha"], fields: [{name: :word_start}]
|
138
|
+
end
|
139
|
+
|
140
|
+
# spaces
|
141
|
+
|
142
|
+
def test_spaces_in_field
|
143
|
+
store_names ["Red Bull"]
|
144
|
+
assert_search "redbull", ["Red Bull"]
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_spaces_in_query
|
148
|
+
store_names ["Dishwasher"]
|
149
|
+
assert_search "dish washer", ["Dishwasher"]
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_spaces_three_words
|
153
|
+
store_names ["Dish Washer Soap", "Dish Washer"]
|
154
|
+
assert_search "dish washer soap", ["Dish Washer Soap"]
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_spaces_stemming
|
158
|
+
store_names ["Almond Milk"]
|
159
|
+
assert_search "almondmilks", ["Almond Milk"]
|
160
|
+
end
|
161
|
+
|
162
|
+
# butter
|
163
|
+
|
164
|
+
def test_exclude_butter
|
165
|
+
store_names ["Butter Tub", "Peanut Butter Tub"]
|
166
|
+
assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"]
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_exclude_butter_word_start
|
170
|
+
store_names ["Butter Tub", "Peanut Butter Tub"]
|
171
|
+
assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"], match: :word_start
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_exclude_butter_exact
|
175
|
+
store_names ["Butter Tub", "Peanut Butter Tub"]
|
176
|
+
assert_search "butter", [], exclude: ["peanut butter"], match: :exact
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_exclude_same_exact
|
180
|
+
store_names ["Butter Tub", "Peanut Butter Tub"]
|
181
|
+
assert_search "Butter Tub", [], exclude: ["Butter Tub"], match: :exact
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_exclude_egg_word_start
|
185
|
+
store_names ["eggs", "eggplant"]
|
186
|
+
assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_exclude_string
|
190
|
+
store_names ["Butter Tub", "Peanut Butter Tub"]
|
191
|
+
assert_search "butter", ["Butter Tub"], exclude: "peanut butter"
|
192
|
+
end
|
193
|
+
|
194
|
+
# other
|
195
|
+
|
196
|
+
def test_all
|
197
|
+
store_names ["Product A", "Product B"]
|
198
|
+
assert_search "*", ["Product A", "Product B"]
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_no_arguments
|
202
|
+
store_names []
|
203
|
+
assert_equal [], Product.search.to_a
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_no_term
|
207
|
+
store_names ["Product A"]
|
208
|
+
assert_equal ["Product A"], Product.search(where: {name: "Product A"}).map(&:name)
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_to_be_or_not_to_be
|
212
|
+
store_names ["to be or not to be"]
|
213
|
+
assert_search "to be", ["to be or not to be"]
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_apostrophe
|
217
|
+
store_names ["Ben and Jerry's"]
|
218
|
+
assert_search "ben and jerrys", ["Ben and Jerry's"]
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_apostrophe_search
|
222
|
+
store_names ["Ben and Jerrys"]
|
223
|
+
assert_search "ben and jerry's", ["Ben and Jerrys"]
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_ampersand_index
|
227
|
+
store_names ["Ben & Jerry's"]
|
228
|
+
assert_search "ben and jerrys", ["Ben & Jerry's"]
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_ampersand_search
|
232
|
+
store_names ["Ben and Jerry's"]
|
233
|
+
assert_search "ben & jerrys", ["Ben and Jerry's"]
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_phrase
|
237
|
+
store_names ["Fresh Honey", "Honey Fresh"]
|
238
|
+
assert_search "fresh honey", ["Fresh Honey"], match: :phrase
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_phrase_order
|
242
|
+
store_names ["Wheat Bread", "Whole Wheat Bread"]
|
243
|
+
assert_order "wheat bread", ["Wheat Bread", "Whole Wheat Bread"], match: :phrase
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_dynamic_fields
|
247
|
+
store_names ["Red Bull"], Speaker
|
248
|
+
assert_search "redbull", ["Red Bull"], {fields: [:name]}, Speaker
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_unsearchable
|
252
|
+
store [
|
253
|
+
{name: "Unsearchable", description: "Almond"}
|
254
|
+
]
|
255
|
+
assert_search "almond", []
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_unsearchable_where
|
259
|
+
store [
|
260
|
+
{name: "Unsearchable", description: "Almond"}
|
261
|
+
]
|
262
|
+
assert_search "*", ["Unsearchable"], where: {description: "Almond"}
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_emoji
|
266
|
+
skip unless defined?(EmojiParser)
|
267
|
+
store_names ["Banana"]
|
268
|
+
assert_search "🍌", ["Banana"], emoji: true
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_emoji_multiple
|
272
|
+
skip unless defined?(EmojiParser)
|
273
|
+
store_names ["Ice Cream Cake"]
|
274
|
+
assert_search "🍨🍰", ["Ice Cream Cake"], emoji: true
|
275
|
+
end
|
276
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class MisspellingsTest < Minitest::Test
|
4
|
+
def test_misspellings
|
5
|
+
store_names ["abc", "abd", "aee"]
|
6
|
+
assert_search "abc", ["abc"], misspellings: false
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_misspellings_distance
|
10
|
+
store_names ["abbb", "aabb"]
|
11
|
+
assert_search "aaaa", ["aabb"], misspellings: {distance: 2}
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_misspellings_prefix_length
|
15
|
+
store_names ["ap", "api", "apt", "any", "nap", "ah", "ahi"]
|
16
|
+
assert_search "ap", ["ap"], misspellings: {prefix_length: 2}
|
17
|
+
assert_search "api", ["ap", "api", "apt"], misspellings: {prefix_length: 2}
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_misspellings_prefix_length_operator
|
21
|
+
store_names ["ap", "api", "apt", "any", "nap", "ah", "aha"]
|
22
|
+
assert_search "ap ah", ["ap", "ah"], operator: "or", misspellings: {prefix_length: 2}
|
23
|
+
assert_search "api ahi", ["ap", "api", "apt", "ah", "aha"], operator: "or", misspellings: {prefix_length: 2}
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_misspellings_fields_operator
|
27
|
+
store [
|
28
|
+
{name: "red", color: "red"},
|
29
|
+
{name: "blue", color: "blue"},
|
30
|
+
{name: "cyan", color: "blue green"},
|
31
|
+
{name: "magenta", color: "red blue"},
|
32
|
+
{name: "green", color: "green"}
|
33
|
+
]
|
34
|
+
assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"], misspellings: false
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_misspellings_below_unmet
|
38
|
+
store_names ["abc", "abd", "aee"]
|
39
|
+
assert_search "abc", ["abc", "abd"], misspellings: {below: 2}
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_misspellings_below_unmet_result
|
43
|
+
store_names ["abc", "abd", "aee"]
|
44
|
+
assert Product.search("abc", misspellings: {below: 2}).misspellings?
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_misspellings_below_met
|
48
|
+
store_names ["abc", "abd", "aee"]
|
49
|
+
assert_search "abc", ["abc"], misspellings: {below: 1}
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_misspellings_below_met_result
|
53
|
+
store_names ["abc", "abd", "aee"]
|
54
|
+
assert !Product.search("abc", misspellings: {below: 1}).misspellings?
|
55
|
+
end
|
56
|
+
end
|
data/test/model_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class ModelTest < Minitest::Test
|
4
|
+
def test_disable_callbacks_model
|
5
|
+
store_names ["product a"]
|
6
|
+
|
7
|
+
Product.disable_search_callbacks
|
8
|
+
assert !Product.search_callbacks?
|
9
|
+
|
10
|
+
store_names ["product b"]
|
11
|
+
assert_search "product", ["product a"]
|
12
|
+
|
13
|
+
Product.enable_search_callbacks
|
14
|
+
Product.reindex
|
15
|
+
|
16
|
+
assert_search "product", ["product a", "product b"]
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_disable_callbacks_global
|
20
|
+
# make sure callbacks default to on
|
21
|
+
assert Searchkick.callbacks?
|
22
|
+
|
23
|
+
store_names ["product a"]
|
24
|
+
|
25
|
+
Searchkick.disable_callbacks
|
26
|
+
assert !Searchkick.callbacks?
|
27
|
+
|
28
|
+
store_names ["product b"]
|
29
|
+
assert_search "product", ["product a"]
|
30
|
+
|
31
|
+
Searchkick.enable_callbacks
|
32
|
+
Product.reindex
|
33
|
+
|
34
|
+
assert_search "product", ["product a", "product b"]
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_multiple_models
|
38
|
+
store_names ["Product A"]
|
39
|
+
store_names ["Product B"], Speaker
|
40
|
+
assert_equal Product.all.to_a + Speaker.all.to_a, Searchkick.search("product", index_name: [Product, Speaker], fields: [:name], order: "name").to_a
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class MultiSearchTest < Minitest::Test
|
4
|
+
def test_basic
|
5
|
+
store_names ["Product A"]
|
6
|
+
store_names ["Store A"], Store
|
7
|
+
products = Product.search("*", execute: false)
|
8
|
+
stores = Store.search("*", execute: false)
|
9
|
+
Searchkick.multi_search([products, stores])
|
10
|
+
assert_equal ["Product A"], products.map(&:name)
|
11
|
+
assert_equal ["Store A"], stores.map(&:name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_error
|
15
|
+
store_names ["Product A"]
|
16
|
+
products = Product.search("*", execute: false)
|
17
|
+
stores = Store.search("*", order: [:bad_field], execute: false)
|
18
|
+
Searchkick.multi_search([products, stores])
|
19
|
+
assert !products.error
|
20
|
+
assert stores.error
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class MultiTenancyTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
skip unless defined?(Apartment)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_basic
|
9
|
+
Apartment::Tenant.switch!("tenant1")
|
10
|
+
store_names ["Product A"], Tenant
|
11
|
+
Apartment::Tenant.switch!("tenant2")
|
12
|
+
store_names ["Product B"], Tenant
|
13
|
+
Apartment::Tenant.switch!("tenant1")
|
14
|
+
assert_search "product", ["Product A"], {load: false}, Tenant
|
15
|
+
Apartment::Tenant.switch!("tenant2")
|
16
|
+
assert_search "product", ["Product B"], {load: false}, Tenant
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
Apartment::Tenant.reset if defined?(Apartment)
|
21
|
+
end
|
22
|
+
end
|
data/test/order_test.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class OrderTest < Minitest::Test
|
4
|
+
def test_order_hash
|
5
|
+
store_names ["Product A", "Product B", "Product C", "Product D"]
|
6
|
+
assert_order "product", ["Product D", "Product C", "Product B", "Product A"], order: {name: :desc}
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_order_string
|
10
|
+
store_names ["Product A", "Product B", "Product C", "Product D"]
|
11
|
+
assert_order "product", ["Product A", "Product B", "Product C", "Product D"], order: "name"
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_order_id
|
15
|
+
skip if cequel?
|
16
|
+
|
17
|
+
store_names ["Product A", "Product B"]
|
18
|
+
product_a = Product.where(name: "Product A").first
|
19
|
+
product_b = Product.where(name: "Product B").first
|
20
|
+
assert_order "product", [product_a, product_b].sort_by(&:id).map(&:name), order: {id: :asc}
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_order_multiple
|
24
|
+
store [
|
25
|
+
{name: "Product A", color: "blue", store_id: 1},
|
26
|
+
{name: "Product B", color: "red", store_id: 3},
|
27
|
+
{name: "Product C", color: "red", store_id: 2}
|
28
|
+
]
|
29
|
+
assert_order "product", ["Product A", "Product B", "Product C"], order: {color: :asc, store_id: :desc}
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_order_ignore_unmapped
|
33
|
+
skip unless elasticsearch_below50?
|
34
|
+
assert_order "product", [], order: {not_mapped: {ignore_unmapped: true}}, conversions: false
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_order_unmapped_type
|
38
|
+
skip if elasticsearch_below50?
|
39
|
+
assert_order "product", [], order: {not_mapped: {unmapped_type: "long"}}, conversions: false
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_order_array
|
43
|
+
store [{name: "San Francisco", latitude: 37.7833, longitude: -122.4167}]
|
44
|
+
assert_order "francisco", ["San Francisco"], order: [{_geo_distance: {location: "0,0"}}]
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class PaginationTest < Minitest::Test
|
4
|
+
def test_limit
|
5
|
+
store_names ["Product A", "Product B", "Product C", "Product D"]
|
6
|
+
assert_order "product", ["Product A", "Product B"], order: {name: :asc}, limit: 2
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_no_limit
|
10
|
+
names = 20.times.map { |i| "Product #{i}" }
|
11
|
+
store_names names
|
12
|
+
assert_search "product", names
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_offset
|
16
|
+
store_names ["Product A", "Product B", "Product C", "Product D"]
|
17
|
+
assert_order "product", ["Product C", "Product D"], order: {name: :asc}, offset: 2
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_pagination
|
21
|
+
store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"]
|
22
|
+
products = Product.search("product", order: {name: :asc}, page: 2, per_page: 2, padding: 1)
|
23
|
+
assert_equal ["Product D", "Product E"], products.map(&:name)
|
24
|
+
assert_equal "product", products.entry_name
|
25
|
+
assert_equal 2, products.current_page
|
26
|
+
assert_equal 1, products.padding
|
27
|
+
assert_equal 2, products.per_page
|
28
|
+
assert_equal 2, products.size
|
29
|
+
assert_equal 2, products.length
|
30
|
+
assert_equal 3, products.total_pages
|
31
|
+
assert_equal 6, products.total_count
|
32
|
+
assert_equal 6, products.total_entries
|
33
|
+
assert_equal 2, products.limit_value
|
34
|
+
assert_equal 3, products.offset_value
|
35
|
+
assert_equal 3, products.offset
|
36
|
+
assert_equal 3, products.next_page
|
37
|
+
assert_equal 1, products.previous_page
|
38
|
+
assert_equal 1, products.prev_page
|
39
|
+
assert !products.first_page?
|
40
|
+
assert !products.last_page?
|
41
|
+
assert !products.empty?
|
42
|
+
assert !products.out_of_range?
|
43
|
+
assert products.any?
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_pagination_nil_page
|
47
|
+
store_names ["Product A", "Product B", "Product C", "Product D", "Product E"]
|
48
|
+
products = Product.search("product", order: {name: :asc}, page: nil, per_page: 2)
|
49
|
+
assert_equal ["Product A", "Product B"], products.map(&:name)
|
50
|
+
assert_equal 1, products.current_page
|
51
|
+
assert products.first_page?
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class PartialReindexTest < Minitest::Test
|
4
|
+
def test_class_method
|
5
|
+
store [{name: "Hi", color: "Blue"}]
|
6
|
+
|
7
|
+
# normal search
|
8
|
+
assert_search "hi", ["Hi"], fields: [:name], load: false
|
9
|
+
assert_search "blue", ["Hi"], fields: [:color], load: false
|
10
|
+
|
11
|
+
# update
|
12
|
+
product = Product.first
|
13
|
+
product.name = "Bye"
|
14
|
+
product.color = "Red"
|
15
|
+
Searchkick.callbacks(false) do
|
16
|
+
product.save!
|
17
|
+
end
|
18
|
+
Product.searchkick_index.refresh
|
19
|
+
|
20
|
+
# index not updated
|
21
|
+
assert_search "hi", ["Hi"], fields: [:name], load: false
|
22
|
+
assert_search "blue", ["Hi"], fields: [:color], load: false
|
23
|
+
|
24
|
+
# partial reindex
|
25
|
+
Product.reindex(:search_name)
|
26
|
+
|
27
|
+
# name updated, but not color
|
28
|
+
assert_search "bye", ["Bye"], fields: [:name], load: false
|
29
|
+
assert_search "blue", ["Bye"], fields: [:color], load: false
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_instance_method
|
33
|
+
store [{name: "Hi", color: "Blue"}]
|
34
|
+
|
35
|
+
# normal search
|
36
|
+
assert_search "hi", ["Hi"], fields: [:name], load: false
|
37
|
+
assert_search "blue", ["Hi"], fields: [:color], load: false
|
38
|
+
|
39
|
+
# update
|
40
|
+
product = Product.first
|
41
|
+
product.name = "Bye"
|
42
|
+
product.color = "Red"
|
43
|
+
Searchkick.callbacks(false) do
|
44
|
+
product.save!
|
45
|
+
end
|
46
|
+
Product.searchkick_index.refresh
|
47
|
+
|
48
|
+
# index not updated
|
49
|
+
assert_search "hi", ["Hi"], fields: [:name], load: false
|
50
|
+
assert_search "blue", ["Hi"], fields: [:color], load: false
|
51
|
+
|
52
|
+
product.reindex(:search_name, refresh: true)
|
53
|
+
|
54
|
+
# name updated, but not color
|
55
|
+
assert_search "bye", ["Bye"], fields: [:name], load: false
|
56
|
+
assert_search "blue", ["Bye"], fields: [:color], load: false
|
57
|
+
end
|
58
|
+
end
|
data/test/query_test.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class QueryTest < Minitest::Test
|
4
|
+
def test_basic
|
5
|
+
store_names ["Milk", "Apple"]
|
6
|
+
query = Product.search("milk", execute: false)
|
7
|
+
# query.body = {query: {match_all: {}}}
|
8
|
+
# query.body = {query: {match: {name: "Apple"}}}
|
9
|
+
query.body[:query] = {match_all: {}}
|
10
|
+
assert_equal ["Apple", "Milk"], query.map(&:name).sort
|
11
|
+
assert_equal ["Apple", "Milk"], query.execute.map(&:name).sort
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_with_effective_min_score
|
15
|
+
store_names ["Milk", "Milk2"]
|
16
|
+
assert_search "milk", ["Milk"], body_options: {min_score: 1}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_with_uneffective_min_score
|
20
|
+
store_names ["Milk", "Milk2"]
|
21
|
+
assert_search "milk", ["Milk", "Milk2"], body_options: {min_score: 0.0001}
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_default_timeout
|
25
|
+
assert_equal "6s", Product.search("*", execute: false).body[:timeout]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_timeout_override
|
29
|
+
assert_equal "1s", Product.search("*", body_options: {timeout: "1s"}, execute: false).body[:timeout]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_request_params
|
33
|
+
assert_equal "dfs_query_then_fetch", Product.search("*", request_params: {search_type: "dfs_query_then_fetch"}, execute: false).params[:search_type]
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class ReindexTest < Minitest::Test
|
4
|
+
def test_scoped
|
5
|
+
skip if nobrainer? || cequel?
|
6
|
+
|
7
|
+
store_names ["Product A"]
|
8
|
+
Searchkick.callbacks(false) do
|
9
|
+
store_names ["Product B", "Product C"]
|
10
|
+
end
|
11
|
+
Product.where(name: "Product B").reindex(refresh: true)
|
12
|
+
assert_search "product", ["Product A", "Product B"]
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_associations
|
16
|
+
skip if nobrainer? || cequel?
|
17
|
+
|
18
|
+
store_names ["Product A"]
|
19
|
+
store = Store.create!(name: "Test")
|
20
|
+
Product.create!(name: "Product B", store_id: store.id)
|
21
|
+
store.products.reindex(refresh: true)
|
22
|
+
assert_search "product", ["Product A", "Product B"]
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_async
|
26
|
+
skip if !defined?(ActiveJob)
|
27
|
+
|
28
|
+
Searchkick.callbacks(false) do
|
29
|
+
store_names ["Product A"]
|
30
|
+
end
|
31
|
+
reindex = Product.reindex(async: true)
|
32
|
+
assert_search "product", [], conversions: false
|
33
|
+
|
34
|
+
index = Searchkick::Index.new(reindex[:index_name])
|
35
|
+
index.refresh
|
36
|
+
assert_equal 1, index.total_docs
|
37
|
+
|
38
|
+
Product.searchkick_index.promote(reindex[:index_name])
|
39
|
+
assert_search "product", ["Product A"]
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_refresh_interval
|
43
|
+
reindex = Product.reindex(refresh_interval: "30s", async: true, import: false)
|
44
|
+
index = Searchkick::Index.new(reindex[:index_name])
|
45
|
+
assert_nil Product.search_index.refresh_interval
|
46
|
+
assert_equal "30s", index.refresh_interval
|
47
|
+
|
48
|
+
Product.search_index.promote(index.name, update_refresh_interval: true)
|
49
|
+
assert_equal "1s", index.refresh_interval
|
50
|
+
assert_equal "1s", Product.search_index.refresh_interval
|
51
|
+
end
|
52
|
+
end
|