Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rankingScore and rankingScoreDetails types #1537

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
35 changes: 35 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export type SearchParams = Query &
hitsPerPage?: number
page?: number
vector?: number[] | null
showRankingScore?: boolean
showRankingScoreDetails?: boolean
}

// Search parameters for searches made with the GET method
Expand Down Expand Up @@ -128,6 +130,39 @@ export type MatchesPosition<T> = Partial<
export type Hit<T = Record<string, any>> = T & {
_formatted?: Partial<T>
_matchesPosition?: MatchesPosition<T>
_rankingScore?: number
_rankingScoreDetails?: Record<string, any>
}

export type RakingScoreDetails = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to add comments to these types, to make them highlight the IDEs of people using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I realised I forgot to use RankingScoreDetails hehe. But to answer your question this is how IDe works w/ typescript types.

Screenshot 2023-07-11 at 11 53 29 Screenshot 2023-07-11 at 11 53 55

By clicking on go to type definition you'll be redirection to the RankingScoreDetails type.

Now, I can add a comment like this:

Screenshot 2023-07-11 at 11 55 53 Screenshot 2023-07-11 at 11 57 05

Which would appear like that if I hover over the type (see last line of second screen):

Screenshot 2023-07-11 at 11 57 00

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is not valid since you'll have a broken code anyway. In the line before you ensure you're taking the first item from an array :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not asserting hit but response. Not sure why it's broken

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hits will always be an array, that's my point. Otherwise you'll have an exception!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your assertion, you're telling that you expect the hits key to be an array. And that will never fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, it's a test that tests Meilisearch not my SDK haha. It's a copy paste from other tests I can remove it

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