|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Manticoresearch\Query; |
|
5
|
|
|
|
|
6
|
|
|
use Manticoresearch\Exceptions\RuntimeException; |
|
7
|
|
|
use Manticoresearch\Query; |
|
8
|
|
|
|
|
9
|
|
|
class Distance extends Query |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Distance constructor. |
|
13
|
|
|
* @param array $args |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct($args = []) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->params['geo_distance'] = []; |
|
18
|
|
|
$this->params['geo_distance']['distance_type'] = $args['type'] ?? 'adaptive'; |
|
19
|
|
|
if (count($args) > 0) { |
|
20
|
|
|
if (!isset($args['location_anchor'])) { |
|
21
|
|
|
throw new RuntimeException('anchors not provided'); |
|
22
|
|
|
} |
|
23
|
|
|
$this->params['geo_distance']['location_anchor'] = $args['location_anchor']; |
|
24
|
|
|
if (!isset($args['location_source'])) { |
|
25
|
|
|
throw new RuntimeException('source attributes not provided'); |
|
26
|
|
|
} |
|
27
|
|
|
if (is_array($args['location_source'])) { |
|
28
|
|
|
$args['location_source'] = implode(',', $args['location_source']); |
|
29
|
|
|
} |
|
30
|
|
|
$this->params['geo_distance']['location_source'] = $args['location_source']; |
|
31
|
|
|
|
|
32
|
|
|
if (!isset($args['location_distance'])) { |
|
33
|
|
|
throw new RuntimeException('distance not provided'); |
|
34
|
|
|
} |
|
35
|
|
|
$this->params['geo_distance']['distance'] = $args['location_distance']; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $distance the distance and it's units, e.g. 1000m, 200km |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setDistance($distance) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->params['geo_distance']['distance'] = $distance; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array|string $source Either an array or comma separated string of the fields to reference for lat & lon |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setSource($source) |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_array($source)) { |
|
53
|
|
|
$source = implode(',', $source); |
|
54
|
|
|
} |
|
55
|
|
|
$this->params['geo_distance']['location_source'] = $source; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the location of the anchor, namely the point by which distances will be measured from |
|
60
|
|
|
* |
|
61
|
|
|
* @param float $lat the latitude of the anchor |
|
62
|
|
|
* @param float $lon the longitude of the anchor |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setAnchor($lat, $lon) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->params['geo_distance']['location_anchor'] = ['lat' => $lat, 'lon' => $lon]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $algorithm the algorithm for distance measurement, either adaptive or haversine |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setDistanceType($algorithm) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$algorithm) { |
|
75
|
|
|
$algorithm = null; |
|
76
|
|
|
} |
|
77
|
|
|
$this->params['geo_distance']['distance_type'] = $algorithm ?? 'adaptive'; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|