全文索引

MongoDB有一个特殊的索引用在文档中搜索文本,之前的博客都是用精确匹配来查询字符串,这些技术有一定的限制。在搜索大块文本的速度非常慢,而且无法处理自然语言礼节的问题。全文本索引使用的是“倒排索引”的思想来做的,和当前非常开源的lucene(全文检索)项目是一样的思想来实现的。使用全文本索引可以非常快的进行文本搜索,MongoDB支持多种语言,可惜在免费版中,并不支持汉语。查MongoDB的官网可以看到,在企业版中安装第三方插件可以支持中文的全文索引。

使用全文本检索需要专门开启这个功能才能进行使用。启动MongoDB时指定--setParameter textSearchEnabled=true选项,或者在运行时执行setParameter命令,都可以启用全文本索引。如 db.adminCommand({"setParameter":1,"textSearchEnabled":true});

例:

db.news.insert({"title":"SEOUL","context":"SEOUL, June 10 (Reuters) - South Korean prosecutors raided the offices of Lotte Group, the country's fifth-largest conglomerate, and several affiliates on Friday, dealing a further blow to its hotel unit's planned IPO, billed as the world's biggest this year."});

db.news.insert({"title":"Many Chinese people","context":"Many Chinese people think that a job on a diplomatic team is too good to quit. So when 28-year-old Liu Xiaoxi left her embassy post as an attache late last year to start a career in photography, she quickly became a popular topic among the Chinese online community."});

db.news.insert({"title":"About","context":"About 200 investigators searched 17 locations including group headquarters in central Seoul and the homes of Chairman Shin Dong-bin and other key executives, local news agency Yonhap reported, citing the Seoul Central Prosecutor's office."});

db.news.insert({"title":"Three people","context":"Three people with direct knowledge of the matter told Reuters that Friday's raids were part of an investigation into a possible slush fund. They also declined to be identified."});

db.news.insert({"title":"A Lotte Group spokesman","context":"A Lotte Group spokesman on Friday declined to comment on the reason for the raid, when asked whether it concerned a possible slush fund. He noted, however, that the situation was difficult given the IPO plans and Lotte Chemical's Axiall bid."});

db.news.insert({"title":"According","context":"According to bourse rules, the deadline for Hotel Lotte to list is July 27, six months from the preliminary approval for the IPO. If it needed to refile its prospectus to warn investors about risks from Friday's probe, which appeared likely, it would probably not be able to meet that deadline, an exchange official told Reuters on Friday."});

db.news.insert({"title":"Friday","context":"On Friday, dozens of Chinese tourists queued as usual to access elevators to the flagship Lotte Duty Free outlet in the group's headquarters complex, as TV cameras waited for investigators to emerge from office doors around the corner."});

db.news.insert({"title":"Named","context":"Named after the heroine of an 18th century Goethe novel, Lotte has grown from its founding in Japan 68 years ago as a maker of chewing gum to a corporate giant with interests ranging from hotels and retail to food and chemicals. The group has annual revenue of around $60 billion in Korea."});

db.news.insert({"title":"Hotel Lotte's","context":"Hotel Lotte's planned flotation of around 35 percent of its shares was intended to bring transparency and improve corporate governance at a group whose ownership structure is convoluted even by the opaque standards of South Korea's conglomerates."});

db.news.insert({"title":"Shares","context":"Shares in Lotte Shopping (023530.KS) , whose units Lotte Department Store and Lotte Home Shopping were raided, fell 1.6 percent on Friday. Lotte Himart (071840.KS) , a consumer electronics retailer, dropped 2.1 percent."});

> db.news.ensureIndex({"title":"text","context":"text"},{"weights":{"title":2,"context":1}})

{

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 1,

"numIndexesAfter" : 2,

"ok" : 1

}

> db.news.find({$text:{$search:"flotation"}})

{ "_id" : ObjectId("57aa8a28fe5f255ba4328c0e"), "title" : "Hotel Lotte's", "context" : "Hotel Lotte's planned flotation of around 35 percent of its shares was intended to bring transparency and improve corporate governance at a group whose ownership structure is convoluted even by the opaque standards of South Korea's conglomerates." }

>

每个集合上最多只能有一个全文本索引,但是全文本索引可以包含多个字段。全文索引与“普通”的多键索引不同,全文本索引中的字段顺序不重要:每个字段都被同等对待,可以为每个字段指定不同的权重来控制不同字段的相对重要性。

