plexinc / papr

MongoDB TypeScript-aware Models
https://plexinc.github.io/papr/
MIT License
426 stars 22 forks source link

Weighted text search #98

Open djdembeck opened 2 years ago

djdembeck commented 2 years ago

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

djdembeck commented 2 years ago

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' } } },
                )