1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Query\Geo; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
15
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents Elasticsearch "geo_shape" query. |
19
|
|
|
* |
20
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html |
21
|
|
|
*/ |
22
|
|
|
class GeoShapeQuery implements BuilderInterface |
23
|
|
|
{ |
24
|
|
|
use ParametersTrait; |
25
|
|
|
|
26
|
|
|
const INTERSECTS = 'intersects'; |
27
|
|
|
const DISJOINT = 'disjoint'; |
28
|
|
|
const WITHIN = 'within'; |
29
|
|
|
const CONTAINS = 'contains'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $fields = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $parameters |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $parameters = []) |
40
|
|
|
{ |
41
|
|
|
$this->setParameters($parameters); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getType() |
48
|
|
|
{ |
49
|
|
|
return 'geo_shape'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add geo-shape provided filter. |
54
|
|
|
* |
55
|
|
|
* @param string $field Field name. |
56
|
|
|
* @param string $type Shape type. |
57
|
|
|
* @param array $coordinates Shape coordinates. |
58
|
|
|
* @param string $relation Spatial relation. |
59
|
|
|
* @param array $parameters Additional parameters. |
60
|
|
|
*/ |
61
|
|
|
public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []) |
62
|
|
|
{ |
63
|
|
|
// TODO: remove this in the next major version |
64
|
|
View Code Duplication |
if (is_array($relation)) { |
|
|
|
|
65
|
|
|
$parameters = $relation; |
66
|
|
|
$relation = self::INTERSECTS; |
67
|
|
|
trigger_error('$parameters as parameter 4 in addShape is deprecated', E_USER_DEPRECATED); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$filter = array_merge( |
71
|
|
|
$parameters, |
72
|
|
|
[ |
73
|
|
|
'type' => $type, |
74
|
|
|
'coordinates' => $coordinates, |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$this->fields[$field] = [ |
79
|
|
|
'shape' => $filter, |
80
|
|
|
'relation' => $relation, |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add geo-shape pre-indexed filter. |
86
|
|
|
* |
87
|
|
|
* @param string $field Field name. |
88
|
|
|
* @param string $id The ID of the document that containing the pre-indexed shape. |
89
|
|
|
* @param string $type Name of the index where the pre-indexed shape is. |
90
|
|
|
* @param string $index Index type where the pre-indexed shape is. |
91
|
|
|
* @param string $relation Spatial relation. |
92
|
|
|
* @param string $path The field specified as path containing the pre-indexed shape. |
93
|
|
|
* @param array $parameters Additional parameters. |
94
|
|
|
*/ |
95
|
|
|
public function addPreIndexedShape( |
96
|
|
|
$field, |
97
|
|
|
$id, |
98
|
|
|
$type, |
99
|
|
|
$index, |
100
|
|
|
$path, |
101
|
|
|
$relation = self::INTERSECTS, |
102
|
|
|
array $parameters = [] |
103
|
|
|
) { |
104
|
|
|
// TODO: remove this in the next major version |
105
|
|
View Code Duplication |
if (is_array($relation)) { |
106
|
|
|
$parameters = $relation; |
107
|
|
|
$relation = self::INTERSECTS; |
108
|
|
|
trigger_error('$parameters as parameter 6 in addShape is deprecated', E_USER_DEPRECATED); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$filter = array_merge( |
112
|
|
|
$parameters, |
113
|
|
|
[ |
114
|
|
|
'id' => $id, |
115
|
|
|
'type' => $type, |
116
|
|
|
'index' => $index, |
117
|
|
|
'path' => $path, |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$this->fields[$field] = [ |
122
|
|
|
'indexed_shape' => $filter, |
123
|
|
|
'relation' => $relation, |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function toArray() |
131
|
|
|
{ |
132
|
|
|
$output = $this->processArray($this->fields); |
133
|
|
|
|
134
|
|
|
return [$this->getType() => $output]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.