Skip to content

Commit 0cf8704

Browse files
authored
Add rankingScore and rankingScoreDetails types (#1537)
* Experimental vector search for MS v1.3.0 * Add vector search error codes * Use permission as key when enabling the experimental prototype * Add rankingScore and rankingScoreDetails types * Use RankingScoreDetails type to type _rankingScoreDetails
1 parent 560bfda commit 0cf8704

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/types/types.ts

+35
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export type SearchParams = Query &
109109
facetName?: string
110110
facetQuery?: string
111111
vector?: number[] | null
112+
showRankingScore?: boolean
113+
showRankingScoreDetails?: boolean
112114
attributesToSearchOn?: string[] | null
113115
}
114116

@@ -148,6 +150,39 @@ export type MatchesPosition<T> = Partial<
148150
export type Hit<T = Record<string, any>> = T & {
149151
_formatted?: Partial<T>
150152
_matchesPosition?: MatchesPosition<T>
153+
_rankingScore?: number
154+
_rankingScoreDetails?: RakingScoreDetails
155+
}
156+
157+
export type RakingScoreDetails = {
158+
words?: {
159+
order: number
160+
matchingWords: number
161+
maxMatchingWords: number
162+
score: number
163+
}
164+
typo?: {
165+
order: number
166+
typoCount: number
167+
maxTypoCount: number
168+
score: number
169+
}
170+
proximity?: {
171+
order: number
172+
score: number
173+
}
174+
attribute?: {
175+
order: number
176+
attributes_ranking_order: number
177+
attributes_query_word_order: number
178+
score: number
179+
}
180+
exactness?: {
181+
order: number
182+
matchType: string
183+
score: number
184+
}
185+
[key: string]: Record<string, any> | undefined
151186
}
152187

153188
export type Hits<T = Record<string, any>> = Array<Hit<T>>

tests/search.test.ts

+45
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,51 @@ describe.each([
257257
expect(hit.id).toEqual(1)
258258
})
259259

260+
test(`${permission} key: search with _showRankingScore enabled`, async () => {
261+
const client = await getClient(permission)
262+
263+
const response = await client.index(index.uid).search('prince', {
264+
showRankingScore: true,
265+
})
266+
267+
const hit = response.hits[0]
268+
269+
expect(response).toHaveProperty('hits', expect.any(Array))
270+
expect(response).toHaveProperty('query', 'prince')
271+
expect(hit).toHaveProperty('_rankingScore')
272+
})
273+
274+
test(`${permission} key: search with showRankingScoreDetails enabled`, async () => {
275+
const client = await getClient(permission)
276+
const key = await getKey(permission)
277+
278+
await fetch(`${HOST}/experimental-features`, {
279+
body: JSON.stringify({ scoreDetails: true }),
280+
headers: {
281+
Authorization: `Bearer ${key}`,
282+
'Content-Type': 'application/json',
283+
},
284+
method: 'PATCH',
285+
})
286+
287+
const response = await client.index(index.uid).search('prince', {
288+
showRankingScoreDetails: true,
289+
})
290+
291+
const hit = response.hits[0]
292+
293+
expect(response).toHaveProperty('hits', expect.any(Array))
294+
expect(response).toHaveProperty('query', 'prince')
295+
expect(hit).toHaveProperty('_rankingScoreDetails')
296+
expect(Object.keys(hit._rankingScoreDetails || {})).toEqual([
297+
'words',
298+
'typo',
299+
'proximity',
300+
'attribute',
301+
'exactness',
302+
])
303+
})
304+
260305
test(`${permission} key: search with array options`, async () => {
261306
const client = await getClient(permission)
262307

0 commit comments

Comments
 (0)