Skip to content

Conversation

@coyotte508
Copy link
Contributor

@coyotte508 coyotte508 commented Jun 4, 2022

Summary

Sorting by text score is a special case. See https://www.mongodb.com/docs/manual/reference/method/cursor.sort/#text-score-metadata-sort

This fixes a type error caused by 6.3.5

Examples

This code fails with 6.3.5:

const cities = await InseeCity.find({ $text: { $search: req.query.query as string } })
    .sort({ score: { $meta: "textScore" } })

It used to work

Sorting by text score is a special case. See https://www.mongodb.com/docs/manual/reference/method/cursor.sort/#text-score-metadata-sort

This fixes a type error caused by 6.3.5
@coyotte508
Copy link
Contributor Author

Note: should fix #11867 and apparently #11370 would fix it as well.

types/query.d.ts Outdated

/** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */
sort(arg?: string | { [key: string]: SortOrder } | undefined | null): this;
sort(arg?: string | { [key: string]: SortOrder } | { score: { $meta: 'textScore' } } | undefined | null): this;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The sort key doesn't necessarily have to be called score. You can also do { textScore: { $meta: 'textScore' } }, see https://thecodebarbarian.com/2014/04/10/a-nodejs-perspective-on-whats-new-in-mongodb-2-6-part-i-text-search.html .

According to https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/, { $meta: 'indexKey' } is also valid.

Maybe something like { [key: string]: { $meta: 'textScore' | 'indexKey' } } ?

Copy link
Contributor Author

@coyotte508 coyotte508 Jun 5, 2022

Choose a reason for hiding this comment

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

Good point about the score key. { $meta: 'indexKey' } should be valid but not for sorting, it's just for the projection I think.

@coyotte508 coyotte508 requested a review from vkarpov15 June 5, 2022 16:01
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Nice, thanks 👍

@vkarpov15 vkarpov15 added this to the 6.3.6 milestone Jun 5, 2022
@vkarpov15 vkarpov15 merged commit 4cf2920 into Automattic:master Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants