@@ -12,7 +12,7 @@ |
||
12 | 12 | */ |
13 | 13 | class Elastic { |
14 | 14 | |
15 | - static function client($config =[]) { |
|
15 | + static function client($config = []) { |
|
16 | 16 | static $client; |
17 | 17 | if (!empty($client)) { |
18 | 18 | return $client; |
@@ -18,8 +18,7 @@ discard block |
||
18 | 18 | foreach ($agg['buckets'] as $bucket) { |
19 | 19 | $res = array_merge($res, $this->getResultFromBucket($bucket, $group_field)); |
20 | 20 | } |
21 | - } |
|
22 | - else { |
|
21 | + } else { |
|
23 | 22 | $res[] = [$group_field => $agg['value']]; |
24 | 23 | } |
25 | 24 | } |
@@ -91,8 +90,7 @@ discard block |
||
91 | 90 | $group_by[$group['field']]['terms']['field'] = $group['field']; |
92 | 91 | if ($prev) { |
93 | 92 | $group_by[$group['field']]['aggs'] = $prev; |
94 | - } |
|
95 | - elseif (!empty($aggregates)) { |
|
93 | + } elseif (!empty($aggregates)) { |
|
96 | 94 | $group_by[$group['field']]['aggs'] = $aggregates; |
97 | 95 | |
98 | 96 | // Sort by aggregate values |
@@ -119,8 +117,7 @@ discard block |
||
119 | 117 | $prev = $group_by; |
120 | 118 | } |
121 | 119 | $params['body']['aggs'] = $group_by; |
122 | - } |
|
123 | - elseif (!empty($aggregates)) { |
|
120 | + } elseif (!empty($aggregates)) { |
|
124 | 121 | // Aggregate without grouping. Will only ever produce one result since it's aggregating on the whole dataset. |
125 | 122 | $params['body']['aggs'] = $aggregates; |
126 | 123 | } |
@@ -122,8 +122,8 @@ |
||
122 | 122 | |
123 | 123 | // For regular (non-count) queries |
124 | 124 | if (!$this->count) { |
125 | - // Don't include source |
|
126 | - $params['body']['_source'] = false; |
|
125 | + // Don't include source |
|
126 | + $params['body']['_source'] = false; |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | // Top-level condition |
@@ -260,19 +260,19 @@ |
||
260 | 260 | } |
261 | 261 | elseif ($operator == "ENDS_WITH" && $conjunction == "AND") { |
262 | 262 | //TODO: WARNING THIS IS EXPENSIVE |
263 | - $bool['filter'][] = ['wildcard' => [$field => '*'.$value]]; |
|
263 | + $bool['filter'][] = ['wildcard' => [$field => '*' . $value]]; |
|
264 | 264 | } |
265 | 265 | elseif ($operator == "ENDS_WITH" && $conjunction == "OR") { |
266 | 266 | //TODO: WARNING THIS IS EXPENSIVE |
267 | - $bool['should'][] = ['wildcard' => [$field => '*'.$value]]; |
|
267 | + $bool['should'][] = ['wildcard' => [$field => '*' . $value]]; |
|
268 | 268 | } |
269 | 269 | elseif ($operator == "CONTAINS" && $conjunction == "AND") { |
270 | 270 | //TODO: WARNING THIS IS EXPENSIVE |
271 | - $bool['filter'][] = ['wildcard' => [$field => '*'.$value.'*']]; |
|
271 | + $bool['filter'][] = ['wildcard' => [$field => '*' . $value . '*']]; |
|
272 | 272 | } |
273 | 273 | elseif ($operator == "CONTAINS" && $conjunction == "OR") { |
274 | 274 | //TODO: WARNING THIS IS EXPENSIVE |
275 | - $bool['should'][] = ['wildcard' => [$field => '*'.$value.'*']]; |
|
275 | + $bool['should'][] = ['wildcard' => [$field => '*' . $value . '*']]; |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 |
@@ -79,8 +79,7 @@ discard block |
||
79 | 79 | |
80 | 80 | if ($this->count) { |
81 | 81 | return $result['count']; |
82 | - } |
|
83 | - else { |
|
82 | + } else { |
|
84 | 83 | // TODO: support for revisions? See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Query%21QueryInterface.php/function/QueryInterface%3A%3Aexecute/8.2.x |
85 | 84 | $ids = array(); |
86 | 85 | foreach ($result['hits']['hits'] as $hit) { |
@@ -95,8 +94,7 @@ discard block |
||
95 | 94 | $params = $this->buildRequest(); |
96 | 95 | if ($this->count) { |
97 | 96 | $result = $this->client->count($params); |
98 | - } |
|
99 | - else { |
|
97 | + } else { |
|
100 | 98 | $result = $this->client->search($params); |
101 | 99 | } |
102 | 100 | |
@@ -173,104 +171,75 @@ discard block |
||
173 | 171 | |
174 | 172 | if (is_object($field) && $conjunction == "AND") { |
175 | 173 | $bool['filter'][] = ['bool' => $this->getElasticFilterItem($field)]; |
176 | - } |
|
177 | - elseif (is_object($field) && $conjunction == "AND") { |
|
174 | + } elseif (is_object($field) && $conjunction == "AND") { |
|
178 | 175 | $bool['should'][] = ['bool' => $this->getElasticFilterItem($field)]; |
179 | - } |
|
180 | - elseif ($operator == "=" && $conjunction == "AND") { |
|
176 | + } elseif ($operator == "=" && $conjunction == "AND") { |
|
181 | 177 | $bool['filter'][] = ['term' => [$field => $value]]; |
182 | - } |
|
183 | - elseif ($operator == "=" && $conjunction == "OR") { |
|
178 | + } elseif ($operator == "=" && $conjunction == "OR") { |
|
184 | 179 | $bool['should'][] = ['term' => [$field => $value]]; |
185 | - } |
|
186 | - elseif (($operator == "<>" || $operator == "!=") && $conjunction == "AND") { |
|
180 | + } elseif (($operator == "<>" || $operator == "!=") && $conjunction == "AND") { |
|
187 | 181 | $bool['must_not'][] = ['term' => [$field => $value]]; |
188 | - } |
|
189 | - elseif (($operator == "<>" || $operator == "!=") && $conjunction == "OR") { |
|
182 | + } elseif (($operator == "<>" || $operator == "!=") && $conjunction == "OR") { |
|
190 | 183 | $bool['should'][] = ['bool' => ['must_not' => ['term' => [$field => $value]]]]; |
191 | - } |
|
192 | - elseif ($operator == "IN" && $conjunction == "AND") { |
|
184 | + } elseif ($operator == "IN" && $conjunction == "AND") { |
|
193 | 185 | $bool['filter'][] = ['terms' => [$field => $value]]; |
194 | - } |
|
195 | - elseif ($operator == "IN" && $conjunction == "OR") { |
|
186 | + } elseif ($operator == "IN" && $conjunction == "OR") { |
|
196 | 187 | $bool['should'][] = ['terms' => [$field => $value]]; |
197 | - } |
|
198 | - elseif ($operator == "NOT IN" && $conjunction == "AND") { |
|
188 | + } elseif ($operator == "NOT IN" && $conjunction == "AND") { |
|
199 | 189 | $bool['must_not'][] = ['terms' => [$field => $value]]; |
200 | - } |
|
201 | - elseif ($operator == "NOT IN" && $conjunction == "OR") { |
|
190 | + } elseif ($operator == "NOT IN" && $conjunction == "OR") { |
|
202 | 191 | $bool['should'][] = ['bool' => ['must_not' => ['terms' => [$field => $value]]]]; |
203 | - } |
|
204 | - elseif ($operator == "IS NULL" && $conjunction == "AND") { |
|
192 | + } elseif ($operator == "IS NULL" && $conjunction == "AND") { |
|
205 | 193 | $bool['must_not'][] = ['exists' => ['field' => $field]]; |
206 | - } |
|
207 | - elseif ($operator == "IS NULL" && $conjunction == "OR") { |
|
194 | + } elseif ($operator == "IS NULL" && $conjunction == "OR") { |
|
208 | 195 | $bool['should'][] = ['bool' => ['must_not' => ['exists' => ['field' => $field]]]]; |
209 | - } |
|
210 | - elseif ($operator == "IS NOT NULL" && $conjunction == "AND") { |
|
196 | + } elseif ($operator == "IS NOT NULL" && $conjunction == "AND") { |
|
211 | 197 | $bool['filter'][] = ['exists' => ['field' => $field]]; |
212 | - } |
|
213 | - elseif ($operator == "IS NOT NULL" && $conjunction == "OR") { |
|
198 | + } elseif ($operator == "IS NOT NULL" && $conjunction == "OR") { |
|
214 | 199 | $bool['should'][] = ['exists' => ['field' => $field]]; |
215 | - } |
|
216 | - elseif ($operator == ">" && $conjunction == "AND") { |
|
200 | + } elseif ($operator == ">" && $conjunction == "AND") { |
|
217 | 201 | $bool['filter'][] = ['range' => [$field => ['gt' => $value]]]; |
218 | - } |
|
219 | - elseif ($operator == ">" && $conjunction == "OR") { |
|
202 | + } elseif ($operator == ">" && $conjunction == "OR") { |
|
220 | 203 | $bool['should'][] = ['range' => [$field => ['gt' => $value]]]; |
221 | - } |
|
222 | - elseif ($operator == ">=" && $conjunction == "AND") { |
|
204 | + } elseif ($operator == ">=" && $conjunction == "AND") { |
|
223 | 205 | $bool['filter'][] = ['range' => [$field => ['gte' => $value]]]; |
224 | - } |
|
225 | - elseif ($operator == ">=" && $conjunction == "OR") { |
|
206 | + } elseif ($operator == ">=" && $conjunction == "OR") { |
|
226 | 207 | $bool['should'][] = ['range' => [$field => ['gte' => $value]]]; |
227 | - } |
|
228 | - elseif ($operator == "<" && $conjunction == "AND") { |
|
208 | + } elseif ($operator == "<" && $conjunction == "AND") { |
|
229 | 209 | $bool['filter'][] = ['range' => [$field => ['lt' => $value]]]; |
230 | - } |
|
231 | - elseif ($operator == "<" && $conjunction == "OR") { |
|
210 | + } elseif ($operator == "<" && $conjunction == "OR") { |
|
232 | 211 | $bool['should'][] = ['range' => [$field => ['lt' => $value]]]; |
233 | - } |
|
234 | - elseif ($operator == "<=" && $conjunction == "AND") { |
|
212 | + } elseif ($operator == "<=" && $conjunction == "AND") { |
|
235 | 213 | $bool['filter'][] = ['range' => [$field => ['lte' => $value]]]; |
236 | - } |
|
237 | - elseif ($operator == "<=" && $conjunction == "OR") { |
|
214 | + } elseif ($operator == "<=" && $conjunction == "OR") { |
|
238 | 215 | $bool['should'][] = ['range' => [$field => ['lte' => $value]]]; |
239 | - } |
|
240 | - elseif ($operator == "BETWEEN") { |
|
216 | + } elseif ($operator == "BETWEEN") { |
|
241 | 217 | natsort($value); |
242 | 218 | if ($conjunction == "AND") { |
243 | 219 | $bool['filter'][] = ['range' => [$field => [ |
244 | 220 | 'gt' => $value[0], |
245 | 221 | 'lt' => $value[1], |
246 | 222 | ]]]; |
247 | - } |
|
248 | - elseif ($conjunction == "OR") { |
|
223 | + } elseif ($conjunction == "OR") { |
|
249 | 224 | $bool['should'][] = ['range' => [$field => [ |
250 | 225 | 'gt' => $value[0], |
251 | 226 | 'lt' => $value[1], |
252 | 227 | ]]]; |
253 | 228 | } |
254 | - } |
|
255 | - elseif ($operator == "STARTS_WITH" && $conjunction == "AND") { |
|
229 | + } elseif ($operator == "STARTS_WITH" && $conjunction == "AND") { |
|
256 | 230 | $bool['filter'][] = ['prefix' => [$field => $value]]; |
257 | - } |
|
258 | - elseif ($operator == "STARTS_WITH" && $conjunction == "OR") { |
|
231 | + } elseif ($operator == "STARTS_WITH" && $conjunction == "OR") { |
|
259 | 232 | $bool['should'][] = ['prefix' => [$field => $value]]; |
260 | - } |
|
261 | - elseif ($operator == "ENDS_WITH" && $conjunction == "AND") { |
|
233 | + } elseif ($operator == "ENDS_WITH" && $conjunction == "AND") { |
|
262 | 234 | //TODO: WARNING THIS IS EXPENSIVE |
263 | 235 | $bool['filter'][] = ['wildcard' => [$field => '*'.$value]]; |
264 | - } |
|
265 | - elseif ($operator == "ENDS_WITH" && $conjunction == "OR") { |
|
236 | + } elseif ($operator == "ENDS_WITH" && $conjunction == "OR") { |
|
266 | 237 | //TODO: WARNING THIS IS EXPENSIVE |
267 | 238 | $bool['should'][] = ['wildcard' => [$field => '*'.$value]]; |
268 | - } |
|
269 | - elseif ($operator == "CONTAINS" && $conjunction == "AND") { |
|
239 | + } elseif ($operator == "CONTAINS" && $conjunction == "AND") { |
|
270 | 240 | //TODO: WARNING THIS IS EXPENSIVE |
271 | 241 | $bool['filter'][] = ['wildcard' => [$field => '*'.$value.'*']]; |
272 | - } |
|
273 | - elseif ($operator == "CONTAINS" && $conjunction == "OR") { |
|
242 | + } elseif ($operator == "CONTAINS" && $conjunction == "OR") { |
|
274 | 243 | //TODO: WARNING THIS IS EXPENSIVE |
275 | 244 | $bool['should'][] = ['wildcard' => [$field => '*'.$value.'*']]; |
276 | 245 | } |