diff --git a/src/adapter/search-response-adapter/highlight-adapter.ts b/src/adapter/search-response-adapter/highlight-adapter.ts index 3d1f7694..9d49f0a1 100644 --- a/src/adapter/search-response-adapter/highlight-adapter.ts +++ b/src/adapter/search-response-adapter/highlight-adapter.ts @@ -41,14 +41,14 @@ function adaptHighlight( ): Record { // formattedHit is the `_formatted` object returned by MeiliSearch. // It contains all the highlighted and croped attributes + const toHighlightMatch = (value: any) => ({ + value: replaceHighlightTags(value, highlightPreTag, highlightPostTag), + }) return Object.keys(formattedHit).reduce((result, key) => { - ;(result[key] as any) = { - value: replaceHighlightTags( - formattedHit[key], - highlightPreTag, - highlightPostTag - ), - } + const value = formattedHit[key] + result[key] = Array.isArray(value) + ? value.map(toHighlightMatch) + : toHighlightMatch(value) return result }, {} as any) } @@ -104,18 +104,23 @@ function adaptSnippet( attributesToSnippet = attributesToSnippet.map( (attribute) => attribute.split(':')[0] ) as any[] + const snippetAll = attributesToSnippet.includes('*') // formattedHit is the `_formatted` object returned by MeiliSearch. // It contains all the highlighted and croped attributes + const toSnippetMatch = (value: any) => ({ + value: snippetValue( + value, + snippetEllipsisText, + highlightPreTag, + highlightPostTag + ), + }) return (Object.keys(formattedHit) as any[]).reduce((result, key) => { - if (attributesToSnippet?.includes(key)) { - ;(result[key] as any) = { - value: snippetValue( - formattedHit[key], - snippetEllipsisText, - highlightPreTag, - highlightPostTag - ), - } + if (snippetAll || attributesToSnippet?.includes(key)) { + const value = formattedHit[key] + result[key] = Array.isArray(value) + ? value.map(toSnippetMatch) + : toSnippetMatch(value) } return result }, {} as any) diff --git a/tests/snippets.tests.ts b/tests/snippets.tests.ts index fa6a2a6d..30e9b222 100644 --- a/tests/snippets.tests.ts +++ b/tests/snippets.tests.ts @@ -49,6 +49,45 @@ describe('Snippet Browser test', () => { expect(snippeted?.overview?.value).toEqual('While...') }) + test('Test * attributesToSnippet on specific query', async () => { + const response = await searchClient.search([ + { + indexName: 'movies', + params: { + query: 'judg', + attributesToSnippet: ['*:2'], + snippetEllipsisText: '...', + }, + }, + ]) + const highlighted = response.results[0]?.hits[0]?._highlightResult + const snippeted = response.results[0].hits[0]._snippetResult + expect(highlighted?.id?.value).toEqual('6') + expect(highlighted?.title?.value).toEqual( + '__ais-highlight__Judg__/ais-highlight__ment Night' + ) + expect(highlighted?.overview?.value).toEqual('While') + expect(highlighted?.genres).toBeTruthy() + if (highlighted?.genres) { + expect(highlighted?.genres[0].value).toEqual('Action') + expect(highlighted?.genres[1].value).toEqual('Thriller') + expect(highlighted?.genres[2].value).toEqual('Crime') + } + expect(highlighted?.release_date?.value).toEqual('750643200') + expect(snippeted?.id?.value).toEqual('6') + expect(snippeted?.title?.value).toEqual( + '__ais-highlight__Judg__/ais-highlight__ment Night...' + ) + expect(snippeted?.overview?.value).toEqual('While...') + expect(snippeted?.genres).toBeTruthy() + if (snippeted?.genres) { + expect(snippeted?.genres[0].value).toEqual('Action...') + expect(snippeted?.genres[1].value).toEqual('Thriller...') + expect(snippeted?.genres[2].value).toEqual('Crime...') + } + expect(snippeted?.release_date?.value).toEqual('750643200') + }) + test('Test two attributesToSnippet on specific query with one hit empty string', async () => { const response = await searchClient.search([ {