-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathfetchMeilisearchResults.test.ts
More file actions
452 lines (409 loc) · 12.1 KB
/
Copy pathfetchMeilisearchResults.test.ts
File metadata and controls
452 lines (409 loc) · 12.1 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
import { describe, beforeAll, test, expect, afterAll } from 'vitest'
import { fetchMeilisearchResults } from '../fetchMeilisearchResults.js'
import {
searchClient,
MOVIES,
meilisearchClient,
} from '../../../__tests__/test.utils.js'
import { type HighlightMetadata } from '../highlight.js'
type Movie = (typeof MOVIES)[number]
const INDEX_NAME = 'movies_fetch-meilisearch-results-test'
const FIRST_ITEM_ID = MOVIES[0].id
const SECOND_ITEM_ID = MOVIES[1].id
beforeAll(async () => {
await meilisearchClient.deleteIndex(INDEX_NAME)
await meilisearchClient.index(INDEX_NAME).addDocuments(MOVIES).waitTask()
})
afterAll(async () => {
await meilisearchClient.deleteIndex(INDEX_NAME)
})
describe('fetchMeilisearchResults', () => {
test('with default options', async () => {
const results = await fetchMeilisearchResults<Movie>({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: '',
},
],
})
expect(results[0].hits[0].id).toEqual(FIRST_ITEM_ID)
expect(results[0].hits[1].id).toEqual(SECOND_ITEM_ID)
})
test('with custom pagination', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: '',
params: {
hitsPerPage: 1,
page: 1, // pages start at 0
},
},
],
})
expect(results[0].hits[0].id).toEqual(SECOND_ITEM_ID)
})
test('with custom highlight tags', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'Ariel',
params: {
highlightPreTag: '<b>',
highlightPostTag: '</b>',
},
},
],
})
expect(results[0].hits[0]._highlightResult?.title?.value).toEqual(
'<b>Ariel</b>'
)
})
test('highlight results contain highlighting metadata', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'Ariel',
},
],
})
expect(results[0].hits[0]._highlightResult?.id?.fullyHighlighted).toEqual(
false
)
expect(results[0].hits[0]._highlightResult?.id?.matchLevel).toEqual('none')
expect(results[0].hits[0]._highlightResult?.id?.matchedWords).toEqual([])
expect(results[0].hits[0]._highlightResult?.id?.value).toEqual(String(2))
})
test('highlight results contain fully highlighted match', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'Ariel',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
expect(results[0].hits[0]._highlightResult?.title).toEqual({
value: `${pre}Ariel${post}`,
fullyHighlighted: true,
matchLevel: 'full',
matchedWords: ['Ariel'],
})
})
test('highlight results contains full match but not fully highlighted', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'Star',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
expect(results[0].hits[0]._highlightResult?.title).toEqual({
value: `${pre}Star${post} Wars`,
fullyHighlighted: false,
matchLevel: 'full',
matchedWords: ['Star'],
})
})
test('highlight results contain partially highlighted match', async () => {
const pre = '<em>'
const post = '</em>'
const movie = MOVIES[0]
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'Tasto', // missing 'i' from 'Taisto'
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
expect(results[0].hits[0]._highlightResult?.overview).toEqual({
// The first word of the overview is highlighted
value: `${pre}Taist${post}` + (movie.overview as string).slice(5),
fullyHighlighted: false,
matchLevel: 'partial',
matchedWords: ['Taist'],
})
})
test('highlight results contain no match', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [{ indexName: INDEX_NAME, query: '' }],
})
expect(results[0].hits[0]._highlightResult?.title).toEqual({
value: 'Ariel',
fullyHighlighted: false,
matchLevel: 'none',
matchedWords: [],
})
})
test('highlight results skips attributes missing value key', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'kill bill',
},
],
})
expect(results[0].hits[0]._highlightResult?.reviews).toEqual(undefined)
})
test('attaches metadata to each hit when present in result', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: 'Ariel',
},
],
})
// If metadata is present in the result, it should be on each hit
const firstResult = results[0] as any
if (firstResult._meilisearch?.metadata) {
const firstHit = results[0].hits[0] as any
expect(firstHit._meilisearch).toBeDefined()
expect(firstHit._meilisearch.metadata).toBeDefined()
expect(firstHit._meilisearch.metadata.queryUid).toBeDefined()
expect(firstHit._meilisearch.metadata.indexUid).toEqual(INDEX_NAME)
}
})
test('does not add metadata field when not present in result', async () => {
const results = await fetchMeilisearchResults({
searchClient,
queries: [
{
indexName: INDEX_NAME,
query: '',
},
],
})
const firstResult = results[0] as any
// Only check hits if result doesn't have metadata
if (!firstResult._meilisearch?.metadata) {
const firstHit = results[0].hits[0] as any
expect(firstHit._meilisearch).toBeUndefined()
}
})
describe('Highlighting Metadata', () => {
interface Person {
id: number
name: string
nicknames: string[]
familyMembers: Array<{
relationship: string
name: string
}>
crew?: Array<{ role: string; credit: string }>
}
interface PersonHighlightResult {
id: HighlightMetadata
name: HighlightMetadata
nicknames: HighlightMetadata[]
familyMembers: Array<{
relationship: HighlightMetadata
name: HighlightMetadata
}>
crew?: Array<{
role: HighlightMetadata
credit: HighlightMetadata
}>
}
const PERSON: Person = {
id: 1,
name: 'Joseph',
nicknames: ['Joe', 'Joey'],
familyMembers: [
{
relationship: 'mother',
name: 'Susan',
},
{
relationship: 'father',
name: 'John',
},
],
crew: [
{ role: 'director', credit: 'Neo' },
{ role: 'producer', credit: 'Trinity' },
],
}
const PEOPLE_INDEX = 'people_highlight_test'
beforeAll(async () => {
await meilisearchClient.deleteIndex(PEOPLE_INDEX)
await meilisearchClient
.index(PEOPLE_INDEX)
.addDocuments([PERSON])
.waitTask()
})
afterAll(async () => {
await meilisearchClient.deleteIndex(PEOPLE_INDEX)
})
describe('Recursive Enrichment', () => {
test('enriches simple string arrays with metadata', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults<Person>({
searchClient,
queries: [
{
indexName: PEOPLE_INDEX,
query: 'Joe',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
const highlightResult = results[0].hits[0]
._highlightResult as PersonHighlightResult
expect(highlightResult.nicknames[0]).toEqual({
value: `${pre}Joe${post}`,
fullyHighlighted: true,
matchLevel: 'full',
matchedWords: ['Joe'],
})
})
test('recursively enriches objects nested inside arrays', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults<Person>({
searchClient,
queries: [
{
indexName: PEOPLE_INDEX,
query: 'Susan',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
const highlightResult = results[0].hits[0]
._highlightResult as PersonHighlightResult
expect(highlightResult.familyMembers[0].name).toEqual({
value: `${pre}Susan${post}`,
fullyHighlighted: true,
matchLevel: 'full',
matchedWords: ['Susan'],
})
})
test('handles multiple highlighted fields within the same nested object', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults<Person>({
searchClient,
queries: [
{
indexName: PEOPLE_INDEX,
query: 'mother',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
const highlightResult = results[0].hits[0]
._highlightResult as PersonHighlightResult
expect(highlightResult.familyMembers[0].relationship).toEqual({
value: `${pre}mother${post}`,
fullyHighlighted: true,
matchLevel: 'full',
matchedWords: ['mother'],
})
})
test('preserves the original structure for non-matching nested fields', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults<Person>({
searchClient,
queries: [
{
indexName: PEOPLE_INDEX,
query: 'Neo',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
const highlightResult = results[0].hits[0]
._highlightResult as PersonHighlightResult
expect(highlightResult.crew).toBeDefined()
expect(highlightResult.crew).toHaveLength(2)
expect(highlightResult.crew![0].credit).toEqual({
value: `${pre}Neo${post}`,
fullyHighlighted: true,
matchLevel: 'full',
matchedWords: ['Neo'],
})
expect(highlightResult.crew![1].credit).toEqual({
value: 'Trinity',
fullyHighlighted: false,
matchLevel: 'none',
matchedWords: [],
})
})
})
describe('Match Logic', () => {
test('performs case-insensitive matchLevel validation', async () => {
const pre = '<em>'
const post = '</em>'
const results = await fetchMeilisearchResults<Person>({
searchClient,
queries: [
{
indexName: PEOPLE_INDEX,
query: 'joseph',
params: {
highlightPreTag: pre,
highlightPostTag: post,
},
},
],
})
const highlightResult = results[0].hits[0]
._highlightResult as PersonHighlightResult
expect(highlightResult.name).toEqual({
value: `${pre}Joseph${post}`,
fullyHighlighted: true,
matchLevel: 'full',
matchedWords: ['Joseph'],
})
})
})
})
})