From f20a7d02329abee59c52dfcb5dcb002ba5e689eb Mon Sep 17 00:00:00 2001 From: Daniel Shuy Date: Tue, 12 Oct 2021 22:22:45 +0800 Subject: [PATCH 1/2] Support * in attributesToSnippet --- .../highlight-adapter.ts | 3 ++- tests/snippets.tests.ts | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/adapter/search-response-adapter/highlight-adapter.ts b/src/adapter/search-response-adapter/highlight-adapter.ts index 3d1f7694..c469935b 100644 --- a/src/adapter/search-response-adapter/highlight-adapter.ts +++ b/src/adapter/search-response-adapter/highlight-adapter.ts @@ -104,10 +104,11 @@ 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 return (Object.keys(formattedHit) as any[]).reduce((result, key) => { - if (attributesToSnippet?.includes(key)) { + if (snippetAll || attributesToSnippet?.includes(key)) { ;(result[key] as any) = { value: snippetValue( formattedHit[key], diff --git a/tests/snippets.tests.ts b/tests/snippets.tests.ts index fa6a2a6d..da93fa23 100644 --- a/tests/snippets.tests.ts +++ b/tests/snippets.tests.ts @@ -49,6 +49,33 @@ 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?.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?.release_date?.value).toEqual('750643200') + }) + test('Test two attributesToSnippet on specific query with one hit empty string', async () => { const response = await searchClient.search([ { From 6e7960b8408b8d1d25bc0959e60add26050aa5e0 Mon Sep 17 00:00:00 2001 From: Daniel Shuy Date: Fri, 15 Oct 2021 01:30:56 +0800 Subject: [PATCH 2/2] Highlight and Snippet arrays --- .../highlight-adapter.ts | 34 +++++++++++-------- tests/snippets.tests.ts | 12 +++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/adapter/search-response-adapter/highlight-adapter.ts b/src/adapter/search-response-adapter/highlight-adapter.ts index c469935b..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) } @@ -107,16 +107,20 @@ function adaptSnippet( 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 (snippetAll || attributesToSnippet?.includes(key)) { - ;(result[key] as any) = { - value: snippetValue( - formattedHit[key], - snippetEllipsisText, - highlightPreTag, - highlightPostTag - ), - } + 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 da93fa23..30e9b222 100644 --- a/tests/snippets.tests.ts +++ b/tests/snippets.tests.ts @@ -67,12 +67,24 @@ describe('Snippet Browser test', () => { '__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') })