因此,根据MongoDB文档,



这对我的用例很有用,这就是我想要发生的情况。但是,鉴于以下数据库条目:

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

搜索Men's会返回预期结果:
> db.test.find({ "$text" : { "$search" : "\"Men's\"" } }, { "_id" : 0 })
{ "t" : "Men's Fashion" }

但是,搜索整个短语“男装时尚”出乎意料地还会返回“女装时尚”:
> db.test.find({ "$text" : { "$search" : "\"Men's Fashion\"" } }, { "_id" : 0 })
{ "t" : "Women's Fashion" }
{ "t" : "Men's Fashion" }

我也尝试过"\"Men's\"\"Fashion\""并获得相同的结果。是否有一种解决方法/技巧来使完整的短语仅返回整个单词匹配?

我正在使用Mongo 2.6.4。有趣的是,它的得分确实低于女性。

最佳答案

您看到的结果是因为woMEN'S FASHION与MEN'S FASHION相匹配,因为搜索字符串是要搜索的字符串中的

此数据集不会发生匹配行为:

/* 1 */
{
    "_id" : ObjectId("55ca6060fb286267994d297e"),
    "text" : "potato pancake"
}

/* 2 */
{
    "_id" : ObjectId("55ca6075fb286267994d297f"),
    "text" : "potato salad"
}

/* 3 */
{
    "_id" : ObjectId("55ca612ffb286267994d2980"),
    "text" : "potato's pancake"
}

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

这是因为该条目确实包含整个查询,因此分数也较低,因为它也包含其他文本。您可以改用Regular Expression Query(即db.test.find({t : {$regex : /^Men\'s Fashion$/}}))。

07-28 10:20