给title和context字段建立全文本索引,给title字段2的权重,context字段1的权重。(权重的范围可以是1~1,000,000,000,默认权重是1)

2d索引

2d索引也是MongoDB最常用的地理空间索引之一,用于游戏地图。2d索引用于扁平表面,而不是球体表面。如果用在球体表面上,在极点附近会出现大量的扭曲变形

> var map = [{

...   "gis" : {

...     "x" : 185,

...     "y" : 150

...   }

... },{

...   "gis" : {

...     "x" : 70,

...     "y" : 180

...   }

... },{

...   "gis" : {

...     "x" : 75,

...     "y" : 180

...   }

... },{

...   "gis" : {

...     "x" : 185,

...     "y" : 185

...   }

... },{

...   "gis" : {

...     "x" : 65,

...     "y" : 185

...   }

... },{

...   "gis" : {

...     "x" : 50,

...     "y" : 50

...   }

... },{

...   "gis" : {

...     "x" : 50,

...     "y" : 50

...   }

... },{

...   "gis" : {

...     "x" : 60,

...     "y" : 55

...   }

... },{

...   "gis" : {

...     "x" : 65,

...     "y" : 80

...   }

... },{

...   "gis" : {

...     "x" : 55,

...     "y" : 80

...   }

... },{

...   "gis" : {

...     "x" : 0,

...     "y" : 0

...   }

... },{

...   "gis" : {

...     "x" : 0,

...     "y" : 200

...   }

... },{

...   "gis" : {

...     "x" : 200,

...     "y" : 0

...   }

... },{

...   "gis" : {

...     "x" : 200,

...     "y" : 200

...   }

... }]

>

> for(var i=0;i<map.length;i++){ db.map.insert(map[i]) }

WriteResult({ "nInserted" : 1 })

> db.map.ensureIndex({gis:"2d"},{min:-1,max:201})

{

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 1,

"numIndexesAfter" : 2,

"ok" : 1

}

查询(70,180)最近的3个点

> db.map.find({"gis":{$near:[70,180]}}).limit(3)

{ "_id" : ObjectId("57ab8ac5893a4e222430fcc1"), "gis" : { "x" : 70, "y" : 180 } }

{ "_id" : ObjectId("57ab8ac5893a4e222430fcc2"), "gis" : { "x" : 75, "y" : 180 } }

{ "_id" : ObjectId("57ab8ac5893a4e222430fcc4"), "gis" : { "x" : 65, "y" : 185 } }

>

查询以点(50,50)和点(190,190)为对角线的正方形中的所有点:

> db.map.find({gis:{"$within":{$box:[[50,50],[190,190]]}}},{_id:0,gis:1})

{ "gis" : { "x" : 50, "y" : 50 } }

{ "gis" : { "x" : 50, "y" : 50 } }

{ "gis" : { "x" : 60, "y" : 55 } }

{ "gis" : { "x" : 55, "y" : 80 } }

{ "gis" : { "x" : 65, "y" : 80 } }

{ "gis" : { "x" : 65, "y" : 185 } }

{ "gis" : { "x" : 70, "y" : 180 } }

{ "gis" : { "x" : 75, "y" : 180 } }

{ "gis" : { "x" : 185, "y" : 150 } }

{ "gis" : { "x" : 185, "y" : 185 } }

查询出以(56,80)为圆心,半径为50,圆面积中的点,如下所示:

> db.map.find({gis:{"$within":{$center:[[56,80],50]}}},{_id:0,gis:1})

{ "gis" : { "x" : 50, "y" : 50 } }

{ "gis" : { "x" : 50, "y" : 50 } }

{ "gis" : { "x" : 60, "y" : 55 } }

{ "gis" : { "x" : 55, "y" : 80 } }

{ "gis" : { "x" : 65, "y" : 80 } }

查看三角形内的所有点,如下所示:

> db.map.find({"gis": {"$within":{"$polygon":[[50,70],[70,90],[90,70]]}} })

{ "_id" : ObjectId("57ab8ac5893a4e222430fcc8"), "gis" : { "x" : 65, "y" : 80 } }

>多边形,可以指定$polygon($ploygon接受一个多元素的数组,每个元素对应多边形的点)

本文转自 meteor_hy 51CTO博客,原文链接:http://blog.51cto.com/caiyuanji/1836850,如需转载请自行联系原作者

