File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
src/adapter/search-response-adapter Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff 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 ] ,
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments