Skip to content

Commit 86eee40

Browse files
committed
Support * in attributesToSnippet
1 parent 815ee1e commit 86eee40

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ 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
109110
return (Object.keys(formattedHit) as any[]).reduce((result, key) => {
110-
if (attributesToSnippet?.includes(key)) {
111+
if (snippetAll || attributesToSnippet?.includes(key)) {
111112
;(result[key] as any) = {
112113
value: snippetValue(
113114
formattedHit[key],

tests/snippets.tests.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ 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('__ais-highlight__Judg__/ais-highlight__ment Night')
67+
expect(highlighted?.overview?.value).toEqual('While')
68+
expect(highlighted?.release_date?.value).toEqual('750643200')
69+
expect(snippeted?.id?.value).toEqual('6')
70+
expect(snippeted?.title?.value).toEqual('__ais-highlight__Judg__/ais-highlight__ment Night...')
71+
expect(snippeted?.overview?.value).toEqual('While...')
72+
expect(snippeted?.release_date?.value).toEqual('750643200')
73+
})
74+
5275
test('Test two attributesToSnippet on specific query with one hit empty string', async () => {
5376
const response = await searchClient.search<Movies>([
5477
{

0 commit comments

Comments
 (0)