mongodb Index(3)相关推荐

  1. mongodb Index(2)

      复合索引 <接上> > 删除之前的collection,重新建立,如下所示: > db.person.drop() true > for(var i=0;i<2 ...

  2. 结合MongoDB开发LBS应用

    http://www.cnblogs.com/jifeng/p/4356052.html 然后列举一下需求: 1.实时性要高,有频繁的更新和读取 2.可按距离排序支持分页 3.支持多条件筛选(一个经纬 ...

  3. [Cacti] cacti监控mongodb性能实战

    前言: 为了更好的使用mongodb,须要监控出mongodb的一些基础使用情况,比方Flush数.连接数.内存使用率.Index操作.Slave延迟等等,这些能够通过配置cacti监控mongodb ...

  4. 结合MongoDB开发LBS应用(mongodb geo)

    简介 随着近几年各类移动终端的迅速普及,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理.我所在的项目也正从事相关系统的开发,我们使用的是S ...

  5. 详解基于MongoDB的地理位置查询,结合Symfony2演示

    简介 随着近几年各类移动终端的迅速普及,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理.我所在的项目也正从事相关系统的开发,我们使用的是S ...

  6. 2.windows安装mongodb企业版

    2.windows安装mongodb企业版 最新内容会在源站更新.转载请保留原文链接: http://dashidan.com/article/mongodb/index.html ① 下载Mongo ...

  7. 结合MongoDB开发LBS应用(转)

    原文链接:结合MongoDB开发LBS应用 简介 随着近几年各类移动终端的迅速普及,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理.我所在 ...

  8. MongoDB-概述:跨平台的面向文档的高性能高可用性易扩展数据库

    Table of Contents MongoDB-概述 数据库 采集 文件 样本文件 MongoDB教程 MongoDB-优势 MongoDB与RDBMS相比的优势 为什么要使用MongoDB? 在 ...

  9. 后端存储实战-极客时间

    电商系统是如何设计的 不要一上来就设计功能 这个系统是给谁用的? 这些人用来该系统解决什么问题? 电商:用户.运营.报表 购物流程:浏览商品-----加购------下单-------支付------ ...

最新文章

  1. Spring PropertyPlaceholderConfigurer
  2. 深入理解python的元组本身不可变性
  3. 教师计算机网络培训工作总结,教师培训工作的自我总结
  4. mysql 查询auto_increment_MySQL查询数据表的Auto_Increment(自增id)
  5. oracle硬盘亮黄灯,RH2288H V3服务器硬盘亮黄灯故障处理案例
  6. Bootstrap带下拉的胶囊导航
  7. 在51系列中data,idata,xdata,pdata的区别
  8. 我的一些学习经验:ONVIF
  9. html里面注释浮动框,“浮动”注释与HTML / CSS / JavaScript /任何
  10. Zabbix故障但是没有错误日志输出的一种解决办法
  11. 历经18年胡培松创制优良新种质 国稻种芯百团计划行动
  12. 历经30年,仍未解决通讯难题,水下机器人是虚假繁荣吗?
  13. API集成测试问题2:Expected status code 200 but received 500. Failed asserting that false is true.
  14. [网络安全]诸神之眼--Nmap的使用
  15. 简单的员工信息管理系统
  16. 关于office2013打开后始终显示正在配置问题的解决方案集锦
  17. 在 Oracle Enterprise Linux 和 iSCSI 上构建您自己的 Oracle RAC 11g 集群
  18. 【概率论与数理统计】猴博士 笔记 p15-16 一、二维连续型求概率
  19. Unity3D脚本中文教程(八)
  20. Facebook广告营销指南!Facebook广告投放技巧与策略!新手教程

热门文章

  1. Android 2.2 Froyo发布
  2. 什么是跨域?以及跨域的解决方案!
  3. 台式计算机网线接口松动怎么办,电脑网线接口附近时常有滋滋声是怎么回事
  4. jitter单位_抖动(jitter)测量
  5. AIoT应用创新大赛-基于TencentOS Tiny 的遥控小车
  6. RTX51tiny 延时长度计算
  7. iPhone开发播放音乐与按钮声音
  8. java计算机毕业设计康养旅游信息系统源程序+mysql+系统+lw文档+远程调试
  9. 密码学归约证明——定长对称加密密钥的敌手不可区分性
  10. ip addr 不显示ip地址