-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsearch_test.dart
690 lines (583 loc) · 20.5 KB
/
search_test.dart
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import 'package:meilisearch/meilisearch.dart';
import 'package:test/test.dart';
import 'utils/books.dart';
import 'utils/books_data.dart';
import 'utils/client.dart';
import 'utils/wait_for.dart';
void main() {
group('Search', () {
setUpClient();
late String uid;
late MeiliSearchIndex index;
setUp(() {
uid = randomUid();
});
group('Books', () {
setUp(() async {
index = await createBooksIndex(uid: uid);
});
test('map', () async {
//search
//if deserialization fails it will throw
final castedResult =
await index.search('').asSearchResult().map(BookDto.fromMap);
//test
expect(castedResult.hits, everyElement(isA<BookDto>()));
});
test('with basic query', () async {
final result = await index.search('prience'); // with typo
expect(result.hits, hasLength(2));
});
test('with basic query with no q', () async {
final result = await index.search(null);
expect(result.hits, hasLength(books.length));
});
test('with basic query with an empty string q=""', () async {
final result = await index.search('');
expect(result.hits, hasLength(books.length));
});
test('with basic query with phrase search', () async {
final result = await index.search('coco "harry"');
expect(result.hits, hasLength(1));
});
test('Show ranking score details', () async {
final res = await index
.search(
'The',
SearchQuery(
showRankingScore: true,
showRankingScoreDetails: true,
attributesToHighlight: ['*'],
showMatchesPosition: true,
),
)
.asSearchResult()
.mapToContainer();
final attributeMatcher = isA<MeiliRankingScoreDetailsAttributeRule>()
.having((p0) => p0.src, 'src', allOf(isNotNull, isNotEmpty))
.having((p0) => p0.score, 'score', isNotNull)
.having((p0) => p0.order, 'order', isNotNull)
.having((p0) => p0.queryWordDistanceScore, 'queryWordDistanceScore',
isNotNull)
.having((p0) => p0.attributeRankingOrderScore,
'attributeRankingOrderScore', isNotNull);
final wordsMatcher = isA<MeiliRankingScoreDetailsWordsRule>()
.having((p0) => p0.src, 'src', allOf(isNotNull, isNotEmpty))
.having((p0) => p0.score, 'score', isNotNull)
.having((p0) => p0.order, 'order', isNotNull)
.having((p0) => p0.matchingWords, 'matchingWords', isNotNull)
.having((p0) => p0.maxMatchingWords, 'maxMatchingWords', isNotNull);
final exactnessMatcher = isA<MeiliRankingScoreDetailsExactnessRule>()
.having((p0) => p0.src, 'src', allOf(isNotNull, isNotEmpty))
.having((p0) => p0.score, 'score', isNotNull)
.having((p0) => p0.order, 'order', isNotNull)
.having(
(p0) => p0.matchType,
'matchType',
allOf(isNotNull, isNotEmpty),
);
final typoMatcher = isA<MeiliRankingScoreDetailsTypoRule>()
.having((p0) => p0.src, 'src', allOf(isNotNull, isNotEmpty))
.having((p0) => p0.score, 'score', isNotNull)
.having((p0) => p0.order, 'order', isNotNull)
.having((p0) => p0.typoCount, 'typoCount', isNotNull)
.having((p0) => p0.maxTypoCount, 'maxTypoCount', isNotNull);
final proximityMatcher = isA<MeiliRankingScoreDetailsProximityRule>()
.having((p0) => p0.src, 'src', allOf(isNotNull, isNotEmpty))
.having((p0) => p0.score, 'score', isNotNull)
.having((p0) => p0.order, 'order', isNotNull);
final rankingScoreDetailsMatcher = isA<MeiliRankingScoreDetails>()
.having((p0) => p0.src, 'src', allOf(isNotNull, isNotEmpty))
.having((p0) => p0.attribute, 'attribute', attributeMatcher)
.having((p0) => p0.words, 'words', wordsMatcher)
.having((p0) => p0.exactness, 'exactness', exactnessMatcher)
.having((p0) => p0.typo, 'typo', typoMatcher)
.having((p0) => p0.proximity, 'proximity', proximityMatcher)
.having((p0) => p0.customRules, 'customRules',
allOf(isNotNull, isEmpty));
expect(res.hits.length, 4);
expect(
res.hits,
everyElement(
isA<MeiliDocumentContainer<Map<String, dynamic>>>()
.having(
(p0) => p0.parsed,
'parsed',
isNotEmpty,
)
.having(
(p0) => p0.src,
'src',
isNotEmpty,
)
.having(
(p0) => p0.rankingScore,
'rankingScore',
isNotNull,
)
.having(
(p0) => p0.rankingScoreDetails,
'rankingScoreDetails',
rankingScoreDetailsMatcher,
),
),
);
});
group('with', () {
test('offset parameter', () async {
final result =
await index.search('', SearchQuery(limit: 3, offset: 10));
expect(result.hits, isEmpty);
});
test('limit parameter', () async {
final result = await index.search('', SearchQuery(limit: 3));
expect(result.hits, hasLength(3));
});
test('cropLength parameter', () async {
final result = await index.search(
'Alice In Wonderland',
SearchQuery(
attributesToCrop: ["title"],
cropLength: 2,
),
);
expect(
result.hits[0]['_formatted']['title'],
equals('Alice In…'),
);
});
test('searches with default cropping parameters', () async {
final result = await index.search(
'prince',
SearchQuery(
attributesToCrop: ['*'],
cropLength: 2,
),
);
expect(
result.hits[0]['_formatted']['title'],
equals('…Petit Prince'),
);
});
test('searches with custom cropMarker', () async {
final result = await index.search(
'prince',
SearchQuery(
attributesToCrop: ['*'],
cropLength: 1,
cropMarker: '[…] ',
),
);
expect(result.hits[0]['_formatted']['title'], equals('[…] Prince'));
});
test('searches with custom highlight tags', () async {
final result = await index.search(
'blood',
SearchQuery(
attributesToHighlight: ['*'],
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
),
);
expect(
result.hits[0]['_formatted']['title'],
equals('Harry Potter and the Half-<mark>Blood</mark> Prince'),
);
});
test('searches with matching strategy last', () async {
final result = await index.search(
'the to',
SearchQuery(
matchingStrategy: MatchingStrategy.last,
),
);
expect(
result.hits.last['title'],
equals('Harry Potter and the Half-Blood Prince'),
);
});
test('searches with matching strategy all', () async {
final result = await index.search(
'the to',
SearchQuery(
matchingStrategy: MatchingStrategy.all,
),
);
expect(
result.hits.last['title'],
equals('The Hitchhiker\'s Guide to the Galaxy'),
);
});
test('searches with matching strategy as null if not set', () async {
final result = await index.search('the to');
expect(
result.hits.last['title'],
equals('Harry Potter and the Half-Blood Prince'),
);
});
test('filter parameter', () async {
await index
.updateFilterableAttributes(['tag']).waitFor(client: client);
final result =
await index.search('prince', SearchQuery(filter: '"tag" = Tale'));
expect(result.hits, hasLength(1));
});
test('filter parameter is null', () async {
var index = await createBooksIndex();
await index
.updateFilterableAttributes(['tag']).waitFor(client: client);
var result = await index.search(
'The Hobbit',
SearchQuery(filterExpression: 'tag'.toMeiliAttribute().isNull()),
);
expect(result.hits, hasLength(1));
expect(result.hits.first["book_id"], equals(9999));
});
test('filter parameter with spaces', () async {
await index
.updateFilterableAttributes(['tag']).waitFor(client: client);
final result = await index.search(
'prince',
SearchQuery(
filter: 'tag = "Epic fantasy"',
),
);
expect(result.hits, hasLength(1));
});
test('facetStats parameter', () async {
final indexWithNumbers = client.index(randomUid());
final docs = List.generate(
10,
(index) => <String, Object?>{
'id': 100 - index,
'year': (index * 2) + 2010,
},
);
await indexWithNumbers.addDocuments(docs).waitFor(client: client);
await indexWithNumbers
.updateFilterableAttributes(['year']).waitFor(client: client);
var result =
await indexWithNumbers.search('', SearchQuery(facets: ['*']));
expect(result.hits, hasLength(10));
expect(result.facetStats?['year']?.min, 2010);
expect(result.facetStats?['year']?.max, 2028);
});
group('facet search', () {
setUp(() async {
await index.updateFilterableAttributes(
['tag', 'book_id'],
).waitFor(client: client);
});
test('basic', () async {
final facetSearchRes = await index.facetSearch(
FacetSearchQuery(
facetName: 'tag',
facetQuery: 't',
),
);
expect(facetSearchRes.facetHits, hasLength(1));
expect(facetSearchRes.facetHits[0].count, 2);
expect(facetSearchRes.facetHits[0].value, "Tale");
expect(facetSearchRes.facetQuery, "t");
});
test('With additional search params', () async {
final facetSearchRes = await index.facetSearch(
FacetSearchQuery(facetName: 'tag', facetQuery: 't', q: 'hobbit'),
);
expect(facetSearchRes.facetHits, hasLength(0));
expect(facetSearchRes.facetQuery, "t");
});
});
test('filter parameter with number', () async {
await index.updateFilterableAttributes(
['tag', 'book_id'],
).waitFor(client: client);
final result = await index.search(
'',
SearchQuery(
filter: 'book_id < 100 AND tag = Tale',
),
);
expect(result.hits, hasLength(1));
});
test('filter parameter with array', () async {
await index
.updateFilterableAttributes(['tag']).waitFor(client: client);
final result =
await index.search('prince', SearchQuery(filter: ['tag = Tale']));
expect(result.hits, hasLength(1));
});
test('filter parameter with multiple array', () async {
await index
.updateFilterableAttributes(['tag']).waitFor(client: client);
final result = await index.search(
'prince',
SearchQuery(filter: [
['tag = Tale', 'tag = Tale'],
'tag = Tale'
]),
);
expect(result.hits, hasLength(1));
});
test('facetDistributions parameter', () async {
await index
.updateFilterableAttributes([ktag]).waitFor(client: client);
final result =
await index.search('prince', SearchQuery(facets: ['*']));
expect(result.hits, hasLength(2));
expect(result.facetDistribution?[ktag]?.length, 2);
});
test('Sort parameter', () async {
await index
.updateSettings(IndexSettings(sortableAttributes: [
'title'
], rankingRules: [
'words',
'sort',
'typo',
'proximity',
'attribute',
'exactness'
]))
.waitFor(client: client);
final result =
await index.search('prince', SearchQuery(sort: ['title:asc']));
expect(result.hits, hasLength(2));
expect(result.hits[0]['book_id'], 4);
});
group('finite-pagination query params', () {
test('with basic query', () async {
final result = await index
.search('pri', SearchQuery(page: 1))
.asPaginatedResult();
expect(result.totalPages, 1);
});
test('accesses fields from Searchable', () async {
final result = await index
.search('pri', SearchQuery(page: 1))
.asPaginatedResult();
expect(result.hits.length, greaterThanOrEqualTo(1));
expect(result.totalPages, 1);
});
test('with mixed pagination query params', () async {
final result = await index
.search('pri', SearchQuery(page: 1, limit: 10))
.asPaginatedResult();
expect(result.totalPages, 1);
});
});
});
});
group('Nested Books', () {
setUp(() async {
index = await createNestedBooksIndex(uid: uid);
});
test('searches within nested content with no parameters', () async {
final response = await index.search('An awesome');
expect(response.hits[0], {
"id": 5,
"title": 'The Hobbit',
"info": {
"comment": 'An awesome book',
"reviewNb": 900,
},
});
});
test(
'searches on nested content with searchable on specific nested field',
() async {
await index.updateSearchableAttributes(
['title', 'info.comment'],
).waitFor(client: client);
final response = await index.search('An awesome');
expect(response.hits[0], {
"id": 5,
"title": 'The Hobbit',
"info": {
"comment": 'An awesome book',
"reviewNb": 900,
},
});
});
group('attributesToSearchOn', () {
setUp(() async {
await index.updateSearchableAttributes(
['title', 'info.comment'],
).waitFor(client: client);
});
test('empty result', () async {
var response = await index.search(
'An awesome',
SearchQuery(attributesToSearchOn: ['title']),
);
expect(response.hits, isEmpty);
});
test('non-empty result', () async {
var response = await index.search(
'An awesome',
SearchQuery(attributesToSearchOn: ['info.comment']),
);
expect(response.hits[0], {
"id": 5,
"title": 'The Hobbit',
"info": {
"comment": 'An awesome book',
"reviewNb": 900,
},
});
});
});
test('searches on nested content with content with sort', () async {
await index
.updateSettings(
IndexSettings(
searchableAttributes: ['title', 'info.comment'],
sortableAttributes: ['info.reviewNb']),
)
.waitFor(client: client);
final response =
await index.search('', SearchQuery(sort: ['info.reviewNb:desc']));
expect(response.hits[0], {
"id": 6,
"title": 'Harry Potter and the Half-Blood Prince',
"info": {
"comment": 'The best book',
"reviewNb": 1000,
},
});
});
});
});
// Commented because of https://github.com/meilisearch/meilisearch-dart/issues/369
// group('Experimental', () {
// setUpClient();
// late String uid;
// late MeiliSearchIndex index;
// late ExperimentalFeatures features;
// setUp(() async {
// features = await client.http.updateExperimentalFeatures(
// UpdateExperimentalFeatures(
// vectorStore: true,
// ),
// );
// expect(features.vectorStore, true);
// uid = randomUid();
// index = await createIndexWithData(uid: uid, data: vectorBooks);
// });
// test('vector search', () async {
// final vector = [0, 1, 2];
// final res = await index
// .search(
// null,
// SearchQuery(
// vector: vector,
// ),
// )
// .asSearchResult()
// .mapToContainer();
// expect(res.vector, vector);
// expect(
// res.hits,
// everyElement(
// isA<MeiliDocumentContainer<Map<String, dynamic>>>()
// .having(
// (p0) => p0.vectors,
// 'vectors',
// isNotNull,
// )
// .having(
// (p0) => p0.semanticScore,
// 'semanticScore',
// isNotNull,
// ),
// ),
// );
// });
// });
test('search code samples', () async {
// #docregion search_get_1
await client.index('movies').search('American ninja');
// #enddocregion
// #docregion search_parameter_guide_show_ranking_score_1
await client
.index('movies')
.search('dragon', SearchQuery(showRankingScore: true));
// #enddocregion
}, skip: true);
test('facet search code samples', () async {
// #docregion facet_search_1
await client.index('books').facetSearch(
FacetSearchQuery(
facetQuery: 'fiction',
facetName: 'genres',
filter: 'rating > 3',
),
);
// #enddocregion
// #docregion facet_search_2
await client.index('books').updateFaceting(
Faceting(
sortFacetValuesBy: {
'genres': FacetingSortTypes.count,
},
),
);
// #enddocregion
// #docregion facet_search_3
await client.index('books').facetSearch(
FacetSearchQuery(
facetQuery: 'c',
facetName: 'genres',
),
);
// #enddocregion
// #docregion search_parameter_guide_attributes_to_search_on_1
await client.index('books').facetSearch(
FacetSearchQuery(
facetQuery: 'c',
facetName: 'genres',
),
);
// #enddocregion
// #docregion search_parameter_guide_facet_stats_1
await client
.index('movie_ratings')
.search('Batman', SearchQuery(facets: ['genres', 'rating']));
// #enddocregion
// #docregion faceted_search_1
await client
.index('books')
.search('', SearchQuery(facets: ['genres', 'rating', 'language']));
// #enddocregion
// #docregion filtering_guide_nested_1
await client.index('movie_ratings').search(
'thriller',
SearchQuery(
filterExpression: Meili.gte(
//or Meili.attr('rating.users')
//or 'rating.users'.toMeiliAttribute()
Meili.attrFromParts(['rating', 'users']),
Meili.value(90),
),
),
);
// #enddocregion
// #docregion sorting_guide_sort_nested_1
await client
.index('movie_ratings')
.search('thriller', SearchQuery(sort: ['rating.users:asc']));
// #enddocregion
// #docregion search_parameter_guide_page_1
await client
.index('movies')
.search('', SearchQuery(page: 2))
.asPaginatedResult();
// #enddocregion
// #docregion search_parameter_guide_hitsperpage_1
await client
.index('movies')
.search('', SearchQuery(hitsPerPage: 15))
.asPaginatedResult();
// #enddocregion
}, skip: true);
}