Skip to content

Commit

Permalink
Add rankingScore and rankingScoreDetails types (#1537)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bidoubiwa authored Jul 12, 2023
1 parent 560bfda commit 0cf8704
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export type SearchParams = Query &
facetName?: string
facetQuery?: string
vector?: number[] | null
showRankingScore?: boolean
showRankingScoreDetails?: boolean
attributesToSearchOn?: string[] | null
}

Expand Down Expand Up @@ -148,6 +150,39 @@ export type MatchesPosition<T> = Partial<
export type Hit<T = Record<string, any>> = T & {
_formatted?: Partial<T>
_matchesPosition?: MatchesPosition<T>
_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<string, any> | undefined
}

export type Hits<T = Record<string, any>> = Array<Hit<T>>
Expand Down
45 changes: 45 additions & 0 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0cf8704

Please sign in to comment.