@@ -18,7 +18,14 @@ interface SearchParams {
1818 /**
1919 * A list of queries to execute.
2020 */
21- queries : AlgoliaMultipleQueriesQuery [ ]
21+ queries : Array <
22+ AlgoliaMultipleQueriesQuery & {
23+ params ?: {
24+ highlightPreTag ?: string
25+ highlightPostTag ?: string
26+ }
27+ }
28+ >
2229}
2330
2431interface HighlightMetadata {
@@ -36,7 +43,6 @@ export function fetchMeilisearchResults<TRecord = Record<string, any>>({
3643 . search < TRecord > (
3744 queries . map ( ( searchParameters ) => {
3845 const { params, ...headers } = searchParameters
39-
4046 return {
4147 ...headers ,
4248 params : {
@@ -51,41 +57,55 @@ export function fetchMeilisearchResults<TRecord = Record<string, any>>({
5157 . then (
5258 ( response : Awaited < ReturnType < typeof searchClient . search < TRecord > > > ) => {
5359 return response . results . map (
54- ( result : AlgoliaSearchResponse < TRecord > ) => ( {
55- ...result ,
56- hits : result . hits . map ( ( hit ) => ( {
57- ...hit ,
58- _highlightResult : (
59- Object . entries ( hit ?. _highlightResult || { } ) as Array <
60- | [ keyof TRecord , { value : string } ]
61- | [ keyof TRecord , Array < { value : string } > ] // if the field is an array
62- >
63- ) . reduce ( ( acc , [ field , highlightResult ] ) => {
64- // if the field is an array, highlightResult is an array of objects
65- if ( Array . isArray ( highlightResult ) ) {
60+ (
61+ result : AlgoliaSearchResponse < TRecord > ,
62+ resultsArrayIndex : number
63+ ) => {
64+ const query = queries [ resultsArrayIndex ]
65+ return {
66+ ...result ,
67+ hits : result . hits . map ( ( hit ) => ( {
68+ ...hit ,
69+ _highlightResult : (
70+ Object . entries ( hit ?. _highlightResult || { } ) as Array <
71+ | [ keyof TRecord , { value : string } ]
72+ | [ keyof TRecord , Array < { value : string } > ] // if the field is an array
73+ >
74+ ) . reduce ( ( acc , [ field , highlightResult ] ) => {
75+ // if the field is an array, highlightResult is an array of objects
76+ if ( Array . isArray ( highlightResult ) ) {
77+ return {
78+ ...acc ,
79+ [ field ] : highlightResult . map ( ( highlight ) =>
80+ calculateHighlightMetadata (
81+ highlight . value ,
82+ query . params ?. highlightPreTag || HIGHLIGHT_PRE_TAG ,
83+ query . params ?. highlightPostTag || HIGHLIGHT_POST_TAG
84+ )
85+ ) ,
86+ }
87+ }
6688 return {
6789 ...acc ,
68- [ field ] : highlightResult . map ( ( highlight ) =>
69- calculateHighlightMetadata ( highlight . value )
90+ [ field ] : calculateHighlightMetadata (
91+ highlightResult . value ,
92+ query . params ?. highlightPreTag || HIGHLIGHT_PRE_TAG ,
93+ query . params ?. highlightPostTag || HIGHLIGHT_POST_TAG
7094 ) ,
7195 }
72- }
73- return {
74- ...acc ,
75- [ field ] : calculateHighlightMetadata ( highlightResult . value ) ,
76- }
77- } , { } as HighlightResult < TRecord > ) ,
78- } ) ) ,
79- } )
96+ } , { } as HighlightResult < TRecord > ) ,
97+ } ) ) ,
98+ }
99+ }
80100 )
81101 }
82102 )
83103}
84104
85105function calculateHighlightMetadata (
86106 value : string ,
87- preTag : string = HIGHLIGHT_PRE_TAG ,
88- postTag : string = HIGHLIGHT_POST_TAG
107+ preTag : string ,
108+ postTag : string
89109) : HighlightMetadata {
90110 // Extract all highlighted segments
91111 const highlightRegex = new RegExp ( `${ preTag } (.*?)${ postTag } ` , 'g' )
0 commit comments