Skip to content

Commit 0cec448

Browse files
Merge #552
552: Support * in attributesToSnippet r=bidoubiwa a=daniel-shuy Fixes #369 Add support for `*` in `attributesToSnippet` to snippet all attributes, as documented in https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/#usage-notes Co-authored-by: Daniel Shuy <[email protected]>
2 parents 241d12b + 6e7960b commit 0cec448

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

src/adapter/search-response-adapter/highlight-adapter.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ function adaptHighlight(
4141
): Record<string, any> {
4242
// formattedHit is the `_formatted` object returned by MeiliSearch.
4343
// It contains all the highlighted and croped attributes
44+
const toHighlightMatch = (value: any) => ({
45+
value: replaceHighlightTags(value, highlightPreTag, highlightPostTag),
46+
})
4447
return Object.keys(formattedHit).reduce((result, key) => {
45-
;(result[key] as any) = {
46-
value: replaceHighlightTags(
47-
formattedHit[key],
48-
highlightPreTag,
49-
highlightPostTag
50-
),
51-
}
48+
const value = formattedHit[key]
49+
result[key] = Array.isArray(value)
50+
? value.map(toHighlightMatch)
51+
: toHighlightMatch(value)
5252
return result
5353
}, {} as any)
5454
}
@@ -104,18 +104,23 @@ function adaptSnippet(
104104
attributesToSnippet = attributesToSnippet.map(
105105
(attribute) => attribute.split(':')[0]
106106
) as any[]
107+
const snippetAll = attributesToSnippet.includes('*')
107108
// formattedHit is the `_formatted` object returned by MeiliSearch.
108109
// It contains all the highlighted and croped attributes
110+
const toSnippetMatch = (value: any) => ({
111+
value: snippetValue(
112+
value,
113+
snippetEllipsisText,
114+
highlightPreTag,
115+
highlightPostTag
116+
),
117+
})
109118
return (Object.keys(formattedHit) as any[]).reduce((result, key) => {
110-
if (attributesToSnippet?.includes(key)) {
111-
;(result[key] as any) = {
112-
value: snippetValue(
113-
formattedHit[key],
114-
snippetEllipsisText,
115-
highlightPreTag,
116-
highlightPostTag
117-
),
118-
}
119+
if (snippetAll || attributesToSnippet?.includes(key)) {
120+
const value = formattedHit[key]
121+
result[key] = Array.isArray(value)
122+
? value.map(toSnippetMatch)
123+
: toSnippetMatch(value)
119124
}
120125
return result
121126
}, {} as any)

tests/snippets.tests.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,45 @@ describe('Snippet Browser test', () => {
4949
expect(snippeted?.overview?.value).toEqual('While...')
5050
})
5151

52+
test('Test * attributesToSnippet on specific query', async () => {
53+
const response = await searchClient.search<Movies>([
54+
{
55+
indexName: 'movies',
56+
params: {
57+
query: 'judg',
58+
attributesToSnippet: ['*:2'],
59+
snippetEllipsisText: '...',
60+
},
61+
},
62+
])
63+
const highlighted = response.results[0]?.hits[0]?._highlightResult
64+
const snippeted = response.results[0].hits[0]._snippetResult
65+
expect(highlighted?.id?.value).toEqual('6')
66+
expect(highlighted?.title?.value).toEqual(
67+
'__ais-highlight__Judg__/ais-highlight__ment Night'
68+
)
69+
expect(highlighted?.overview?.value).toEqual('While')
70+
expect(highlighted?.genres).toBeTruthy()
71+
if (highlighted?.genres) {
72+
expect(highlighted?.genres[0].value).toEqual('Action')
73+
expect(highlighted?.genres[1].value).toEqual('Thriller')
74+
expect(highlighted?.genres[2].value).toEqual('Crime')
75+
}
76+
expect(highlighted?.release_date?.value).toEqual('750643200')
77+
expect(snippeted?.id?.value).toEqual('6')
78+
expect(snippeted?.title?.value).toEqual(
79+
'__ais-highlight__Judg__/ais-highlight__ment Night...'
80+
)
81+
expect(snippeted?.overview?.value).toEqual('While...')
82+
expect(snippeted?.genres).toBeTruthy()
83+
if (snippeted?.genres) {
84+
expect(snippeted?.genres[0].value).toEqual('Action...')
85+
expect(snippeted?.genres[1].value).toEqual('Thriller...')
86+
expect(snippeted?.genres[2].value).toEqual('Crime...')
87+
}
88+
expect(snippeted?.release_date?.value).toEqual('750643200')
89+
})
90+
5291
test('Test two attributesToSnippet on specific query with one hit empty string', async () => {
5392
const response = await searchClient.search<Movies>([
5493
{

0 commit comments

Comments
 (0)