define geo index in ArangoDB

倖福魔咒の 提交于 2019-12-11 12:15:52

问题


I have a data structure like below. how can I define a geo index for that structure?

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

I tried these command: 1-

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

2-

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

but I think none works correctly. because after I search the nearest items, I got []. whats the problem?


回答1:


Your second index creation is the correct one.

One has to know, that latitude and longtitude aren't actualy km units. Entering [-10, 1] into a distance calculator (which also is a good read about the calculation details) tells you its 1117 km away from [0,0]. Since the unit of the radius is m in ArangoDB, the numbers get big.

Searching at the actual spot will for sure find your item:

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

But, searching from [0,0] requires a bigger radius:

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 
    } 
  } 
]



回答2:


Following on from the previous answer, are you using the arangosh interface or arangodb js?

I ask as the arangodb js interface is different. In this case you would look at something like:

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

Also, arangodb does then come with a number of really quite useful Geolocation functions (near, within etc) which have a helper function to automatically calculate the distance.



来源:https://stackoverflow.com/questions/37225637/define-geo-index-in-arangodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!