1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/search |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[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 Gpupo\Search; |
13
|
|
|
|
14
|
|
|
use Gpupo\Search\Result\CountableCollection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Comunicacao com o SphinxClient. |
18
|
|
|
*/ |
19
|
|
|
abstract class SearchAbstract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Facade para Single query, obtendo apenas os resultados (matches). |
23
|
|
|
* |
24
|
|
|
* @param string $index |
25
|
|
|
*/ |
26
|
|
|
public function search($index, array $filters = null, |
27
|
|
|
array $queries = null, array $fieldWeights = null, |
28
|
|
|
$limit = 20, $offset = 0 |
29
|
|
|
) { |
30
|
|
|
$result = $this->query($index, $filters, $queries, |
31
|
|
|
$fieldWeights, $limit, $offset); |
32
|
|
|
|
33
|
|
|
if (!isset($result['matches'])) { |
34
|
|
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $result['matches']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Executa Queries. |
42
|
|
|
* |
43
|
|
|
* @see \Gpupo\Search\Query\FiltersAbstract::toArray() Filter Array Sintaxe |
44
|
|
|
* @see \Gpupo\Search\Query\QueryAbstract::getQueries() Query Array Sintaxe |
45
|
|
|
* @see \Gpupo\Search\Query\QueryAbstract::getFieldWeights() Query Array Sintaxe |
46
|
|
|
* |
47
|
|
|
* @param array $filter Search filter |
|
|
|
|
48
|
|
|
* @param array $queries Search query |
49
|
|
|
* @param array $fieldWeights Field weights array |
50
|
|
|
* @param int $limit |
51
|
|
|
* @param int $offset |
52
|
|
|
* |
53
|
|
|
* @return array Results |
54
|
|
|
*/ |
55
|
|
|
public function query($index, array $filters = null, |
56
|
|
|
array $queries = null, array $fieldWeights = null, |
57
|
|
|
$limit = 20, $offset = 0 |
58
|
|
|
) { |
59
|
|
|
$sphinxClient = $this->getSphinxClient(); |
60
|
|
|
$sphinxClient->SetLimits($offset, $limit); |
61
|
|
|
if (null !== $filters) { |
62
|
|
|
foreach ($filters as $filter) { |
63
|
|
|
if (!isset($filter['key'])) { |
|
|
|
|
64
|
|
|
// Filtro existe mas sem key |
65
|
|
|
} |
66
|
|
|
if ( |
67
|
|
|
array_key_exists('min', $filter) && |
68
|
|
|
array_key_exists('max', $filter) |
69
|
|
|
) { |
70
|
|
|
$sphinxClient->SetFilterRange( |
71
|
|
|
$filter['key'], |
72
|
|
|
(integer) $filter['min'], |
73
|
|
|
(integer) $filter['max'] |
74
|
|
|
); |
75
|
|
|
} else { |
76
|
|
|
if (!isset($filter['values']) || !is_array($filter['values'])) { |
|
|
|
|
77
|
|
|
//Filtro existe mas sem valor; |
78
|
|
|
} |
79
|
|
|
$sphinxClient->SetFilter( |
80
|
|
|
$filter['key'], |
81
|
|
|
$filter['values'] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
if (null !== $queries) { |
87
|
|
|
foreach ($queries as $key => $queryInfo) { |
88
|
|
|
$query = $this->implodeQueryValues($queryInfo); |
89
|
|
|
|
90
|
|
|
if (array_key_exists('countableAttributes', $queryInfo)) { |
91
|
|
|
$array = $queryInfo['countableAttributes']; |
92
|
|
|
if (!is_array($array)) { |
93
|
|
|
$array = [$array]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$sphinxClient->addFacetedQuery($query, $index, $array); |
97
|
|
|
} else { |
98
|
|
|
$sphinxClient->AddQuery($query, $index); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (null !== $fieldWeights) { |
104
|
|
|
$sphinxClient->SetFieldWeights($fieldWeights); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$result = $this->getResult($sphinxClient); |
108
|
|
|
|
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* RunQueries() + validate. |
114
|
|
|
* |
115
|
|
|
* - Single Query: Resultados da Query |
116
|
|
|
* |
117
|
|
|
* - Multi Query: Array de Resultados das Querys |
118
|
|
|
* |
119
|
|
|
* Formato de cada Resultado: |
120
|
|
|
* |
121
|
|
|
* <code> |
122
|
|
|
* //Results |
123
|
|
|
* array( |
124
|
|
|
* array( |
125
|
|
|
* 'id' => 12345, |
126
|
|
|
* 'weight' => 30, |
127
|
|
|
* 'attrs' => array(...) |
128
|
|
|
* ), |
129
|
|
|
* array( |
130
|
|
|
* 'id' => 23456, |
131
|
|
|
* 'weight' => 20, |
132
|
|
|
* 'attrs' => array(...) |
133
|
|
|
* ), |
134
|
|
|
* ... |
135
|
|
|
* ); |
136
|
|
|
* </code> |
137
|
|
|
* |
138
|
|
|
* @param \SphinxClient $sphinxClient |
139
|
|
|
* |
140
|
|
|
* @throws \Exception |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function getResult(\SphinxClient $sphinxClient) |
145
|
|
|
{ |
146
|
|
|
$result = $sphinxClient->RunQueries(); |
147
|
|
|
|
148
|
|
|
if (false === $result) { |
149
|
|
|
throw new \Exception( |
150
|
|
|
$sphinxClient->getLastError() |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
if ($sphinxClient->GetLastWarning()) { |
154
|
|
|
throw new \Exception( |
155
|
|
|
$sphinxClient->GetLastWarning() |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (false === $result) { |
160
|
|
|
throw new \Exception( |
161
|
|
|
$sphinxClient->getLastError() |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
if ($sphinxClient->GetLastWarning()) { |
165
|
|
|
throw new \Exception( |
166
|
|
|
$sphinxClient->GetLastWarning() |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
//Suporte ao formato inicial de unica query |
171
|
|
|
if (count($result) === 1) { |
172
|
|
|
return current($result); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $result; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Transforma uma query array em uma string usada na |
180
|
|
|
* query do Client Sphinx Search. |
181
|
|
|
* |
182
|
|
|
* @param array $queryInfo |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
protected function implodeQueryValues(array $queryInfo) |
187
|
|
|
{ |
188
|
|
|
$query = "@{$queryInfo['key']} " |
189
|
|
|
.( |
190
|
|
|
'*'.implode('* *', $queryInfo['values']) |
191
|
|
|
.'*' |
192
|
|
|
).PHP_EOL; |
193
|
|
|
|
194
|
|
|
return $query; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Facade para query, obtendo resultados em objeto. |
199
|
|
|
*/ |
200
|
|
|
public function getCollection($index, array $filters = null, |
201
|
|
|
array $queries = null, array $fieldWeights = null, |
202
|
|
|
$limit = 20, $offset = 0, $countableAttributes = null |
203
|
|
|
) { |
204
|
|
|
$result = $this->query($index, $filters, $queries, |
205
|
|
|
$fieldWeights, $limit, $offset); |
206
|
|
|
|
207
|
|
|
if (is_array($result)) { |
208
|
|
|
$i = 0; |
209
|
|
|
|
210
|
|
|
if ($countableAttributes) { |
211
|
|
|
foreach ($countableAttributes as $attributeName) { |
212
|
|
|
$i++; |
213
|
|
|
$result[0]['attributes']['countable'][$attributeName] = new CountableCollection($result[$i], $attributeName); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
for ($l = 1; $l <= $i; $l++) { |
218
|
|
|
unset($result[$l]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$collection = $this->factoryCollection($result); |
222
|
|
|
|
223
|
|
|
return $collection; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.