MongoDB full word search with exact phrase not returning expected results

So, according to the MongoDB docs,

if the document field contains the word blueberry, a search for the term blue will not match the document

This is good for my use case, this is what I want. However, given the following entries in the database:

> db.test.drop() > db.test.insert({ "t" : "Men Fashion" }) > db.test.insert({ "t" : "Women Fashion" }) > db.test.ensureIndex({ "t" : "text" }) 

A search for men returns the expected results:

 > db.test.find({ "$text" : { "$search" : "\"Men's\"" } }, { "_id" : 0 }) { "t" : "Men Fashion" } 

However, searching for the entire phrase Men Fashion, unexpectedly also returns Women Fashion:

 > db.test.find({ "$text" : { "$search" : "\"Men Fashion\"" } }, { "_id" : 0 }) { "t" : "Women Fashion" } { "t" : "Men Fashion" } 

I tried "\"Men's\"\"Fashion\"" also with the same results. Is there any workaround / trick to get the full phrase to return whole words?

I am using Mongo 2.6.4. Interestingly, it scores women lower than men.

+6
source share
1 answer

The results you see are related to the fact that woMEN FASHION matches MEN FASHION in the sense that the search bar located in the search bar.

In this dataset, no matching behavior occurs:

 /* 1 */ { "_id" : ObjectId("55ca6060fb286267994d297e"), "text" : "potato pancake" } /* 2 */ { "_id" : ObjectId("55ca6075fb286267994d297f"), "text" : "potato salad" } /* 3 */ { "_id" : ObjectId("55ca612ffb286267994d2980"), "text" : "potato pancake" } 

with request

 db.getCollection('rudy_test').find({$text : {"$search" : "\"potato pancake\""}}) 

This is due to the fact that the record contains the entire query, the rating is only lower, because it contains other text. You can use the db.test.find({t : {$regex : /^Men\ Fashion$/}}) query (i.e. db.test.find({t : {$regex : /^Men\ Fashion$/}}) ).

+2
source

All Articles