mongodb 中的“文本搜索”概念并不是这样工作的。相反,这里的概念是您在中定义“多个字段”“文本索引” http://docs.mongodb.org/manual/core/index-text/只需搜索术语即可。
假设你有这样的“东西”:
{ "_id" : ObjectId("55ba22294bde97b581332979"), "title" : "Hello there" },
{ "_id" : ObjectId("55ba22414bde97b58133297a"), "title" : "Hello world" },
{
"_id" : ObjectId("55ba22594bde97b58133297b"),
"title" : "Hello world",
"suburb" : "melbourne"
}
然后我决定创建一个像这样的文本索引:
db.junk.createIndex(
{ "title": "text", "suburb": "text" },
{ "weights": { "title": 10 } }
)
然后我使用进行搜索$text http://docs.mongodb.org/manual/reference/operator/query/text/:
db.junk.find(
{ "$text": { "$search": "Hello World Melbourne" } },
{ "score": { "$meta": "textScore" } }
).sort({ "score": { "$meta": "textScore" } })
结果如下:
{
"_id" : ObjectId("55ba22594bde97b58133297b"),
"title" : "Hello world",
"suburb" : "melbourne",
"score" : 11.5
},
{
"_id" : ObjectId("55ba22414bde97b58133297a"),
"title" : "Hello world",
"score" : 1.5
},
{
"_id" : ObjectId("55ba22294bde97b581332979"),
"title" : "Hello there",
"score" : 1
}
其中“两者”搜索索引中指定的所有字段,并在本例中考虑给予“郊区”字段的附加“权重”,以使其成为更受欢迎的排名。
因此,您不需要在术语中使用附加条件,而是将“所有”术语放入“一个”文本查询字符串中以在多个字段上进行搜索。