Mongo 按索引字段搜索数据库
Mongo Db search by indexed field
我将字段 'search_string' 添加到我的文档中并为其编制索引。 db.my_collection.createIndex({search_string: "text"})
Search_string 包含:'a ar are aren arena'.
我可以找到这样的记录:db.my_collection.find({$text: {$search: 'ar'}})
、db.my_collection.find({$text: {$search: 'аren'}})
、db.my_collection.find({$text: {$search: 'arenа'}})
,但是 db.my_collection.find({$text: {$search: 'а'}})
和 db.my_collection.find({$text: {$search: 'аre'}})
returns 什么都没有。为什么会这样?
来自MongoDBmanual
Match Operation
Stop Words
The $text operator ignores language-specific stop words, such as the and and in English.
"a" 和 "are" 都在默认的英语停用词列表中,因此将被忽略。快速 google 搜索英语停用词将找到包含完整列表的大量页面。
默认情况下,MongoDB使用英文作为文本索引,不索引停用词。
If you specify a language value of "none", then the text search uses simple tokenization with no list of stop words and no stemming.
所以你应该像这样创建你的索引:
db.my_collection.createIndex(
{ search_string : "text" },
{ default_language: "none" }
)
MongoDB 文档 here。
我将字段 'search_string' 添加到我的文档中并为其编制索引。 db.my_collection.createIndex({search_string: "text"})
Search_string 包含:'a ar are aren arena'.
我可以找到这样的记录:db.my_collection.find({$text: {$search: 'ar'}})
、db.my_collection.find({$text: {$search: 'аren'}})
、db.my_collection.find({$text: {$search: 'arenа'}})
,但是 db.my_collection.find({$text: {$search: 'а'}})
和 db.my_collection.find({$text: {$search: 'аre'}})
returns 什么都没有。为什么会这样?
来自MongoDBmanual
Match Operation
Stop Words
The $text operator ignores language-specific stop words, such as the and and in English.
"a" 和 "are" 都在默认的英语停用词列表中,因此将被忽略。快速 google 搜索英语停用词将找到包含完整列表的大量页面。
默认情况下,MongoDB使用英文作为文本索引,不索引停用词。
If you specify a language value of "none", then the text search uses simple tokenization with no list of stop words and no stemming.
所以你应该像这样创建你的索引:
db.my_collection.createIndex(
{ search_string : "text" },
{ default_language: "none" }
)
MongoDB 文档 here。