Skip to content

Commit a95a005

Browse files
committed
Fix fully highlighted match test
1 parent 8aeeaf6 commit a95a005

File tree

2 files changed

+86
-74
lines changed

2 files changed

+86
-74
lines changed

packages/autocomplete-client/src/search/__tests__/fetchMeilisearchResults.test.ts

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,55 +76,47 @@ describe('fetchMeilisearchResults', () => {
7676
)
7777
})
7878

79-
// test('with highlighting metadata', async () => {
80-
// const results = await fetchMeilisearchResults({
81-
// searchClient,
82-
// queries: [
83-
// {
84-
// indexName: INDEX_NAME,
85-
// query: 'Hit',
86-
// },
87-
// ],
88-
// })
79+
test('highlight results contain highlighting metadata', async () => {
80+
const results = await fetchMeilisearchResults({
81+
searchClient,
82+
queries: [
83+
{
84+
indexName: INDEX_NAME,
85+
query: 'Ariel',
86+
},
87+
],
88+
})
8989

90-
// expect(results[0].hits[0]._highlightResult).toEqual({
91-
// id: {
92-
// value: '1',
93-
// fullyHighlighted: false,
94-
// matchLevel: 'none',
95-
// matchedWords: [],
96-
// },
97-
// label: {
98-
// value: `${HIGHLIGHT_PRE_TAG}Hit${HIGHLIGHT_POST_TAG} 1`,
99-
// fullyHighlighted: false,
100-
// matchLevel: 'partial',
101-
// matchedWords: ['Hit'],
102-
// },
103-
// })
104-
// })
90+
expect(results[0].hits[0]._highlightResult?.id?.fullyHighlighted).toEqual(
91+
false
92+
)
93+
expect(results[0].hits[0]._highlightResult?.id?.matchLevel).toEqual('none')
94+
expect(results[0].hits[0]._highlightResult?.id?.matchedWords).toEqual([])
95+
expect(results[0].hits[0]._highlightResult?.id?.value).toEqual(String(2))
96+
})
10597

106-
// test('with fully highlighted match', async () => {
107-
// const pre = '<em>'
108-
// const post = '</em>'
109-
// const results = await fetchMeilisearchResults({
110-
// searchClient,
111-
// queries: [
112-
// {
113-
// indexName: INDEX_NAME,
114-
// query: 'Hit 1',
115-
// params: {
116-
// highlightPreTag: pre,
117-
// highlightPostTag: post,
118-
// },
119-
// },
120-
// ],
121-
// })
98+
test('with fully highlighted match', async () => {
99+
const pre = '<em>'
100+
const post = '</em>'
101+
const results = await fetchMeilisearchResults({
102+
searchClient,
103+
queries: [
104+
{
105+
indexName: INDEX_NAME,
106+
query: 'Ariel',
107+
params: {
108+
highlightPreTag: pre,
109+
highlightPostTag: post,
110+
},
111+
},
112+
],
113+
})
122114

123-
// expect(results[0].hits[0]._highlightResult?.label).toEqual({
124-
// value: `${pre}Hit${post} ${pre}1${post}`,
125-
// fullyHighlighted: true,
126-
// matchLevel: 'full',
127-
// matchedWords: ['Hit'],
128-
// })
129-
// })
115+
expect(results[0].hits[0]._highlightResult?.title).toEqual({
116+
value: `${pre}Ariel${post}`,
117+
fullyHighlighted: true,
118+
matchLevel: 'full',
119+
matchedWords: ['Ariel'],
120+
})
121+
})
130122
})

packages/autocomplete-client/src/search/fetchMeilisearchResults.ts

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ interface SearchParams {
1818
/**
1919
* A list of queries to execute.
2020
*/
21-
queries: AlgoliaMultipleQueriesQuery[]
21+
queries: Array<
22+
AlgoliaMultipleQueriesQuery & {
23+
params?: {
24+
highlightPreTag?: string
25+
highlightPostTag?: string
26+
}
27+
}
28+
>
2229
}
2330

2431
interface HighlightMetadata {
@@ -36,7 +43,6 @@ export function fetchMeilisearchResults<TRecord = Record<string, any>>({
3643
.search<TRecord>(
3744
queries.map((searchParameters) => {
3845
const { params, ...headers } = searchParameters
39-
4046
return {
4147
...headers,
4248
params: {
@@ -51,41 +57,55 @@ export function fetchMeilisearchResults<TRecord = Record<string, any>>({
5157
.then(
5258
(response: Awaited<ReturnType<typeof searchClient.search<TRecord>>>) => {
5359
return response.results.map(
54-
(result: AlgoliaSearchResponse<TRecord>) => ({
55-
...result,
56-
hits: result.hits.map((hit) => ({
57-
...hit,
58-
_highlightResult: (
59-
Object.entries(hit?._highlightResult || {}) as Array<
60-
| [keyof TRecord, { value: string }]
61-
| [keyof TRecord, Array<{ value: string }>] // if the field is an array
62-
>
63-
).reduce((acc, [field, highlightResult]) => {
64-
// if the field is an array, highlightResult is an array of objects
65-
if (Array.isArray(highlightResult)) {
60+
(
61+
result: AlgoliaSearchResponse<TRecord>,
62+
resultsArrayIndex: number
63+
) => {
64+
const query = queries[resultsArrayIndex]
65+
return {
66+
...result,
67+
hits: result.hits.map((hit) => ({
68+
...hit,
69+
_highlightResult: (
70+
Object.entries(hit?._highlightResult || {}) as Array<
71+
| [keyof TRecord, { value: string }]
72+
| [keyof TRecord, Array<{ value: string }>] // if the field is an array
73+
>
74+
).reduce((acc, [field, highlightResult]) => {
75+
// if the field is an array, highlightResult is an array of objects
76+
if (Array.isArray(highlightResult)) {
77+
return {
78+
...acc,
79+
[field]: highlightResult.map((highlight) =>
80+
calculateHighlightMetadata(
81+
highlight.value,
82+
query.params?.highlightPreTag || HIGHLIGHT_PRE_TAG,
83+
query.params?.highlightPostTag || HIGHLIGHT_POST_TAG
84+
)
85+
),
86+
}
87+
}
6688
return {
6789
...acc,
68-
[field]: highlightResult.map((highlight) =>
69-
calculateHighlightMetadata(highlight.value)
90+
[field]: calculateHighlightMetadata(
91+
highlightResult.value,
92+
query.params?.highlightPreTag || HIGHLIGHT_PRE_TAG,
93+
query.params?.highlightPostTag || HIGHLIGHT_POST_TAG
7094
),
7195
}
72-
}
73-
return {
74-
...acc,
75-
[field]: calculateHighlightMetadata(highlightResult.value),
76-
}
77-
}, {} as HighlightResult<TRecord>),
78-
})),
79-
})
96+
}, {} as HighlightResult<TRecord>),
97+
})),
98+
}
99+
}
80100
)
81101
}
82102
)
83103
}
84104

85105
function calculateHighlightMetadata(
86106
value: string,
87-
preTag: string = HIGHLIGHT_PRE_TAG,
88-
postTag: string = HIGHLIGHT_POST_TAG
107+
preTag: string,
108+
postTag: string
89109
): HighlightMetadata {
90110
// Extract all highlighted segments
91111
const highlightRegex = new RegExp(`${preTag}(.*?)${postTag}`, 'g')

0 commit comments

Comments
 (0)