nau*_*boy 5 mongodb mongodb-query mongodb-indexes
我有一个具有以下架构的文档
{
description : String,
tags : [String]
}
Run Code Online (Sandbox Code Playgroud)
我已将这两个字段都索引为文本,但问题是,每当我在数组中搜索特定字符串时,仅当该字符串是数组的第一个元素时,它才会返回文档。因此,似乎 $text 索引仅适用于第一个元素,这是 mongo 固有的工作方式还是有一个选项必须传递给索引?
示例文档
{
description : 'random description',
tags : ["hello", "there"]
}
Run Code Online (Sandbox Code Playgroud)
创建索引的对象
{description : 'text', tags : 'text'}
Run Code Online (Sandbox Code Playgroud)
查询
db.myCollection.find({$text : {$search : 'hello'}});
Run Code Online (Sandbox Code Playgroud)
返回一个文档但是
db.myCollection.find({$text : {$search : 'there'}});
Run Code Online (Sandbox Code Playgroud)
不返回任何内容。
使用版本2.6.11
我还有其他索引,但这些是唯一的文本索引。这是相应的输出db.myCollection.getIndexes()
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "description_text_tags_text",
"ns" : "myDB.myCollection",
"weights" : {
"description" : 1,
"tags" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 2
},
Run Code Online (Sandbox Code Playgroud)
这与字符串是数组的第一个元素还是第二个元素无关。单词“there”位于“english”语言的停用词列表中,根本没有添加到索引中。文本索引过程涉及在将术语添加到文本索引之前从文本中提取和删除停用词,并且这些过程与语言相关。
您可能希望将文本索引创建为:
db.myCollection.ensureIndex({description : 'text', tags : 'text'}, { default_language: "none" })
Run Code Online (Sandbox Code Playgroud)
如果使用“none”作为默认语言,则文本索引过程将进行简单的标记化,并且不会使用任何停用词列表。默认情况下,“english”用作文本索引的“default_language”。
| 归档时间: |
|
| 查看次数: |
1005 次 |
| 最近记录: |