Created
September 4, 2011 19:12
-
-
Save mahono/1193362 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( | |
'product' => array( | |
'properties' => array( | |
'title' => array('type' => 'string', 'store' => 'yes'), | |
'location' => array('type' => 'geo_point', 'store' => 'yes') | |
) | |
) | |
)); | |
// create two documents of two different locations | |
$data = array( | |
'product' => array( | |
'title' => 'Product A', | |
'location' => $brandenburgerTor | |
) | |
); | |
$docs = array(); | |
$docs[] = new Elastica_Document('doc1', $data); | |
$data['product']['title'] = 'Product B'; | |
$data['product']['location'] = $alex; | |
$docs[] = new Elastica_Document('doc2', $data); | |
$type->addDocuments($docs); | |
// 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"; | |
} | |
// problem is: nothing is found, but why? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment