user2500558
user2500558

Reputation: 73

Mongoose text search with diacriticSensitive

I have the following mongoose query:

User.find(
    {
        $text: {
            $search: req.query.search,
            $diacriticSensitive: true
        }
    },
    {
        score: {
            $meta: "textScore"
        }
    }, function(err, results) {
    if (err) {
        next(err);
        return;
    }

    return res.json(results);
});

The query leads to the following error message:

Can't canonicalize query: BadValue extra fields in $text

When I remove the "$diacriticSensitive: true" part from the query I get no error message, but only get exact matches.

What I want is when the user searches for "USERNA", but only the user "USERNAME" is available, this result should also be shown with a low score.

I am using Mongoose 4.3.5 and MongoDB 3.0.3.

MongoDB supports this attribute, doesn't Mongoose support it?

Upvotes: 2

Views: 4544

Answers (1)

zangw
zangw

Reputation: 48366

Per doc, $diacriticSensitive option is enable in mongodb v3.2


For articles document

{ "_id" : ObjectId("56d305dcc7ce7117db9b6da4"), "subject" : "Coffee Shopping", "author" : "efg", "view" : 5 }
{ "_id" : ObjectId("56d305e9c7ce7117db9b6da5"), "subject" : "Coffee and cream", "author" : "efg", "view" : 10 }
{ "_id" : ObjectId("56d305fec7ce7117db9b6da6"), "subject" : "Baking a cake", "author" : "abc", "view" : 50 }

Here are my test codes

var ArticleSchema = new mongoose.Schema({
    subject: String,
    author: String,
    view: Number,
    score: Number,
});

ArticleSchema.index({subject: 'text'});
var article = mongoose.model('article', ArticleSchema);

function queryText() {
    article
        .find({$text: {$search: 'Coffee', $diacriticSensitive: true}}, 
                {score: {$meta: 'textScore'}})
        .exec(function(err, doc) {
            if (err)
                console.log(err);
            else
                console.log(doc);
        });
}

Result:

{ "_id" : ObjectId("56d305dcc7ce7117db9b6da4"), "subject" : "Coffee Shopping", "author" : "efg", "view" : 5, "score" : 0.75 }
{ "_id" : ObjectId("56d305e9c7ce7117db9b6da5"), "subject" : "Coffee and cream", "author" : "efg", "view" : 10, "score" : 0.75 }

Upvotes: 2

Related Questions