match_test.rb
9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require_relative "test_helper"
class MatchTest < Minitest::Test
# exact
def test_match
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
assert_search "milk", ["Milk", "Whole Milk", "Fat Free Milk"]
end
def test_case
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
assert_search "MILK", ["Milk", "Whole Milk", "Fat Free Milk"]
end
def test_cheese_space_in_index
store_names ["Pepper Jack Cheese Skewers"]
assert_search "pepperjack cheese skewers", ["Pepper Jack Cheese Skewers"]
end
def test_operator
store_names ["fresh", "honey"]
assert_search "fresh honey", ["fresh", "honey"], {operator: "or"}
assert_search "fresh honey", [], {operator: "and"}
assert_search "fresh honey", ["fresh", "honey"], {operator: :or}
end
# def test_cheese_space_in_query
# store_names ["Pepperjack Cheese Skewers"]
# assert_search "pepper jack cheese skewers", ["Pepperjack Cheese Skewers"]
# end
def test_middle_token
store_names ["Dish Washer Amazing Organic Soap"]
assert_search "dish soap", ["Dish Washer Amazing Organic Soap"]
end
def test_middle_token_wine
store_names ["Beringer Wine Founders Estate Chardonnay"]
assert_search "beringer chardonnay", ["Beringer Wine Founders Estate Chardonnay"]
end
def test_percent
# Note: "2% Milk" doesn't get matched in ES below 5.1.1
# This could be a bug since it has an edit distance of 1
store_names ["1% Milk", "Whole Milk"]
assert_search "1%", ["1% Milk"]
end
# ascii
def test_jalapenos
store_names ["Jalapeño"]
assert_search "jalapeno", ["Jalapeño"]
end
def test_swedish
store_names ["ÅÄÖ"]
assert_search "aao", ["ÅÄÖ"]
end
# stemming
def test_stemming
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
assert_search "milks", ["Milk", "Whole Milk", "Fat Free Milk"]
end
# fuzzy
def test_misspelling_sriracha
store_names ["Sriracha"]
assert_search "siracha", ["Sriracha"]
end
def test_misspelling_multiple
store_names ["Greek Yogurt", "Green Onions"]
assert_search "greed", ["Greek Yogurt", "Green Onions"]
end
def test_short_word
store_names ["Finn"]
assert_search "fin", ["Finn"]
end
def test_edit_distance_two
store_names ["Bingo"]
assert_search "bin", []
assert_search "bingooo", []
assert_search "mango", []
end
def test_edit_distance_one
store_names ["Bingo"]
assert_search "bing", ["Bingo"]
assert_search "bingoo", ["Bingo"]
assert_search "ringo", ["Bingo"]
end
def test_edit_distance_long_word
store_names ["thisisareallylongword"]
assert_search "thisisareallylongwor", ["thisisareallylongword"] # missing letter
assert_search "thisisareelylongword", [] # edit distance = 2
end
def test_misspelling_tabasco
store_names ["Tabasco"]
assert_search "tobasco", ["Tabasco"]
end
def test_misspelling_zucchini
store_names ["Zucchini"]
assert_search "zuchini", ["Zucchini"]
end
def test_misspelling_ziploc
store_names ["Ziploc"]
assert_search "zip lock", ["Ziploc"]
end
def test_misspelling_zucchini_transposition
store_names ["zucchini"]
assert_search "zuccihni", ["zucchini"]
# need to specify field
# as transposition option isn't supported for multi_match queries
# until Elasticsearch 6.1
assert_search "zuccihni", [], misspellings: {transpositions: false}, fields: [:name]
end
def test_misspelling_lasagna
store_names ["lasagna"]
assert_search "lasanga", ["lasagna"], misspellings: {transpositions: true}
assert_search "lasgana", ["lasagna"], misspellings: {transpositions: true}
assert_search "lasaang", [], misspellings: {transpositions: true} # triple transposition, shouldn't work
assert_search "lsagana", [], misspellings: {transpositions: true} # triple transposition, shouldn't work
end
def test_misspelling_lasagna_pasta
store_names ["lasagna pasta"]
assert_search "lasanga", ["lasagna pasta"], misspellings: {transpositions: true}
assert_search "lasanga pasta", ["lasagna pasta"], misspellings: {transpositions: true}
assert_search "lasanga pasat", ["lasagna pasta"], misspellings: {transpositions: true} # both words misspelled with a transposition should still work
end
def test_misspellings_word_start
store_names ["Sriracha"]
assert_search "siracha", ["Sriracha"], fields: [{name: :word_start}]
end
# spaces
def test_spaces_in_field
store_names ["Red Bull"]
assert_search "redbull", ["Red Bull"]
end
def test_spaces_in_query
store_names ["Dishwasher"]
assert_search "dish washer", ["Dishwasher"]
end
def test_spaces_three_words
store_names ["Dish Washer Soap", "Dish Washer"]
assert_search "dish washer soap", ["Dish Washer Soap"]
end
def test_spaces_stemming
store_names ["Almond Milk"]
assert_search "almondmilks", ["Almond Milk"]
end
# butter
def test_exclude_butter
store_names ["Butter Tub", "Peanut Butter Tub"]
assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"]
assert_search_relation ["Butter Tub"], Product.search("butter").exclude("peanut butter")
assert_search_relation ["Butter Tub"], Product.search("butter").exclude(["peanut butter"])
end
def test_exclude_butter_word_start
store_names ["Butter Tub", "Peanut Butter Tub"]
assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"], match: :word_start
end
def test_exclude_butter_exact
store_names ["Butter Tub", "Peanut Butter Tub"]
assert_search "butter", [], exclude: ["peanut butter"], fields: [{name: :exact}]
end
def test_exclude_same_exact
store_names ["Butter Tub", "Peanut Butter Tub"]
assert_search "Butter Tub", ["Butter Tub"], exclude: ["Peanut Butter Tub"], fields: [{name: :exact}]
end
def test_exclude_egg_word_start
store_names ["eggs", "eggplant"]
assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start
end
def test_exclude_string
store_names ["Butter Tub", "Peanut Butter Tub"]
assert_search "butter", ["Butter Tub"], exclude: "peanut butter"
end
def test_exclude_match_all
store_names ["Butter"]
assert_search "*", [], exclude: "butter"
end
def test_exclude_match_all_fields
store_names ["Butter"]
assert_search "*", [], fields: [:name], exclude: "butter"
assert_search "*", ["Butter"], fields: [:color], exclude: "butter"
end
# other
def test_all
store_names ["Product A", "Product B"]
assert_search "*", ["Product A", "Product B"]
end
def test_no_arguments
store_names []
assert_equal [], Product.search.to_a
end
def test_no_term
store_names ["Product A"]
assert_equal ["Product A"], Product.search(where: {name: "Product A"}).map(&:name)
end
def test_to_be_or_not_to_be
store_names ["to be or not to be"]
assert_search "to be", ["to be or not to be"]
end
def test_apostrophe
store_names ["Ben and Jerry's"]
assert_search "ben and jerrys", ["Ben and Jerry's"]
end
def test_apostrophe_search
store_names ["Ben and Jerrys"]
assert_search "ben and jerry's", ["Ben and Jerrys"]
end
def test_ampersand_index
store_names ["Ben & Jerry's"]
assert_search "ben and jerrys", ["Ben & Jerry's"]
end
def test_ampersand_search
store_names ["Ben and Jerry's"]
assert_search "ben & jerrys", ["Ben and Jerry's"]
end
def test_phrase
store_names ["Fresh Honey", "Honey Fresh"]
assert_search "fresh honey", ["Fresh Honey"], match: :phrase
end
def test_phrase_again
store_names ["Social entrepreneurs don't have it easy raising capital"]
assert_search "social entrepreneurs don't have it easy raising capital", ["Social entrepreneurs don't have it easy raising capital"], match: :phrase
end
def test_phrase_order
store_names ["Wheat Bread", "Whole Wheat Bread"]
assert_order "wheat bread", ["Wheat Bread", "Whole Wheat Bread"], match: :phrase, fields: [:name]
end
def test_dynamic_fields
store_names ["Red Bull"], Speaker
assert_search "redbull", ["Red Bull"], {fields: [:name]}, Speaker
end
def test_unsearchable
skip
store [
{name: "Unsearchable", description: "Almond"}
]
assert_search "almond", []
end
def test_unsearchable_where
store [
{name: "Unsearchable", description: "Almond"}
]
assert_search "*", ["Unsearchable"], where: {description: "Almond"}
end
def test_emoji
skip unless defined?(EmojiParser)
store_names ["Banana"]
assert_search "🍌", ["Banana"], emoji: true
assert_search_relation ["Banana"], Product.search("🍌").emoji
end
def test_emoji_multiple
skip unless defined?(EmojiParser)
store_names ["Ice Cream Cake"]
assert_search "🍨🍰", ["Ice Cream Cake"], emoji: true
assert_search_relation ["Ice Cream Cake"], Product.search("🍨🍰").emoji
end
# TODO find better place
def test_search_relation
skip unless defined?(ActiveRecord)
assert_nil Product.current_scope
_, stderr = capture_io { Product.search("*") }
assert_equal "", stderr
assert Product.where(name: nil).current_scope
_, stderr = capture_io { Product.where(name: nil).search("*") }
assert_match "WARNING", stderr
end
end