Newer
Older
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
105
106
107
108
109
110
111
112
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\DSL\Sort;
/**
* A special type of sorting by distance.
*/
class GeoSort extends AbstractSort
{
/**
* Possible types.
*
* Examples:
* [-70, 40]
* ["lat" : 40, "lon" : -70]
* "-70,40"
*
* @var string|array
*/
protected $location;
/**
* Units in which to measure distance.
*
* @var string
*/
protected $unit;
/**
* Constructor for geo sort.
*
* @param string $field Field name.
* @param array|string $location Possible types examples:
* [-70, 40]
* ["lat" : 40, "lon" : -70]
* "-70,40".
* @param string $order Order.
* @param string $unit Units for measuring the distance.
* @param string $mode Mode.
*/
public function __construct($field, $location, $order = self::ORDER_DESC, $unit = null, $mode = null)
{
$this->setLocation($location);
$this->setUnit($unit);
parent::__construct($field, $order, $mode);
}
/**
* @param string $unit
*/
public function setUnit($unit)
{
$this->unit = $unit;
}
/**
* @return string
*/
public function getUnit()
{
return $this->unit;
}
/**
* @param array|string $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* @return array|string
*/
public function getLocation()
{
return $this->location;
}
/**
* @return string
*/
final public function getType()
{
return '_geo_distance';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$value = parent::toArray();
if ($this->getUnit() !== null) {
$value['unit'] = $this->getUnit();
}
$value[$this->getField()] = $this->getLocation();
return $value;
}
}