-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathQueryIntegration.php
More file actions
590 lines (508 loc) · 16 KB
/
QueryIntegration.php
File metadata and controls
590 lines (508 loc) · 16 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
* Integrate with WP_Query
*
* @since 1.0
* @package elasticpress
*/
namespace ElasticPress\Indexable\Post;
use WP_Query;
use ElasticPress\Indexables;
use ElasticPress\Utils;
if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
exit; // Exit if accessed directly.
// @codeCoverageIgnoreEnd
}
/**
* Query integration class
*/
class QueryIntegration {
/**
* Is set only when we switch_to_blog in MS context
*
* @var boolean
*/
private $switched = false;
/**
* Checks to see if we should be integrating and if so, sets up the appropriate actions and filters.
*
* @param string $indexable_slug Indexable slug. Optional.
*
* @since 0.9
* @since 3.6.0 Added $indexable_slug
*/
public function __construct( $indexable_slug = 'post' ) {
/**
* Filter whether to enable query integration during indexing
*
* @since 4.5.2
* @hook ep_enable_query_integration_during_indexing
*
* @param {bool} $enable To allow query integration during indexing
* @param {string} $indexable_slug Indexable slug
* @return {bool} New value
*/
$allow_query_integration_during_indexing = apply_filters( 'ep_enable_query_integration_during_indexing', false, $indexable_slug );
// Ensure that we are currently allowing ElasticPress to override the normal WP_Query
// Indexable->is_full_reindexing() is not available at this point yet, so using the IndexHelper version of it.
if ( \ElasticPress\IndexHelper::factory()->is_full_reindexing( $indexable_slug, get_current_blog_id() ) && ! $allow_query_integration_during_indexing ) {
return;
}
// Add header
add_action( 'pre_get_posts', array( $this, 'add_es_header' ), 5 );
// Query ES for posts
add_filter( 'posts_pre_query', array( $this, 'get_es_posts' ), 10, 2 );
// Properly restore blog if necessary
add_action( 'loop_end', array( $this, 'maybe_restore_blog' ), 10 );
// Properly switch to blog if necessary
add_action( 'the_post', array( $this, 'maybe_switch_to_blog' ), 10, 2 );
// Sets the correct value for found_posts
add_filter( 'found_posts', array( $this, 'found_posts' ), 10, 2 );
}
/**
* Set the found_posts variable on WP_Query.
*
* @param int $found_posts Number of found posts
* @param WP_Query $query Query object
* @since 2.8.2
* @return int
*/
public function found_posts( $found_posts, $query ) {
/**
* Filter to skip WP Query integration
*
* @hook ep_skip_query_integration
* @param {bool} $skip True to skip
* @param {WP_Query} $query WP Query to evaluate
* @return {bool} New skip value
*/
if ( ( isset( $query->elasticsearch_success ) && false === $query->elasticsearch_success ) || ( ! Indexables::factory()->get( 'post' )->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_query_integration', false, $query ) ) ) {
return $found_posts;
}
return $query->num_posts;
}
/**
* Disables cache_results, adds header.
*
* @param WP_Query $query WP_Query instance
* @since 0.9
*/
public function add_es_header( $query ) {
/**
* Filter to skip WP Query integration
*
* @hook ep_skip_query_integration
* @param {bool} $skip True to skip
* @param {WP_Query} $query WP Query to evaluate
* @return {bool} New skip value
*/
if ( ! Indexables::factory()->get( 'post' )->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_query_integration', false, $query ) ) {
return;
}
if ( ! headers_sent() ) {
/**
* Manually setting a header as $wp_query isn't yet initialized when we
* call: add_filter('wp_headers', 'filter_wp_headers');
*/
// @codeCoverageIgnoreStart
header( 'X-ElasticPress-Query: true' );
// @codeCoverageIgnoreEnd
}
}
/**
* Gets the blog ID that the class is currently switched to.
*
* @return int
*/
public function get_switched() {
return $this->switched;
}
/**
* Switch to the correct site if the post site id is different than the actual one.
*
* Note: This function can bring a performance penalty in multisites with a high number of sites.
*
* @param WP_Post $post Post object
* @param WP_Query $query WP_Query instance. If null, the global query will be used.
* @since 0.9
* @since 3.6.2 `$query` parameter added.
*/
public function maybe_switch_to_blog( $post, $query = null ) {
global $wp_query;
if ( ! $query ) {
$query = $wp_query;
}
if ( ! is_multisite() ) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
if ( ! empty( $post->site_id ) && get_current_blog_id() !== $post->site_id ) {
if ( $this->switched ) {
restore_current_blog();
$this->switched = false;
}
switch_to_blog( $post->site_id );
$this->switched = $post->site_id;
remove_action( 'the_post', array( $this, 'maybe_switch_to_blog' ), 10, 2 );
setup_postdata( $post );
add_action( 'the_post', array( $this, 'maybe_switch_to_blog' ), 10, 2 );
if ( $this->switched && ! $query->in_the_loop ) {
restore_current_blog();
$this->switched = false;
}
}
}
/**
* Make sure the correct blog is restored
*
* @param WP_Query $query WP_Query instance
*
* @since 0.9
*/
public function maybe_restore_blog( $query ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
if ( ! is_multisite() ) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
if ( $this->switched ) {
restore_current_blog();
$this->switched = false;
}
}
/**
* Get posts from Elasticsearch
*
* @param array $posts Array of posts
* @param WP_Query $query WP_Query instance
* @since 3.0
* @return string
*/
public function get_es_posts( $posts, $query ) {
global $wpdb;
/**
* Filter to skip WP Query integration
*
* @hook ep_skip_query_integration
* @param {bool} $skip True to skip
* @param {WP_Query} $query WP Query to evaluate
* @return {bool} New skip value
*/
if ( ! Indexables::factory()->get( 'post' )->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_query_integration', false, $query ) ) {
return $posts;
}
$query_vars = $query->query_vars;
/**
* Filter post type query variables before WP Query
*
* @since 2.1
* @hook ep_query_post_type
* @param {string|array} $post_types Post types
* @param {WP_Query} $query WP Query object
* @return {string|array} New post types
*/
$query_vars['post_type'] = apply_filters( 'ep_query_post_type', $query_vars['post_type'] ?? '', $query );
if ( 'any' === $query_vars['post_type'] ) {
unset( $query_vars['post_type'] );
}
/**
* If not search and not set, default to post. If not set and is search, use searchable post types.
*/
if ( empty( $query_vars['post_type'] ) ) {
$tax_post_type = Utils\get_post_types_for_tax_query( $query );
if ( ! empty( $tax_post_type ) ) {
$query_vars['post_type'] = $tax_post_type;
} elseif ( empty( $query_vars['s'] ) ) {
$query_vars['post_type'] = 'post';
} else {
$query_vars['post_type'] = array_values( get_post_types( array( 'exclude_from_search' => false ) ) );
}
}
/**
* No post types so bail
*/
if ( empty( $query_vars['post_type'] ) ) {
return [];
}
/**
* Filter cached posts pre-post query
*
* @hook ep_wp_query_cached_posts
* @param {array} $posts Array of posts
* @param {WP_Query} $query WP Query object
* @return {array} New cached posts
*/
$new_posts = apply_filters( 'ep_wp_query_cached_posts', [], $query );
$ep_query = null;
if ( count( $new_posts ) < 1 ) {
$scope = 'current';
$site__in = '';
$site__not_in = '';
if ( ! empty( $query_vars['sites'] ) ) {
_deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) );
}
if ( ! empty( $query_vars['site__in'] ) || ! empty( $query_vars['sites'] ) ) {
$site__in = ! empty( $query_vars['site__in'] ) ? (array) $query_vars['site__in'] : (array) $query_vars['sites'];
if ( in_array( 'all', $site__in, true ) ) {
$scope = 'all';
} elseif ( in_array( 'current', $site__in, true ) ) {
$site__in = (array) get_current_blog_id();
}
}
if ( ! empty( $query_vars['site__not_in'] ) ) {
$site__not_in = (array) $query_vars['site__not_in'];
}
$formatted_args = Indexables::factory()->get( 'post' )->format_args( $query_vars, $query );
/**
* Filter post query scope
*
* @hook ep_search_scope
* @param {string} $scope Current scope
* @return {string} New scope
* @since 2.1
*/
$scope = apply_filters( 'ep_search_scope', $scope );
if ( ! defined( 'EP_IS_NETWORK' ) || ! EP_IS_NETWORK ) {
// @codeCoverageIgnoreStart
$scope = 'current';
// @codeCoverageIgnoreEnd
}
$index = null;
if ( 'all' === $scope ) {
$index = Indexables::factory()->get( 'post' )->get_network_alias();
} elseif ( ! empty( $site__in ) ) {
$index = [];
foreach ( $site__in as $site_id ) {
$index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id );
}
$index = implode( ',', $index );
} elseif ( ! empty( $site__not_in ) ) {
$sites = \get_sites(
array(
'fields' => 'ids',
'site__not_in' => $site__not_in,
)
);
foreach ( $sites as $site_id ) {
if ( ! Utils\is_site_indexable( $site_id ) ) {
continue;
}
$index[] = Indexables::factory()->get( 'post' )->get_index_name( $site_id );
}
$index = implode( ',', $index );
}
$ep_query = Indexables::factory()->get( 'post' )->query_es( $formatted_args, $query->query_vars, $index, $query );
/**
* ES failed. Go back to MySQL.
*/
if ( false === $ep_query ) {
$query->elasticsearch_success = false;
return null;
}
$found_documents = is_array( $ep_query['found_documents'] ) ? $ep_query['found_documents']['value'] : $ep_query['found_documents']; // 7.0+ have this as an array rather than int
$query->found_posts = $found_documents;
$query->num_posts = $query->found_posts;
$query->max_num_pages = ceil( $found_documents / $query->get( 'posts_per_page' ) );
$query->suggested_terms = $this->maybe_sanitize_suggestion( $ep_query );
$query->elasticsearch_success = true;
// Determine how we should format the results from ES based on the fields parameter.
$fields = $query->get( 'fields', '' );
switch ( $fields ) {
case 'ids':
$new_posts = $this->format_hits_as_ids( $ep_query['documents'], $new_posts );
break;
case 'id=>parent':
$new_posts = $this->format_hits_as_id_parents( $ep_query['documents'], $new_posts );
break;
default:
$new_posts = $this->format_hits_as_posts( $ep_query['documents'], $new_posts );
break;
}
/**
* Fires after non cached post query
*
* @hook ep_wp_query_non_cached_search
* @param {array} $new_posts Array of posts from query
* @param {array} $ep_query Raw Elasticsearch query
* @param {WP_Query} $query WordPress query
*/
do_action( 'ep_wp_query_non_cached_search', $new_posts, $ep_query, $query );
}
/**
* Fires before returning posts from query
*
* @hook ep_wp_query
* @param {array} $new_posts Array of posts from query
* @param {array} $ep_query Raw Elasticsearch query
* @param {WP_Query} $query WordPress query
*/
do_action( 'ep_wp_query', $new_posts, $ep_query, $query );
/**
* Fires before returning posts from query
*
* Pre-3.0 backwards compat
*
* @hook ep_wp_query_search
* @param {array} $new_posts Array of posts from query
* @param {array} $ep_query Raw Elasticsearch query
* @param {WP_Query} $query WordPress query
*/
do_action( 'ep_wp_query_search', $new_posts, $ep_query, $query );
return $new_posts;
}
/**
* Format the ES hits/results as post objects.
*
* @since 2.4.0
*
* @param array $posts The posts that should be formatted.
* @param array $new_posts Array of posts from cache.
*
* @return array
*/
protected function format_hits_as_posts( $posts, $new_posts ) {
foreach ( $posts as $post_array ) {
$post = new \stdClass();
$post->ID = $post_array['post_id'];
$post->site_id = get_current_blog_id();
if ( ! empty( $post_array['site_id'] ) ) {
$post->site_id = $post_array['site_id'];
}
/**
* Filter post object properties set after query
*
* @hook ep_search_post_return_args
* @param {array} $properties Post properties
* @return {array} New properties
*/
$post_return_args = apply_filters(
'ep_search_post_return_args',
array(
'post_type',
'post_author',
'post_name',
'post_status',
'post_title',
'post_parent',
'post_content',
'post_excerpt',
'post_date',
'post_date_gmt',
'post_modified',
'post_modified_gmt',
'post_mime_type',
'comment_count',
'comment_status',
'ping_status',
'menu_order',
'permalink',
'terms',
'post_meta',
'meta',
)
);
foreach ( $post_return_args as $key ) {
if ( 'post_author' === $key ) {
$post->$key = $post_array[ $key ]['id'];
} elseif ( isset( $post_array[ $key ] ) ) {
$post->$key = $post_array[ $key ];
}
}
/**
* Replace post attributes with highlighted versions if available.
*
* $post_array['highlight'] is set from $hit['highlight'] in Elasticsearch.php
* when going through the returned results, and that is defined by
* the Highlighting Feature on setup, calling ep_formatted_args to
* define the highlight array of fields.
*/
if ( isset( $post_array['highlight'] ) ) {
foreach ( $post_array['highlight'] as $key => $val ) {
// e.g. $post->post_content
if ( isset( $post->$key ) ) {
/**
* e.g. replaces post content value with the highlighted value
* $post->post_content = implode( ' ', $post_array['highlight']['post_content'] );
*
* Depending on how highlight.fields.<field>.number_of_fragments is set in the query,
* Elasticsearch can return an array with N entries, with N being the number
* of matches found.
*/
$post->$key = implode( ' ', $val );
}
}
}
$post->elasticsearch = true; // Super useful for debugging
if ( $post ) {
$new_posts[] = $post;
}
}
return $new_posts;
}
/**
* Format the ES hits/results as an array of ids.
*
* @since 2.4.0
*
* @param array $posts The posts that should be formatted.
* @param array $new_posts Array of posts from cache.
*
* @return array
*/
protected function format_hits_as_ids( $posts, $new_posts ) {
foreach ( $posts as $post_array ) {
$new_posts[] = $post_array['post_id'];
}
return $new_posts;
}
/**
* Format the ES hits/results as objects containing id and parent id.
*
* @since 2.4.0
*
* @param array $posts The posts that should be formatted.
* @param array $new_posts Array of posts from cache.
*
* @return array
*/
protected function format_hits_as_id_parents( $posts, $new_posts ) {
foreach ( $posts as $post_array ) {
$post = new \stdClass();
$post->ID = $post_array['post_id'];
$post->post_parent = $post_array['post_parent'];
$post->elasticsearch = true; // Super useful for debugging
$new_posts[] = $post;
}
return $new_posts;
}
/**
* Remove any suggestion that has a score lower than the minimum score.
*
* @since 4.6.0
* @param array $ep_query The query array.
* @return array
*/
protected function maybe_sanitize_suggestion( $ep_query ) {
if ( ! isset( $ep_query['suggest']['ep_suggestion'], $ep_query['suggest']['ep_suggestion'][0] ) ) {
return [];
}
$suggestion = $ep_query['suggest']['ep_suggestion'][0];
/**
* Filter the score for a suggestion. If the score is lower than this, it will be removed.
*
* @since 4.6.0
* @param float $min_score The minimum score allowed.
* @return float
*/
$min_score = (float) apply_filters( 'ep_suggestion_minimum_score', 0.0001 );
$suggestion['options'] = array_filter(
$suggestion['options'],
function ( $option ) use ( $min_score ) {
return number_format( $option['score'], 10 ) > $min_score;
}
);
return $suggestion;
}
}