1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Geo; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Search\Traits\HasField; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Filters documents that include only hits that exists within a specific distance from a geo point. |
7
|
|
|
* |
8
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html |
9
|
|
|
*/ |
10
|
|
|
class GeoDistanceQuery extends AbstractQuery |
11
|
|
|
{ |
12
|
|
|
use HasField; |
13
|
|
|
|
14
|
|
|
public const DISTANCE_TYPE_SLOPPY_ARC = 'sloppy_arc'; |
15
|
|
|
public const DISTANCE_TYPE_ARC = 'arc'; |
16
|
|
|
public const DISTANCE_TYPE_PLANE = 'plane'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
private $latitude; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
private $longitude; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string The radius of the circle centred on the specified location. Points which fall into this circle are |
30
|
|
|
* considered to be matches. The distance can be specified in various units. |
31
|
|
|
* |
32
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#distance-units |
33
|
|
|
*/ |
34
|
|
|
private $distance; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ?string How to compute the distance. Can either be sloppy_arc (default), arc (slightly more precise but |
38
|
|
|
* significantly slower) or plane (faster, but inaccurate on long distances and close to the poles). |
39
|
|
|
*/ |
40
|
|
|
private $distanceType; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function toArray() |
47
|
|
|
{ |
48
|
|
|
$geoDistance = [ |
49
|
|
|
'distance' => $this->getDistance(), |
50
|
|
|
$this->getField() => [ |
51
|
|
|
'lat' => $this->getLatitude(), |
52
|
|
|
'lon' => $this->getLongitude(), |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$distanceType = $this->getDistanceType(); |
57
|
|
|
if (null !== $distanceType) { |
58
|
|
|
$geoDistance['distance_type'] = $distanceType; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return ['geo_distance' => $geoDistance]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $latitude |
67
|
|
|
* @param mixed $longitude |
68
|
|
|
* @return GeoDistanceQuery |
69
|
|
|
*/ |
70
|
|
|
public function setLocation($latitude, $longitude) |
71
|
|
|
{ |
72
|
|
|
$this->latitude = $latitude; |
73
|
|
|
$this->longitude = $longitude; |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function getLongitude() |
82
|
|
|
{ |
83
|
|
|
return $this->longitude; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function getLatitude() |
91
|
|
|
{ |
92
|
|
|
return $this->latitude; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $distance |
98
|
|
|
* @return GeoDistanceQuery |
99
|
|
|
*/ |
100
|
|
|
public function setDistance($distance) |
101
|
|
|
{ |
102
|
|
|
$this->distance = $distance; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getDistance() |
111
|
|
|
{ |
112
|
|
|
return $this->distance; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $distanceType |
118
|
|
|
* @return GeoDistanceQuery |
119
|
|
|
*/ |
120
|
|
|
public function setDistanceType($distanceType) |
121
|
|
|
{ |
122
|
|
|
$this->distanceType = $distanceType; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string|null |
129
|
|
|
*/ |
130
|
|
|
public function getDistanceType(): ?string |
131
|
|
|
{ |
132
|
|
|
return $this->distanceType; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|