| 前提 | 集合上已建文本索引 |
|---|---|
| 语法 | { $text: { $search: "词 -排除词" } } |
| 排序 | $meta: "textScore" |
建索引并查询
建索引并查询
db.articles.createIndex({ title: "text", body: "text" }, { weights: { title: 10, body: 1 } })
db.articles.find({ $text: { $search: "分布式 事务 -mongodb" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })文本索引对中文按空格与标点切词,中文检索效果有限;整句分词需求应评估外部检索方案。