-
-
Save fabian/1195505 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
// some geo locations | |
$brandenburgerTor = array( | |
'lat' => 52.516455903398, | |
'lon' => 13.380317687988 | |
); | |
$friedrichstrasse = array( | |
'lat' => 52.519772437396, | |
'lon' => 13.388257026672 | |
); | |
$alex = array( | |
'lat' => 52.521548116471, | |
'lon' => 13.410186767578 | |
); | |
// setting up the client, index and type | |
$client = new Elastica_Client(); | |
$index = $client->getIndex('test'); | |
$index->create(array(), true); | |
$type = $index->getType('product'); | |
// mapping | |
$type->setMapping(array( | |
'title' => array('type' => 'string', 'store' => 'yes'), | |
'location' => array('type' => 'geo_point', 'store' => 'yes'), | |
)); | |
// create two documents of two different locations | |
$data = array( | |
'title' => 'Product A', | |
'location' => $brandenburgerTor, | |
); | |
$docs = array(); | |
$docs[] = new Elastica_Document('doc1', $data); | |
$data['title'] = 'Product B'; | |
$data['location'] = $alex; | |
$docs[] = new Elastica_Document('doc2', $data); | |
$type->addDocuments($docs); | |
// give elasticsearch some time to index | |
sleep(2); | |
// do the query using a geo location in the near of the documents | |
$filter = new Elastica_Filter_GeoDistance('product.location', $friedrichstrasse['lat'], $friedrichstrasse['lon'], '100km'); | |
$all = new Elastica_Query_MatchAll(); | |
$query = new Elastica_Query_Filtered($all, $filter); | |
$results = $index->search($query); | |
foreach ($results as $result) | |
{ | |
echo $result->getId()."\n"; | |
} | |
if (0 == count($results)) | |
{ | |
echo "nothing found\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment