AliPrf
AliPrf

Reputation: 389

define geo index in ArangoDB

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?

Upvotes: 1

Views: 299

Answers (2)

Guido
Guido

Reputation: 37

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.

Upvotes: 1

dothebart
dothebart

Reputation: 6077

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

Upvotes: 3

Related Questions