mongo文本搜索的一个例子
2016-05-27 11:35
369 查看
假如有一个名为articles的集合,数据如下:
{ "_id" : 1, "title" : "cakes and ale" } { "_id" : 2, "title" : "more cakes" } { "_id" : 3, "title" : "bread" } { "_id" : 4, "title" : "some cakes" } |
执行如下搜索后,可以得到文档对于cake关键字的相关度以及对应的文档个数:
db.articles.aggregate( [ { $match: { $text: { $search: "cake" } } }, { $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } } ] ) |
执行结果如下:
{ "_id" : 0.75, "count" : 1 } { "_id" : 1, "count" : 2 } |
当然,也可以按照结果的相关度进行排序。
db.articles.aggregate( [ { $match: { $text: { $search: "cake" } } }, { $ sort: { score: { $meta: "textScore" } } } ] ) |
返回的结果就是按照相关度进行排序后的结果。
相关文章推荐
- golang select
- django中的模型为什么返回的是对象
- history.back()和history.go()的区别
- Django入门
- django-debug-tools 使用
- django-debug-tools 使用
- GoldenGate的mgr进程
- Google Code Jam 2016 Round 1C C
- Algorithm_DP
- Google Code Jam 2016 Round 1B B
- go开发的Web 压力测试工具:Boom
- Django的render_to_response返回的变量转义问题解决
- VigorRoads: Kickstarter模式开启
- 为什么Go不支持函数和运算的重载
- Go语言的method
- golang使用反射创建对象
- django实战-XX学院选课系统
- 当AlphaGo战胜李世石以后,我们来聊聊深度学习
- HDU 3221 Brute-force Algorithm
- 个人logo 以前的设计