GeoDistance.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Elastica\Aggregation;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Class GeoDistance.
  6. *
  7. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html
  8. */
  9. class GeoDistance extends AbstractAggregation
  10. {
  11. const DISTANCE_TYPE_ARC = 'arc';
  12. const DISTANCE_TYPE_PLANE = 'plane';
  13. /**
  14. * @param string $name the name if this aggregation
  15. * @param string $field the field on which to perform this aggregation
  16. * @param string|array $origin the point from which distances will be calculated
  17. */
  18. public function __construct($name, $field, $origin)
  19. {
  20. parent::__construct($name);
  21. $this->setField($field)->setOrigin($origin);
  22. }
  23. /**
  24. * Set the field for this aggregation.
  25. *
  26. * @param string $field the name of the document field on which to perform this aggregation
  27. *
  28. * @return $this
  29. */
  30. public function setField($field)
  31. {
  32. return $this->setParam('field', $field);
  33. }
  34. /**
  35. * Set the origin point from which distances will be calculated.
  36. *
  37. * @param string|array $origin valid formats are array("lat" => 52.3760, "lon" => 4.894), "52.3760, 4.894", and array(4.894, 52.3760)
  38. *
  39. * @return $this
  40. */
  41. public function setOrigin($origin)
  42. {
  43. return $this->setParam('origin', $origin);
  44. }
  45. /**
  46. * Add a distance range to this aggregation.
  47. *
  48. * @param int $fromValue a distance
  49. * @param int $toValue a distance
  50. *
  51. * @throws \Elastica\Exception\InvalidException
  52. *
  53. * @return $this
  54. */
  55. public function addRange($fromValue = null, $toValue = null)
  56. {
  57. if (is_null($fromValue) && is_null($toValue)) {
  58. throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
  59. }
  60. $range = [];
  61. if (!is_null($fromValue)) {
  62. $range['from'] = $fromValue;
  63. }
  64. if (!is_null($toValue)) {
  65. $range['to'] = $toValue;
  66. }
  67. return $this->addParam('ranges', $range);
  68. }
  69. /**
  70. * Set the unit of distance measure for this aggregation.
  71. *
  72. * @param string $unit defaults to km
  73. *
  74. * @return $this
  75. */
  76. public function setUnit($unit)
  77. {
  78. return $this->setParam('unit', $unit);
  79. }
  80. /**
  81. * Set the method by which distances will be calculated.
  82. *
  83. * @param string $distanceType see DISTANCE_TYPE_* constants for options. Defaults to arc.
  84. *
  85. * @return $this
  86. */
  87. public function setDistanceType($distanceType)
  88. {
  89. return $this->setParam('distance_type', $distanceType);
  90. }
  91. }