diff --git a/src/types/types.ts b/src/types/types.ts index 41aaa8922..5d11fe2f1 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -109,6 +109,8 @@ export type SearchParams = Query & facetName?: string facetQuery?: string vector?: number[] | null + showRankingScore?: boolean + showRankingScoreDetails?: boolean attributesToSearchOn?: string[] | null } @@ -148,6 +150,39 @@ export type MatchesPosition = Partial< export type Hit> = T & { _formatted?: Partial _matchesPosition?: MatchesPosition + _rankingScore?: number + _rankingScoreDetails?: RakingScoreDetails +} + +export type RakingScoreDetails = { + words?: { + order: number + matchingWords: number + maxMatchingWords: number + score: number + } + typo?: { + order: number + typoCount: number + maxTypoCount: number + score: number + } + proximity?: { + order: number + score: number + } + attribute?: { + order: number + attributes_ranking_order: number + attributes_query_word_order: number + score: number + } + exactness?: { + order: number + matchType: string + score: number + } + [key: string]: Record | undefined } export type Hits> = Array> diff --git a/tests/search.test.ts b/tests/search.test.ts index 3fc770c7c..222f36323 100644 --- a/tests/search.test.ts +++ b/tests/search.test.ts @@ -257,6 +257,51 @@ describe.each([ expect(hit.id).toEqual(1) }) + test(`${permission} key: search with _showRankingScore enabled`, async () => { + const client = await getClient(permission) + + const response = await client.index(index.uid).search('prince', { + showRankingScore: true, + }) + + const hit = response.hits[0] + + expect(response).toHaveProperty('hits', expect.any(Array)) + expect(response).toHaveProperty('query', 'prince') + expect(hit).toHaveProperty('_rankingScore') + }) + + test(`${permission} key: search with showRankingScoreDetails enabled`, async () => { + const client = await getClient(permission) + const key = await getKey(permission) + + await fetch(`${HOST}/experimental-features`, { + body: JSON.stringify({ scoreDetails: true }), + headers: { + Authorization: `Bearer ${key}`, + 'Content-Type': 'application/json', + }, + method: 'PATCH', + }) + + const response = await client.index(index.uid).search('prince', { + showRankingScoreDetails: true, + }) + + const hit = response.hits[0] + + expect(response).toHaveProperty('hits', expect.any(Array)) + expect(response).toHaveProperty('query', 'prince') + expect(hit).toHaveProperty('_rankingScoreDetails') + expect(Object.keys(hit._rankingScoreDetails || {})).toEqual([ + 'words', + 'typo', + 'proximity', + 'attribute', + 'exactness', + ]) + }) + test(`${permission} key: search with array options`, async () => { const client = await getClient(permission)