首页 文章

Mongodb geohaystack 与标准 mongodb 空间索引相同吗?

提问于
浏览
1

似乎 mongodb 有两种类型的地理空间索引。

http://www.mongodb.org/display/DOCS/Geospatial 索引

标准的。附注:

目前,每个集合可能只有 1 个地理空间索引。虽然 MongoDB 可能允许创建多个索引,但不支持此行为。因为 MongoDB 只能使用一个索引来支持单个查询,所以在大多数情况下,拥有多个地理索引会产生不良行为。

然后有这个所谓的 geohaystack thingy。

http://www.mongodb.org/display/DOCS/Geospatial 干草堆索引

他们都声称使用相同的算法。他们都把地球变成了几个网格。然后根据它进行搜索。

那有什么不同呢?

Mongodb 似乎没有使用 Rtree 和东西吗?

注意:回答这个问题MongoDB 如何实现它的空间索引?说 2d 索引也使用 geohash。

1 回答

  • 3

    实现类似,但在地理空间干草堆索引页面上描述了用例差异。

    干草堆索引是针对 small-region longitude/latitude 搜索调整的“bucket-based”(又名“象限”)搜索:

    In addition to ordinary 2d geospatial indices, mongodb supports the use
        of bucket-based geospatial indexes. Called "Haystack indexing", these
        indices can accelerate small-region type longitude / latitude queries
        when additional criteria is also required.
    
        For example, "find all restaurants within 25 miles with name 'foo'".
    
        Haystack indices allow you to tune your bucket size to the distribution
        of your data, so that in general you search only very small regions of
        2d space for a particular kind of document.  They are not suited for
        finding the closest documents to a particular location, when the
        closest documents are far away compared to bucket size.
    

    bucketSize参数是必需的,并确定 haystack 索引的粒度。

    所以,例如:

    db.places.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })
    

    此示例 bucketSize 为 1 创建一个索引,其中 1 个经度或纬度单位的密钥存储在同一个存储桶中。索引中还可以包含其他类别,这意味着将在查找位置详细信息的同时查找信息。

    B-tree 表示类似于:

    { loc: "x,y", category: z }
    

    如果您的用例通常搜索“附近”位置(i.e.“25 英里范围内的餐馆”),则干草堆索引可能更有效。可以在每个桶中找到并计算附加索引字段(例如,类别)的匹配。

    相反,如果您正在搜索“最近的餐厅”并且想要返回结果而不考虑距离,那么正常的 2d 索引将更有效。

    目前(截至 MongoDB 2.2.0)对干草堆索引有一些限制:

    • haystack 索引中只能包含一个附加字段

    • 附加索引字段必须是单个值,而不是数组

    • 不支持 null long/lat 值

    注意:纬度之间的距离会有很大差异(经度,不那么)。见:纬度和经度之间的距离是多少?

相关问题