-
Notifications
You must be signed in to change notification settings - Fork 29
/
class-es-wp-meta-query.php
436 lines (388 loc) · 12.5 KB
/
class-es-wp-meta-query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
/**
* ES_WP_Query classes: ES_WP_Meta_Query class
*
* @package ES_WP_Query
*/
/**
* Elasticsearch wrapper for WP_Meta_Query
*/
class ES_WP_Meta_Query extends WP_Meta_Query {
/**
* Some object which extends ES_WP_Query_Wrapper.
*
* @var ES_WP_Query_Wrapper
*/
protected $es_query;
/**
* Initialize class .
*
* Call parent, and then initialize a variable
* containing all meta -queries and their types .
*
* @access public
*
* @param array $meta_query array of meta query clauses .
*
* @return none
*/
public function __construct( $meta_query = false ) {
/*
* Call parent, so $this->sanitize_query() gets called
* amongst other stuff.
*/
parent::__construct( $meta_query );
$this->queries_types_all = $this->queries_types_all_get(
$this->queries
);
}
/**
* Returns a simplified version of the meta-queries given as an argument.
* The simplification pertains to returning only the key/type values of
* each part of the meta-query. Also, the simplification takes care to flatten
* out the result.
*
* If the meta-query is composed of multiple joining queries, these are
* processed by recursively walking through them, and calling this
* function to process each.
*
* @param array $meta_clauses Meta clauses to be processed.
* @access protected
* @return array All queries, but only with key and type key/values pairs.
*/
protected function queries_types_all_get( $meta_clauses ) {
$queries_types = array();
if ( ! is_array( $meta_clauses ) ) {
return array();
}
if ( empty( $meta_clauses ) ) {
return $meta_clauses;
}
foreach ( array_keys( $meta_clauses ) as $meta_clause_key ) {
if ( $this->is_first_order_clause(
$meta_clauses[ $meta_clause_key ]
) ) {
/*
* Save this part of the meta-query, but keep only
* the key/type pairs.
*
*
* Note: If there are multiple sub-queries with the same
* key, this will overwrite the previous one (if any).
* As a result, the last one will be the one who prevails.
*/
if ( isset( $meta_clauses[ $meta_clause_key ]['key'] ) ) {
$queries_types[
$meta_clause_key
] = array(
'key' => $meta_clauses[ $meta_clause_key ]['key'],
);
} else {
$queries_types[
$meta_clause_key
] = array(
'key' => $meta_clause_key,
);
}
if ( isset(
$meta_clauses[ $meta_clause_key ]['type']
) ) {
$queries_types[ $meta_clause_key ]['type'] =
$meta_clauses[ $meta_clause_key ]['type'];
}
} else {
/*
* Recursively process the clause.
*/
$recursive_result = $this->queries_types_all_get(
$meta_clauses[ $meta_clause_key ]
);
/*
* Only save the result if an array, and
* it is not empty.
*/
if (
( is_array( $recursive_result ) ) &&
( ! empty( $recursive_result ) )
) {
$queries_types = array_merge(
$queries_types,
$recursive_result
);
}
}
}
return $queries_types;
}
/**
* Turns an array of meta query parameters into ES Query DSL
*
* @access public
*
* @param object $es_query Any object which extends ES_WP_Query_Wrapper.
* @param string $type Type of meta. Currently, only 'post' is supported.
* @return array ES filters
*/
public function get_dsl( $es_query, $type ) {
// Currently only 'post' is supported.
if ( 'post' !== $type ) {
return false;
}
$this->es_query = $es_query;
$filters = $this->get_dsl_clauses();
return apply_filters_ref_array( 'get_meta_dsl', array( $filters, $this->queries, $type, $this->es_query ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}
/**
* Generate ES Filter clauses to be appended to a main query.
*
* Called by the public {@see ES_WP_Meta_Query::get_dsl()}, this method
* is abstracted out to maintain parity with the other Query classes.
*
* @access protected
*
* @return array
*/
protected function get_dsl_clauses() {
/*
* $queries are passed by reference to
* `ES_WP_Meta_Query::get_dsl_for_query()` for recursion. To keep
* $this->queries unaltered, pass a copy.
*/
$queries = $this->queries;
return $this->get_dsl_for_query( $queries );
}
/**
* Generate ES filters for a single query array.
*
* If nested subqueries are found, this method recurses the tree to produce
* the properly nested DSL.
*
* @access protected
*
* @param array $query Query to parse, passed by reference.
* @return array Array containing nested ES filter clauses.
*/
protected function get_dsl_for_query( &$query ) {
$filters = array();
foreach ( $query as $key => &$clause ) {
if ( 'relation' === $key ) {
$relation = $query['relation'];
} elseif ( is_array( $clause ) ) {
if ( $this->is_first_order_clause( $clause ) ) {
// This is a first-order clause.
$filters[] = $this->get_dsl_for_clause( $clause, $query, $key );
} else {
// This is a subquery, so we recurse.
$filters[] = $this->get_dsl_for_query( $clause );
}
}
}
// Filter to remove empties.
$filters = array_filter( $filters );
$this->clauses = array_filter( $this->clauses );
if ( ! empty( $relation ) && 'or' === strtolower( $relation ) ) {
$relation = 'should';
} else {
$relation = 'filter';
}
if ( count( $filters ) > 1 ) {
$filters = array(
'bool' => array(
$relation => $filters,
),
);
} elseif ( ! empty( $filters ) ) {
$filters = reset( $filters );
}
return $filters;
}
/**
* Generate ES filter clauses for a first-order query clause.
*
* "First-order" means that it's an array with a 'key' or 'value'.
*
* @access public
*
* @param array $clause Query clause, passed by reference.
* @param array $query Parent query array.
* @param string $clause_key Optional. The array key used to name the
* clause in the original `$meta_query`
* parameters. If not provided, a key will be
* generated automatically.
* @return array ES filter clause component.
*/
public function get_dsl_for_clause( &$clause, $query, $clause_key = '' ) {
// Key must be a string, so fallback for clause keys is 'meta-clause'.
if ( is_int( $clause_key ) || ! $clause_key ) {
$clause_key = 'meta-clause';
}
// Ensure unique clause keys, so none are overwritten.
$iterator = 1;
$clause_key_base = $clause_key;
while ( isset( $this->clauses[ $clause_key ] ) ) {
$clause_key = $clause_key_base . '-' . $iterator;
$iterator++;
}
// Split out 'exists' and 'not exists' queries. These may also be
// queries missing a value or with an empty array as the value.
if ( isset( $clause['compare'] ) && ! empty( $clause['value'] ) ) {
if ( 'EXISTS' === strtoupper( $clause['compare'] ) ) {
$clause['compare'] = is_array( $clause['value'] ) ? 'IN' : '=';
} elseif ( 'NOT EXISTS' === strtoupper( $clause['compare'] ) ) {
unset( $clause['value'] );
}
}
if ( ( isset( $clause['value'] ) && is_array( $clause['value'] ) && empty( $clause['value'] ) ) || ( ! array_key_exists( 'value', $clause ) && ! empty( $clause['key'] ) ) ) {
$this->clauses[ $clause_key ] =& $clause;
if ( isset( $clause['compare'] ) && 'NOT EXISTS' === strtoupper( $clause['compare'] ) ) {
return $this->es_query->dsl_missing( $this->es_query->meta_map( trim( $clause['key'] ) ) );
} else {
return $this->es_query->dsl_exists( $this->es_query->meta_map( trim( $clause['key'] ) ) );
}
}
$clause['key'] = isset( $clause['key'] ) ? trim( $clause['key'] ) : '*';
if ( array_key_exists( 'value', $clause ) && is_null( $clause['value'] ) ) {
$clause['value'] = '';
}
$clause['value'] = isset( $clause['value'] ) ? $clause['value'] : null;
if ( isset( $clause['compare'] ) ) {
$clause['compare'] = strtoupper( $clause['compare'] );
} else {
$clause['compare'] = is_array( $clause['value'] ) ? 'IN' : '=';
}
if ( in_array( $clause['compare'], array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ), true ) ) {
if ( ! is_array( $clause['value'] ) ) {
$clause['value'] = preg_split( '/[,\s]+/', $clause['value'] );
}
if ( empty( $clause['value'] ) ) {
// This compare type requires an array of values. If we don't
// have one, we bail on this query.
return array();
}
} else {
$clause['value'] = trim( $clause['value'] );
}
// Store the clause in our flat array.
$this->clauses[ $clause_key ] =& $clause;
if ( '*' === $clause['key'] && ! in_array( $clause['compare'], array( '=', '!=', 'LIKE', 'NOT LIKE' ), true ) ) {
return apply_filters( 'es_meta_query_keyless_query', array(), $clause['value'], $clause['compare'], $this, $this->es_query );
}
$clause['type'] = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
// Allow adapters to normalize meta values (like `strtolower` if mapping to `raw_lc`).
$clause['value'] = apply_filters( 'es_meta_query_meta_value', $clause['value'], $clause['key'], $clause['compare'], $clause['type'] );
switch ( $clause['compare'] ) {
case '>':
case '>=':
case '<':
case '<=':
switch ( $clause['compare'] ) {
case '>':
$operator = 'gt';
break;
case '>=':
$operator = 'gte';
break;
case '<':
$operator = 'lt';
break;
case '<=':
$operator = 'lte';
break;
}
$filter = $this->es_query->dsl_range( $this->es_query->meta_map( $clause['key'], $clause['type'] ), array( $operator => $clause['value'] ) );
break;
case 'LIKE':
case 'NOT LIKE':
if ( '*' === $clause['key'] ) {
$filter = $this->es_query->dsl_multi_match( $this->es_query->meta_map( $clause['key'], 'analyzed' ), $clause['value'] );
} else {
$filter = $this->es_query->dsl_match( $this->es_query->meta_map( $clause['key'], 'analyzed' ), $clause['value'] );
}
break;
case 'BETWEEN':
case 'NOT BETWEEN':
// These may produce unexpected results depending on how your data is indexed.
$clause['value'] = array_slice( $clause['value'], 0, 2 );
if ( 'DATETIME' === $clause['type'] ) {
$date1 = strtotime( $clause['value'][0] );
$date2 = strtotime( $clause['value'][1] );
if ( $date1 && $date2 ) {
$clause['value'] = array( $date1, $date2 );
sort( $clause['value'] );
$filter = $this->es_query->dsl_range(
$this->es_query->meta_map( $clause['key'], $clause['type'] ),
ES_WP_Date_Query::build_date_range( $clause['value'][0], '>=', $clause['value'][1], '<=' )
);
}
} else {
natcasesort( $clause['value'] );
$filter = $this->es_query->dsl_range(
$this->es_query->meta_map( $clause['key'], $clause['type'] ),
array(
'gte' => $clause['value'][0],
'lte' => $clause['value'][1],
)
);
}
break;
case 'REGEXP':
case 'NOT REGEXP':
case 'RLIKE':
_doing_it_wrong( 'ES_WP_Query', esc_html__( 'ES_WP_Query does not support regular expression meta queries.', 'es-wp-query' ), '0.1' );
// Empty out $clause, since this will be disregarded.
$clause = array();
return array();
default:
if ( '*' === $clause['key'] ) {
$filter = $this->es_query->dsl_multi_match( $this->es_query->meta_map( $clause['key'], $clause['type'] ), $clause['value'] );
} else {
$filter = $this->es_query->dsl_terms( $this->es_query->meta_map( $clause['key'], $clause['type'] ), $clause['value'] );
}
break;
}
if ( ! empty( $filter ) ) {
// To maintain parity with WP_Query, if we're doing a negation
// query, we still only query posts where the meta key exists.
if ( in_array( $clause['compare'], array( 'NOT IN', '!=', 'NOT BETWEEN', 'NOT LIKE' ), true ) ) {
return array(
'bool' => array(
'filter' => array(
$this->es_query->dsl_exists( $this->es_query->meta_map( $clause['key'] ) ),
),
'must_not' => $filter,
),
);
} else {
return $filter;
}
}
}
/**
* Get the ES mapping suffix for the given type.
*
* @param string $type Meta_Query type. See Meta_Query docs.
* @return string
*/
public function get_cast_for_type( $type = '' ) {
$type = preg_replace( '/^([A-Z]+).*$/', '$1', strtoupper( $type ) );
switch ( $type ) {
case 'NUMERIC':
return 'long';
case 'SIGNED':
return 'long';
case 'UNSIGNED':
return 'long';
case 'BINARY':
return 'boolean';
case 'DECIMAL':
return 'double';
case 'DATE':
return 'date';
case 'DATETIME':
return 'datetime';
case 'TIME':
return 'time';
}
return '';
}
}