Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <?php
  2.  
  3. // some geo locations
  4. $brandenburgerTor = array(
  5. 'lat' => 52.516455903398,
  6. 'lon' => 13.380317687988
  7. );
  8.  
  9. $friedrichstrasse = array(
  10. 'lat' => 52.519772437396,
  11. 'lon' => 13.388257026672
  12. );
  13.  
  14. $alex = array(
  15. 'lat' => 52.521548116471,
  16. 'lon' => 13.410186767578
  17. );
  18.  
  19. // setting up the client, index and type
  20. $client = new Elastica_Client();
  21.  
  22. $index = $client->getIndex('test');
  23.  
  24. $index->create(array(), true);
  25.  
  26. $type = $index->getType('product');
  27.  
  28. // mapping
  29. $type->setMapping(array(
  30. 'title' => array('type' => 'string', 'store' => 'yes'),
  31. 'location' => array('type' => 'geo_point', 'store' => 'yes'),
  32. ));
  33.  
  34.  
  35. // create two documents of two different locations
  36.  
  37. $data = array(
  38. 'title' => 'Product A',
  39. 'location' => $brandenburgerTor,
  40. );
  41.  
  42. $docs = array();
  43.  
  44. $docs[] = new Elastica_Document('doc1', $data);
  45.  
  46. $data['title'] = 'Product B';
  47. $data['location'] = $alex;
  48.  
  49. $docs[] = new Elastica_Document('doc2', $data);
  50.  
  51. $type->addDocuments($docs);
  52.  
  53. // give elasticsearch some time to index
  54. sleep(2);
  55.  
  56. // do the query using a geo location in the near of the documents
  57.  
  58. $filter = new Elastica_Filter_GeoDistance('product.location', $friedrichstrasse['lat'], $friedrichstrasse['lon'], '100km');
  59. $all = new Elastica_Query_MatchAll();
  60. $query = new Elastica_Query_Filtered($all, $filter);
  61.  
  62.  
  63. $results = $index->search($query);
  64.  
  65. foreach ($results as $result)
  66. {
  67. echo $result->getId()."\n";
  68. }
  69.  
  70. if (0 == count($results))
  71. {
  72. echo "nothing found\n";
  73. }
Add Comment
Please, Sign In to add comment