Skip to content

Instantly share code, notes, and snippets.

@cbiggins
Created June 17, 2011 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbiggins/1031279 to your computer and use it in GitHub Desktop.
Save cbiggins/1031279 to your computer and use it in GitHub Desktop.
Simple geo targeted search with Sphinx
<?php
require_once('sphinxapi.php');
$sphinx = new sphinxClient;
$sphinx->SetServer('localhost',9312);
// How many k's from the target do we want to return results from?
// 10k's should do it. (In metres)
// @geodist is a magic key displaying metres from target.
$sphinx->SetFilterFloatRange('@geodist',0,10000);
// We'll base our search in Sydney CBD...
// $ search -i suburbs 'sydney' gives this...
// 3. document=188, weight=1, postcode=2000, lat=-0.591084, lon=2.639081
$sphinx->SetGeoAnchor('lat', 'lon', -0.591084, 2.639081);
// Sort by the distance from our target
$sphinx->SetSortMode(SPH_SORT_EXTENDED, "@geodist ASC");
// Run the query
$results = $sphinx->Query('', 'suburbs');
// Check the results.
dump($results['matches']);
function dump($s) {
print '<pre>';
print_r($s);
print '</pre>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment