我正在尝试使用多个字段进行mongodb全文搜索.我已经在3个字段设置了索引 - 名称,描述,类别,并通过验证
document.collection.getIndexes (),返回 -
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "document.collection"
},
{
"v" : 1,
"key" : {
"name" : 2,
"description" : 1,
"category" : 1
},
"name" : "name_2_description_1_category_1",
"ns" : "document.collection",
"background" : true,
"safe" : null
}
]
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试执行文本搜索,请使用以下命令 -
db.collection.find( {$text:{$search:'alias'}} ).limit(10)
Run Code Online (Sandbox Code Playgroud)
收到以下错误消息:
error: {
"$err" : "Unable to execute query: error processing query: ns=document.collection limit=10 skip=0\nTree: TEXT : query=alias, language=, tag=NULL\nSort: {}\nProj: {}\n planner returned error: need exactly one text index for $text query",
"code" : 17007
}
Run Code Online (Sandbox Code Playgroud)
我试过谷歌和mongodb文档,但我找不到任何东西.
Chr*_*n P 16
您应该在要搜索的字段上创建文本索引:
db.deals.ensureIndex({ name: "text", description : "text", category : "text" });
Run Code Online (Sandbox Code Playgroud)
从$ text运算符的文档:
$ text对使用文本索引编制索引的字段的内容执行文本搜索.
您为三个字段创建的索引是复合索引,而不是文本索引.文本索引如下所示:
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "name_text_description_text_category_text",
"ns" : "test.deals",
"weights" : {
"category" : 1,
"description" : 1,
"name" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 2
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16154 次 |
| 最近记录: |