首页 文章

在ArangoDB中定义地理索引

提问于
浏览
1

我有一个如下所示的数据结构 . 如何为该结构定义地理索引?

{
  "currentGeoLocation": {
    "latitude": -10,
    "longitude": 1
  }
}

我试过这些命令:1-

db.test.ensureIndex({ type: "geo", fields: [ "currentGeoLocation[latitude]","currentGeoLocation[longitude]" ] });

2-

db.test.ensureIndex({ type: "geo", fields: [ "currentGeoLocation.latitude","currentGeoLocation.longitude" ] });

但我认为没有一个正常 . 因为在我搜索最近的物品后,我得到了[] . 有什么问题?

2 回答

  • 3

    您的第二个索引创建是正确的 .

    人们必须知道,纬度和经度不是实际公里单位 . 输入 [-10, 1]a distance calculator(这也是关于计算细节的一个很好的读数)告诉你 1117 km 远离 [0,0] . 由于ArangoDB中半径的单位是 m ,因此数字变大 .

    在实际搜索位置肯定会找到您的项目:

    db._query('FOR item IN WITHIN(test, -10, 1, 1) RETURN item').toArray()
    

    但是,从 [0,0] 搜索需要更大的半径:

    db._query('FOR item IN WITHIN(test, 0, 0, 1200000) RETURN item').toArray()
    [ 
      { 
        "_key" : "840", 
        "_id" : "test/840", 
        "_rev" : "840", 
        "currentGeoLocation" : { 
          "latitude" : -10, 
          "longitude" : 1 
        } 
      } 
    ]
    
  • 1

    继上一个答案之后,您使用的是arangosh界面还是arangodb js?

    我问,因为arangodb js界面不同 . 在这种情况下,你会看到类似的东西:

    db.collection('testnodes').createGeoIndex(['currentGeoLocation.latitude', 'currentGeoLocation.longitude'])
    

    此外,arangodb确实带有许多非常有用的Geolocation函数(near,within等),它们具有自动计算距离的辅助函数 .

相关问题