我有一个名为“posts”的文档,它是这样的:
{
"_id" : ObjectId("5afc22290c06a67f081fa463"),
"title" : "Cool",
"description" : "this is amazing"
}
我已经在标题和描述上添加了索引:
db.posts.createIndex( { title: "text", description: "text" } )
问题是,当我搜索并输入“amaz”时,它会返回上面带有“this is Amazing”的数据,而只有当我输入“amazing”时它才应该返回数据
db.posts.find({ $text: { $search: 'amaz' } }, (err, results) => {
return res.json(results);
});
最佳答案
此处的原始数据归功于@amenadiel:
https://stackoverflow.com/a/24316510/7948962
来自 MongoDB 文档:
https://docs.mongodb.com/manual/core/index-text/
Index Entries
text index tokenizes and stems the terms in the indexed fields for the index entries. text index stores one index entry for each unique stemmed term in each indexed field for each document in the collection. The index uses simple language-specific suffix stemming.
这允许您在索引中搜索部分“词干”术语,并让数据库返回所有相关结果。在您的特定场景中,amaz
有点奇怪,因为与其他单词(例如 talking
)相比,它有点不规则,后者被标记为单词 谈话
,或谈话
到谈话
。类似地,walking
和 walked
变为 walk
。
就您而言,文本中的单词 amazing
将被标记为 amaz
。如果您的列包含诸如 amazed
之类的数据,它也会收到相同的 amaz
token 。这些结果也会从 amaz
的搜索中返回。
关于MongoDb 索引全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50371396/