Created
January 28, 2011 18:53
-
-
Save tjulien/800736 to your computer and use it in GitHub Desktop.
geohaystack example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/mongodb/mongo/blob/master/jstests/geo_haystack2.js | |
t = db.geo_haystack2 | |
t.drop() | |
function distance( a , b ){ | |
var x = a[0] - b[0]; | |
var y = a[1] - b[1]; | |
return Math.sqrt( ( x * x ) + ( y * y ) ); | |
} | |
function distanceTotal( a , arr , f ){ | |
var total = 0; | |
for ( var i=0; i<arr.length; i++ ){ | |
total += distance( a , arr[i][f] ); | |
} | |
return total; | |
} | |
queries = [ | |
{ near : [ 7 , 8 ] , maxDistance : 3 , search : { z : 3 } } , | |
] | |
answers = queries.map( function(){ return { totalDistance : 0 , results : [] }; } ) | |
n = 0; | |
for ( x=0; x<20; x++ ){ | |
for ( y=0; y<20; y++ ){ | |
t.insert( { _id : n , loc : [ x , y ] , z : [ n % 10 , ( n + 5 ) % 10 ] } ); | |
for ( i=0; i<queries.length; i++ ){ | |
var d = distance( queries[i].near , [ x , y ] ) | |
if ( d > queries[i].maxDistance ) | |
continue; | |
if ( queries[i].search.z != n % 10 && | |
queries[i].search.z != ( n + 5 ) % 10 ) | |
continue; | |
answers[i].results.push( { _id : n , loc : [ x , y ] } ) | |
answers[i].totalDistance += d; | |
} | |
n++; | |
} | |
} | |
t.ensureIndex( { loc : "geoHaystack" , z : 1 } , { bucketSize : .7 } ); | |
for ( i=0; i<queries.length; i++ ){ | |
print( "---------" ); | |
printjson( queries[i] ); | |
res = t.runCommand( "geoSearch" , queries[i] ) | |
print( "\t" + tojson( res.stats ) ); | |
print( "\tshould have: " + answers[i].results.length + "\t actually got: " + res.stats.n ); | |
assert.eq( answers[i].results.length , res.stats.n, "num:"+ i + " number matches" ) | |
assert.eq( answers[i].totalDistance , distanceTotal( queries[i].near , res.results , "loc" ), "num:"+ i + " totalDistance" ) | |
//printjson( res ); | |
//printjson( answers[i].length ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment