MongoDb 索引全文搜索

标签 mongodb indexing mongoose

我有一个名为“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)相比,它有点不规则,后者被标记为单词 谈话,或谈话谈话。类似地,walkingwalked 变为 walk

就您而言,文本中的单词 amazing 将被标记为 amaz。如果您的列包含诸如 amazed 之类的数据,它也会收到相同的 amaz token 。这些结果也会从 amaz 的搜索中返回。

关于MongoDb 索引全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50371396/

相关文章:

MySQL --> MongoDB : Keep IDs or create mapping?

c# - 从另一个列表中的字符串获取列表的索引

node.js - 你如何更新 Node.js 中的一对多关系?

node.js - Mongo 更新 super 慢

java - 哪种noSQL DB适合变量多字段的快速查询?

node.js - 如何增加批量数组字段 mongodb 上的计数器

javascript - for循环内的异步函数

node.js - 如何将自定义数据添加到 Mongoose 模式?

node.js - MongoDB多个不区分大小写的字段唯一索引,错误信息没有意义

python - 从另一个列表中删除列表项