summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Aggregation/GeoDistance.php
blob: c50018a07444884bdd15857092d1716e736624dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Elastica\Aggregation;

use Elastica\Exception\InvalidException;

/**
 * Class GeoDistance.
 *
 * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html
 */
class GeoDistance extends AbstractAggregation
{
    const DISTANCE_TYPE_SLOPPY_ARC = 'sloppy_arc';
    const DISTANCE_TYPE_ARC = 'arc';
    const DISTANCE_TYPE_PLANE = 'plane';

    /**
     * @param string       $name   the name if this aggregation
     * @param string       $field  the field on which to perform this aggregation
     * @param string|array $origin the point from which distances will be calculated
     */
    public function __construct($name, $field, $origin)
    {
        parent::__construct($name);
        $this->setField($field)->setOrigin($origin);
    }

    /**
     * Set the field for this aggregation.
     *
     * @param string $field the name of the document field on which to perform this aggregation
     *
     * @return $this
     */
    public function setField($field)
    {
        return $this->setParam('field', $field);
    }

    /**
     * Set the origin point from which distances will be calculated.
     *
     * @param string|array $origin valid formats are array("lat" => 52.3760, "lon" => 4.894), "52.3760, 4.894", and array(4.894, 52.3760)
     *
     * @return $this
     */
    public function setOrigin($origin)
    {
        return $this->setParam('origin', $origin);
    }

    /**
     * Add a distance range to this aggregation.
     *
     * @param int $fromValue a distance
     * @param int $toValue   a distance
     *
     * @throws \Elastica\Exception\InvalidException
     *
     * @return $this
     */
    public function addRange($fromValue = null, $toValue = null)
    {
        if (is_null($fromValue) && is_null($toValue)) {
            throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
        }

        $range = array();

        if (!is_null($fromValue)) {
            $range['from'] = $fromValue;
        }

        if (!is_null($toValue)) {
            $range['to'] = $toValue;
        }

        return $this->addParam('ranges', $range);
    }

    /**
     * Set the unit of distance measure for  this aggregation.
     *
     * @param string $unit defaults to km
     *
     * @return $this
     */
    public function setUnit($unit)
    {
        return $this->setParam('unit', $unit);
    }

    /**
     * Set the method by which distances will be calculated.
     *
     * @param string $distanceType see DISTANCE_TYPE_* constants for options. Defaults to sloppy_arc.
     *
     * @return $this
     */
    public function setDistanceType($distanceType)
    {
        return $this->setParam('distance_type', $distanceType);
    }
}