papr
papr copied to clipboard
Weighted text search
Hello,
I'm looking into adding text search with relevance scoring into my project. I see MongoDB has this feature (at least in 4.4+): https://docs.mongodb.com/v4.4/text-search/#sorting
It looks like Papr supports $text
and $search
already, but not score
or .sort()
. It also doesn't seem there is an index option in the Papr schema like Mongoose has: https://mongoosejs.com/docs/guide.html#indexes
Not super happy with how I was able to accomplish this, but here it is:
Author.find(
{ $text: { $search: name } },
{ projection: { asin: true, description: true, image: true, genres: true, name: true, score: { $meta: 'textScore' } }, limit: 25, sort: { score: { $meta: 'textScore' } } },
)