-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathtest_index_search_meilisearch.py
More file actions
455 lines (379 loc) · 17.3 KB
/
Copy pathtest_index_search_meilisearch.py
File metadata and controls
455 lines (379 loc) · 17.3 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# pylint: disable=invalid-name
import pytest
def test_basic_search(index_with_documents):
"""Tests search with a simple query."""
response = index_with_documents().search("How to Train Your Dragon")
assert isinstance(response, dict)
assert response["hits"][0]["id"] == "166428"
assert response["estimatedTotalHits"] is not None
assert "hitsPerPage" is not response
def test_basic_search_with_empty_params(index_with_documents):
"""Tests search with a simple query and empty params."""
response = index_with_documents().search("How to Train Your Dragon", {})
assert isinstance(response, dict)
assert response["hits"][0]["id"] == "166428"
assert "_formatted" not in response["hits"][0]
def test_basic_search_with_empty_query(index_with_documents):
"""Tests search with an empty query and empty params."""
response = index_with_documents().search("")
assert isinstance(response, dict)
assert len(response["hits"]) == 20
assert response["query"] == ""
def test_basic_search_with_no_query(index_with_documents):
"""Tests search with no query [None] and empty params."""
response = index_with_documents().search(None, {})
assert isinstance(response, dict)
assert len(response["hits"]) == 20
def test_custom_search(index_with_documents):
"""Tests search with a simple query and a custom parameter (attributesToHighlight)."""
response = index_with_documents().search("Dragon", {"attributesToHighlight": ["title"]})
assert isinstance(response, dict)
assert response["hits"][0]["id"] == "166428"
assert "_formatted" in response["hits"][0]
assert "dragon" in response["hits"][0]["_formatted"]["title"].lower()
def test_custom_search_with_empty_query(index_with_documents):
"""Tests search with an empty query and custom parameter (attributesToHighlight)."""
response = index_with_documents().search("", {"attributesToHighlight": ["title"]})
assert isinstance(response, dict)
assert len(response["hits"]) == 20
assert response["query"] == ""
def test_custom_search_with_no_query(index_with_documents):
"""Tests search with no query [None] and a custom parameter (limit)."""
response = index_with_documents().search(None, {"limit": 5})
assert isinstance(response, dict)
assert len(response["hits"]) == 5
def test_custom_search_params_with_wildcard(index_with_documents):
"""Tests search with '*' in query params."""
response = index_with_documents().search(
"a",
{
"limit": 5,
"attributesToHighlight": ["*"],
"attributesToRetrieve": ["*"],
"attributesToCrop": ["*"],
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 5
assert "_formatted" in response["hits"][0]
assert "title" in response["hits"][0]["_formatted"]
def test_custom_search_params_with_simple_string(index_with_documents):
"""Tests search with a list of one string in query params."""
response = index_with_documents().search(
"a",
{
"limit": 5,
"attributesToHighlight": ["title"],
"attributesToRetrieve": ["title"],
"attributesToCrop": ["title"],
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 5
assert "_formatted" in response["hits"][0]
assert "title" in response["hits"][0]["_formatted"]
assert not "release_date" in response["hits"][0]["_formatted"]
def test_custom_search_params_with_string_list(index_with_documents):
"""Tests search with string list in query params."""
response = index_with_documents().search(
"a",
{
"limit": 5,
"attributesToRetrieve": ["title", "overview"],
"attributesToHighlight": ["title"],
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 5
assert "title" in response["hits"][0]
assert "overview" in response["hits"][0]
assert not "release_date" in response["hits"][0]
assert "title" in response["hits"][0]["_formatted"]
assert "overview" in response["hits"][0]["_formatted"]
def test_custom_search_params_with_crop_marker(index_with_documents):
"""Tests search with a list of one string in query params."""
response = index_with_documents().search(
"dragon",
{
"limit": 1,
"attributesToCrop": ["overview"],
"cropLength": 10,
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "_formatted" in response["hits"][0]
assert "overview" in response["hits"][0]["_formatted"]
assert response["hits"][0]["_formatted"]["overview"].count(" ") < 10
assert response["hits"][0]["_formatted"]["overview"].count("…") == 2
def test_custom_search_params_with_customized_crop_marker(index_with_documents):
"""Tests search with a list of one string in query params."""
response = index_with_documents().search(
"dragon",
{
"limit": 1,
"attributesToCrop": ["overview"],
"cropLength": 10,
"cropMarker": "(ꈍᴗꈍ)",
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "_formatted" in response["hits"][0]
assert "overview" in response["hits"][0]["_formatted"]
assert response["hits"][0]["_formatted"]["overview"].count("(ꈍᴗꈍ)") == 2
def test_custom_search_params_with_highlight_tag(index_with_documents):
"""Tests search with a list of one string in query params."""
response = index_with_documents().search(
"dragon",
{
"limit": 1,
"attributesToHighlight": ["*"],
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "_formatted" in response["hits"][0]
assert "title" in response["hits"][0]["_formatted"]
assert (
response["hits"][0]["_formatted"]["title"]
== "How to Train Your <em>Dragon</em>: The Hidden World"
)
def test_custom_search_params_with_customized_highlight_tag(index_with_documents):
"""Tests search with a list of one string in query params."""
response = index_with_documents().search(
"dragon",
{
"limit": 1,
"attributesToHighlight": ["*"],
"highlightPreTag": "(⊃。•́‿•̀。)⊃ ",
"highlightPostTag": " ⊂(´• ω •`⊂)",
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "_formatted" in response["hits"][0]
assert "title" in response["hits"][0]["_formatted"]
assert (
response["hits"][0]["_formatted"]["title"]
== "How to Train Your (⊃。•́‿•̀。)⊃ Dragon ⊂(´• ω •`⊂): The Hidden World"
)
def test_custom_search_params_with_facets_distribution(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"facets": ["genre"]})
assert isinstance(response, dict)
assert len(response["hits"]) == 12
assert "facetDistribution" in response
assert "genre" in response["facetDistribution"]
assert response["facetDistribution"]["genre"]["cartoon"] == 1
assert response["facetDistribution"]["genre"]["action"] == 3
assert response["facetDistribution"]["genre"]["fantasy"] == 1
def test_custom_search_params_with_filter_string(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"filter": "genre = action"})
assert isinstance(response, dict)
assert len(response["hits"]) == 3
assert "facetDistribution" not in response
def test_custom_search_params_with_filter_string_with_space(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre"])
index.wait_for_task(update.task_uid)
response = index.search("galaxy", {"filter": "genre = 'sci fi'"})
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "facetDistribution" not in response
def test_custom_search_params_with_multiple_filter_string_with_space(
index_with_documents,
):
index = index_with_documents()
update = index.update_filterable_attributes(["genre", "release_date"])
index.wait_for_task(update.task_uid)
response = index.search("galaxy", {"filter": "genre = 'sci fi' AND release_date < 1550000000"})
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "facetDistribution" not in response
def test_custom_search_params_with_array_filter_with_space(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre", "release_date"])
index.wait_for_task(update.task_uid)
response = index.search("galaxy", {"filter": ["genre = 'sci fi'", "release_date < 1550000000"]})
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "facetDistribution" not in response
def test_custom_search_params_with_mutilple_filter_string(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre", "release_date"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"filter": "genre = action AND release_date < 1550000000"})
assert isinstance(response, dict)
assert len(response["hits"]) == 2
assert "facetDistribution" not in response
assert response["hits"][0]["title"] == "Avengers: Infinity War"
def test_custom_search_params_with_filter(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"filter": [["genre = action"]]})
assert isinstance(response, dict)
assert len(response["hits"]) == 3
assert "facetDistribution" not in response
def test_custom_search_params_with_multiple_filter(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre"])
index.wait_for_task(update.task_uid)
response = index.search(
"world", {"filter": ["genre = action", ["genre = action", "genre = action"]]}
)
assert isinstance(response, dict)
assert len(response["hits"]) == 3
assert "facetDistribution" not in response
def test_custom_search_params_with_many_params(index_with_documents):
index = index_with_documents()
update = index.update_filterable_attributes(["genre"])
index.wait_for_task(update.task_uid)
response = index.search(
"world",
{"filter": [["genre = action"]], "attributesToRetrieve": ["title", "poster"]},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 3
assert "facetDistribution" not in response
assert "title" in response["hits"][0]
assert "poster" in response["hits"][0]
assert "overview" not in response["hits"][0]
assert "release_date" not in response["hits"][0]
assert response["hits"][0]["title"] == "Avengers: Infinity War"
def test_custom_search_params_with_sort_string(index_with_documents):
index = index_with_documents()
response = index.update_ranking_rules(
["words", "typo", "sort", "proximity", "attribute", "exactness"]
)
index.wait_for_task(response.task_uid)
update = index.update_sortable_attributes(["title"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"sort": ["title:asc"]})
assert isinstance(response, dict)
assert len(response["hits"]) == 12
assert "facetDistribution" not in response
assert response["hits"][0]["title"] == "Alita: Battle Angel"
assert response["hits"][1]["title"] == "Aquaman"
def test_custom_search_params_with_sort_int(index_with_documents):
index = index_with_documents()
response = index.update_ranking_rules(
["words", "typo", "sort", "proximity", "attribute", "exactness"]
)
index.wait_for_task(response.task_uid)
update = index.update_sortable_attributes(["release_date"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"sort": ["release_date:asc"]})
assert isinstance(response, dict)
assert len(response["hits"]) == 12
assert "facetDistribution" not in response
assert response["hits"][0]["title"] == "Avengers: Infinity War"
assert response["hits"][1]["title"] == "Redcon-1"
def test_custom_search_params_with_multiple_sort(index_with_documents):
index = index_with_documents()
response = index.update_ranking_rules(
["words", "typo", "sort", "proximity", "attribute", "exactness"]
)
index.wait_for_task(response.task_uid)
update = index.update_sortable_attributes(["title", "release_date"])
index.wait_for_task(update.task_uid)
response = index.search("world", {"sort": ["title:asc", "release_date:asc"]})
assert isinstance(response, dict)
assert len(response["hits"]) == 12
assert "facetDistribution" not in response
assert response["hits"][0]["title"] == "Alita: Battle Angel"
assert response["hits"][1]["title"] == "Aquaman"
def test_phrase_search(index_with_documents):
response = index_with_documents().search('coco "dumbo"')
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert "facetDistribution" not in response
assert "title" in response["hits"][0]
assert "poster" in response["hits"][0]
assert "overview" in response["hits"][0]
assert "release_date" in response["hits"][0]
assert response["hits"][0]["title"] == "Dumbo"
assert "_formatted" not in response["hits"][0]
def test_basic_search_on_nested_documents(index_with_documents, nested_movies):
"""Tests search with a simple query on nested fields."""
response = index_with_documents("nested_fields_index", nested_movies).search("An awesome")
assert isinstance(response, dict)
assert response["hits"][0]["id"] == 5
assert len(response["hits"]) == 1
def test_search_on_nested_documents_with_searchable_attributes(index_with_documents, nested_movies):
"""Tests search on nested fields with searchable attribute."""
index = index_with_documents("nested_fields_index", nested_movies)
response_searchable_attributes = index.update_searchable_attributes(["title", "info.comment"])
index.wait_for_task(response_searchable_attributes.task_uid)
response = index.search("An awesome")
assert isinstance(response, dict)
assert response["hits"][0]["id"] == 5
assert len(response["hits"]) == 1
def test_search_on_nested_documents_with_sortable_attributes(index_with_documents, nested_movies):
"""Tests search on nested fields with searchable attribute and sortable attributes."""
index = index_with_documents("nested_fields_index", nested_movies)
response_settings = index.update_settings(
{
"searchableAttributes": ["title", "info.comment"],
"sortableAttributes": ["info.reviewNb"],
}
)
index.wait_for_task(response_settings.task_uid)
response = index.search("", {"sort": ["info.reviewNb:desc"]})
assert isinstance(response, dict)
assert response["hits"][0]["id"] == 6
def test_custom_search_params_with_matching_strategy_all(index_with_documents):
"""Tests search with matching strategy param set to all"""
response = index_with_documents().search(
"man loves",
{
"limit": 5,
"matchingStrategy": "all",
},
)
assert isinstance(response, dict)
assert len(response["hits"]) == 1
def test_custom_search_params_with_matching_strategy_last(index_with_documents):
"""Tests search with matching strategy param set to last"""
response = index_with_documents().search(
"man loves",
{
"limit": 5,
"matchingStrategy": "last",
},
)
assert isinstance(response, dict)
assert len(response["hits"]) > 1
def test_custom_search_params_with_pagination_parameters(index_with_documents):
"""Tests search with matching strategy param set to last"""
response = index_with_documents().search("", {"hitsPerPage": 1, "page": 1})
assert isinstance(response, dict)
assert len(response["hits"]) == 1
assert response["hitsPerPage"] == 1
assert response["page"] == 1
assert response["totalPages"] is not None
assert response["totalHits"] is not None
assert "estimatedTotalHits" is not response
def test_custom_search_params_with_pagination_parameters_at_zero(index_with_documents):
"""Tests search with matching strategy param set to last"""
response = index_with_documents().search("", {"hitsPerPage": 0, "page": 0})
assert isinstance(response, dict)
assert len(response["hits"]) == 0
assert response["hitsPerPage"] == 0
assert response["page"] == 0
assert response["totalPages"] is not None
assert response["totalHits"] is not None
assert "estimatedTotalHits" is not response
@pytest.mark.usefixtures("enable_vector_search")
def test_vector_search(index_with_documents_and_vectors):
response = index_with_documents_and_vectors().search(
"How to Train Your Dragon", opt_params={"vector": [0.1, 0.2]}
)
assert response["hits"][0]["id"] == "287947"
assert response["vector"] == [0.1, 0.2]