Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/adapter/search-response-adapter/highlight-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ function adaptHighlight(
): Record<string, any> {
// 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)
}
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions tests/snippets.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Movies>([
{
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<Movies>([
{
Expand Down