第29章:MongoDB-索引--全文索引

①全文索引

全文索引是用于对长文本检索来使用的,是用正则表达式只能对字符串类型的值进行检索。注意:创建索引是一件比较耗时耗费资源的事情,而全文索引更是耗时更厉害,如果对索引键的内容比较长,需要对内容进行分词,会出现更严重的性能问题。

创建全文索引,建议在mongodb不忙的时候创建,mongodb的分词现在好像不支持中文,如果是对内容比较小的比如小于100个汉字的可以试用一下mongodb的全文索引,如果是对一篇很长的文章使用全文索引这是非常不合适的,这会把mongodb累死的,对于内容比较多可以采取其他技术如Lucenne、Solr、ElasticSearch等技术

范例:定义一个新的集合

db.news.insert({"title":"m1 m2 m3 m4 m5","content":"m5");

db.news.insert({"title":"m1 m2 m3 m4","content":"m3");

db.news.insert({"title":"m5","content":"m6");

db.news.insert({"title":"m5","content":"m5");

db.news.insert({"title":"m6","content":"m5");

范例:设置全文检索
db.news.ensureIndex({"title":"text"},{"content":"text"});

 

④范例:实现数据的模糊查询

如果要想表示出全文检索,则使用“$text”判断符,而要想进行数据的查询则使用“$search”运算符:

|- 查询指定关键字:{"$search" : "查询关键字"};

|- 查询多个关键字(或关系):{"$search" : "查询关键字查询关键字 ..."};

|- 查询多个关键字(与关系):{"$search" : "\"查询关键字\" \"查询关键字\"..."}

|- 查询多个关键字(排除某一个):{"$search" : "查询关键字查询关键字 ... -排除关键字"}

范例:查询单个内容

db.news.find({"text":{"$search":"m5"}});

范例:包含有“m5”与“m4”的信息

db.news.find({"text":{"$search":"m5 m4"}});

范例:同时包含有“ m5”与“ m4”的内容

db.news.find({"text":{"$search":""m5" "m4""}});

范例:包含有“m5 m4”但是没有“ m3”的内容

db.news.find({"text":{"$search":""m5" "m4" -m3"}});

但是在进行全文检索操作的时候还可以使用相似度的打分来判断检索成果。

⑤范例:为结果打分

db.news.find({"text":{"$search":"m5"},{"score":{"$meta":"textScore"}}});

db.news.find({"text":{"$search":"m5"},{"score":{"$meta":"textScore"}}}).sort({"score":{"$meta":"textScore"}});

按照打分的成绩进行排列,实际上就可以实现更加准确的信息搜索。

 

⑥范例:为所有字段设置全文检索

db.news.ensureIndex({"$**":"text"});

这是一种最简单的设置全文索引的方式,但是尽可能别名,一个字:慢。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Lucky-stars/p/10555392.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值