Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full text search with weight in mongoose

As I find out, since version 3.8.9, mongoose support full text search. But I can't find a good documentation for it!
I want to do something like:

db.collection.ensureIndex(     // Fields to index     {         animal:  "text",         color:   "text",         pattern: "text",         size:    "text"     },      // Options     {         name: "best_match_index",          // Adjust field weights (default is 1)         weights: {             animal: 5,  // Most relevant search field             size:   4   // Also relevant        }     } ) 

Can I do it with pure mongoose? Or I have to use some plugin like mongoose-text-search? How about without weight?
And how should I do it?

like image 646
Foad Nosrati Habibi Avatar asked Jul 12 '14 14:07

Foad Nosrati Habibi


People also ask

Can MongoDB do full text search?

Implement full-text search in MongoDB Atlas Go to any cluster and select the “Search” tab to do so. From there, you can click on “Create Search Index” to launch the process. Once the index is created, you can use the $search operator to perform full-text searches.

How do I search for text in a field in MongoDB?

In MongoDB, we can perform text search using text index and $text operator. Text index: MongoDB proved text indexes that are used to find the specified text from the string content. Text indexes should be either a string or an array of string elements.

What is $in in Mongoose?

The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.

How does MongoDB text search work?

MongoDB text search uses the Snowball stemming library to reduce words to an expected root form (or stem) based on common language rules. Algorithmic stemming provides a quick reduction, but languages have exceptions (such as irregular or contradicting verb conjugation patterns) that can affect accuracy.


1 Answers

Yes, you can use full text search in Mongoose >= 3.8.9. Firstly, a collection can have at most one text index (see docs). So, to define text index for several fields, you need compound index:

schema.index({ animal: 'text', color: 'text', pattern: 'text', size: 'text' }); 

Now you can use $text query operator like this:

Model     .find(         { $text : { $search : "text to look for" } },          { score : { $meta: "textScore" } }     )     .sort({ score : { $meta : 'textScore' } })     .exec(function(err, results) {         // callback     }); 

This will also sort results by relevance score.

As for weights, you can try to pass weights options object to index() method (where you define compound index) (working at least with v4.0.1 of mongoose):

schema.index({ animal: 'text', color: 'text', pattern: 'text', size: 'text' }, {name: 'My text index', weights: {animal: 10, color: 4, pattern: 2, size: 1}}); 
like image 160
eagor Avatar answered Oct 05 '22 14:10

eagor