玩转mongodb(七):索引,速度的引领(全文索引、地理空间索引)

    本篇博文主要介绍MongoDB中一些常用的特殊索引类型,主要包括:

  •     用于简单字符串搜索的全文本索引;
  •     用于球体空间(2dsphere)和二维平面(2d)的地理空间索引。 

一、全文索引

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

    如果公司用的是免费版的MongoDB,而又需要用到中文的全文索引,建议使用lucene或者solr等开源项目来做。(没钱就得用技术来补,赤裸裸的现实。)

    使用全文本检索需要专门开启这个功能才能进行使用。启动MongoDB时指定--setParameter textSearchEnabled=true选项,或者在运行时执行setParameter命令,都可以启用全文本索引。

db.adminCommand({"setParameter":1,"textSearchEnabled":true});

    准备10条数据:

 1 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."});
 2 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."});
 3 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."});
 4 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."});
 5 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."});
 6 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."});
 7 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."});
 8 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."});
 9 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."});
10 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."});
全文索引的数据准备

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

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

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

    我们利用这个全文本索引来搜索一下。搜索的内容是“flotation”。

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

    结果如下图所示:

    

 

二、2dsphere索引

    2dsphere索引是MongoDB最常用的地理空间索引之一,用于地球表面类型的地图。允许使用GeoJSON格式(http://www.geojson.org)指定点、线、多边形。

    点可以用形如[longitude,latitude]([经度,纬度])的两个元素的数组表示("loc"字段的名字可以是任意的,但是其中的子对象是有GeoJSON指定的,不能改变):

{
    "name":"beijing",
    "loc":{
        "type":"Point",
        "coordinates":[40,2]
    }  
}

    线可以用一个由点组成的数组来表示:

{
    "name":"changjiang",
    "loc":{
        "type":"Line",
        "coordinates":[[1,2],[2,3],[3,4]]
    }  
}

    多边形的表示方式与线一样,但是“type”不同:

{
    "name":"shenzhen",
    "loc":{
        "type":"Polygon",
        "coordinates":[[1,2],[2,3],[3,4]]
    }  
}

    创建2dsphere索引: 

db.mapinfo.ensureIndex({"loc":"2dsphere"})

    地理空间查询的类型有三种:交集(intersection)包含(within)接近(nearness)。查询时,需要将希望查找的内容指定为形如{"$geometry":geoJsonDesc}的GeoJSON对象。

    使用“$geoIntersects”查询位置相交的文档:

var customMapinfo = {
    "type":"Polygon",
    "coordinates":[[12.2223,39,4424],[13.2223,38,4424],[13.2223,39,4424]]
}

db.mapinfo.find({
    "loc":{"$geoIntersects":{"$geometry":customMapinfo}} 
})

    这样就会找到所有与customMapinfo区域有交集的文档。

    使用“$within”查询完全包含在某个区域的文档:

db.mapinfo.find({
    "loc":{"$within":{"$geometry":customMapinfo}} 
})

    使用“$near”查询附近的位置:

db.mapinfo.find({
    "loc":{"$within":{"$geometry":customMapinfo}} 
})

 

三、2d索引

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

    文档中应该使用包含两个元素的数组表示2d索引字段。

{
    "name":"node1",
    "tile":[32,22]
}

    创建索引

db.gameMapinfo.ensureIndex({"tile":"2d"})

    使用$near查询点[20,20]附近的文档:

db.gameMapinfo.find({"tile":{"$near":[20,20]}})

    使用$within查询出某个形状(矩形、圆形或者多边形)范围内的所有文档。

    矩形,可以指定$box选项($box接受一个两元素的数组,第一个元素指定左下角的坐标,第二个元素指定右上角的坐标):

db.gameMapinfo.find({"tile":{"$within":{"$box":[[10,20],[15,30]]}}})

    圆形,可以指定$center选项($center接受一个两元素数组作为参数,第一个元素是一个点,用于指定圆心,第二个元素用于指定半径):

db.gameMapinfo.find({"tile":{"$within":{"$center":[[12,12],5]}}})

    多边形,可以指定$polygon($ploygon接受一个多元素的数组,每个元素对应多边形的点),下面以一个三角形为例:

db.gameMapinfo.find({"tile":{"$within":{"$polygon":[[0,20],[10,0],[-10,0]]}}})

 

 

  喜欢请微信扫描下面二维码,关注我公众号--“精修Java”,做一些实战项目中的问题和解决方案分享。 

posted @ 2016-06-12 10:32  壮壮熊  阅读(6055)  评论(2编辑  收藏  举报