@@ -91,6 +91,7 @@ public function filter__posts_pre_query( $posts, $query ) {
91
91
92
92
if ( ! is_array ( $ this ->search_result ) ) {
93
93
do_action ( 'jetpack_search_abort ' , 'no_search_results_array ' , $ this ->search_result );
94
+
94
95
return $ posts ;
95
96
}
96
97
@@ -136,6 +137,7 @@ public function filter__posts_pre_query( $posts, $query ) {
136
137
public function do_search ( \WP_Query $ query ) {
137
138
if ( ! $ this ->should_handle_query ( $ query ) ) {
138
139
do_action ( 'jetpack_search_abort ' , 'search_attempted_non_search_query ' , $ query );
140
+
139
141
return ;
140
142
}
141
143
@@ -181,8 +183,8 @@ public function do_search( \WP_Query $query ) {
181
183
*
182
184
* @since 5.0.0
183
185
*
184
- * @param array $wp_query_args The current query args, in WP_Query format.
185
- * @param \WP_Query $query The original WP_Query object.
186
+ * @param array $wp_query_args The current query args, in WP_Query format.
187
+ * @param \WP_Query $query The original WP_Query object.
186
188
*/
187
189
$ wp_query_args = apply_filters ( 'jetpack_search_es_wp_query_args ' , $ wp_query_args , $ query );
188
190
@@ -203,15 +205,17 @@ public function do_search( \WP_Query $query ) {
203
205
}
204
206
205
207
// Convert the WP-style args into ES args.
206
- $ es_query_args = $ this ->convert_wp_query_to_api_args ( $ wp_query_args );
208
+ $ api_query_args = $ this ->convert_wp_query_to_api_args ( $ wp_query_args );
209
+ $ api_query_args = $ this ->trigger_es_query_args_filter ( $ api_query_args , $ query );
210
+ $ api_query_args = $ this ->trigger_instant_search_query_args_filter ( $ api_query_args );
207
211
208
212
// Only trust ES to give us IDs, not the content since it is a mirror.
209
- $ es_query_args ['fields ' ] = array (
213
+ $ api_query_args ['fields ' ] = array (
210
214
'post_id ' ,
211
215
);
212
216
213
217
// Do the actual search query!
214
- $ this ->search_result = $ this ->search ( $ es_query_args );
218
+ $ this ->search_result = $ this ->search ( $ api_query_args );
215
219
216
220
if ( is_wp_error ( $ this ->search_result ) || ! is_array ( $ this ->search_result ) || empty ( $ this ->search_result ['results ' ] ) || ! is_array ( $ this ->search_result ['results ' ] ) ) {
217
221
$ this ->found_posts = 0 ;
@@ -331,6 +335,108 @@ public function convert_wp_query_to_api_args( array $args ) {
331
335
);
332
336
}
333
337
338
+ /**
339
+ * Trigger the jetpack_search_es_query_args filter for compatibility with Classic Search.
340
+ *
341
+ * The arguments can only be simulated, so this is not a 1:1 replacement.
342
+ * We support only some modifications, since not all of them are supported by Instant API.
343
+ * The goal is to support all common ones.
344
+ *
345
+ * @param array $api_query_args Array of API query arguments.
346
+ * @param \WP_Query $query The original WP_Query object.
347
+ *
348
+ * @return array
349
+ */
350
+ private function trigger_es_query_args_filter ( array $ api_query_args , \WP_Query $ query ): array {
351
+ $ es_query_args = array (
352
+ 'blog_id ' => $ api_query_args ['blog_id ' ] ?? 1 ,
353
+ 'size ' => $ api_query_args ['size ' ] ?? 10 ,
354
+ 'from ' => $ api_query_args ['from ' ] ?? 0 ,
355
+ 'sort ' => array (
356
+ array ( '_score ' => array ( 'order ' => 'desc ' ) ),
357
+ ),
358
+ 'filter ' => $ api_query_args ['filter ' ] ?? array (),
359
+ 'query ' => array (
360
+ 'function_score ' => array (
361
+ 'query ' => array (
362
+ 'bool ' => array (
363
+ 'must ' => array (
364
+ array (
365
+ 'multi_match ' => array (
366
+ 'fields ' => array ( 'title.en ' ),
367
+ 'query ' => $ api_query_args ['query ' ] ?? '' ,
368
+ 'operator ' => 'and ' ,
369
+ ),
370
+ ),
371
+ ),
372
+ ),
373
+ ),
374
+ 'functions ' => array ( array ( 'gauss ' => array ( 'date_gmt ' => array ( 'origin ' => '2025-05-13 ' ) ) ) ),
375
+ 'max_boost ' => 2.0 ,
376
+ 'score_mode ' => 'multiply ' ,
377
+ 'boost_mode ' => 'multiply ' ,
378
+ ),
379
+ ),
380
+ 'aggregations ' => $ api_query_args ['aggregations ' ] ?? array (),
381
+ 'fields ' => $ api_query_args ['fields ' ] ?? array (),
382
+ );
383
+
384
+ $ es_query_args = apply_filters ( 'jetpack_search_es_query_args ' , $ es_query_args , $ query );
385
+
386
+ if ( ! empty ( $ es_query_args ['aggregations ' ] ) && is_array ( $ es_query_args ['aggregations ' ] ) ) {
387
+ $ api_query_args ['aggregations ' ] = $ es_query_args ['aggregations ' ];
388
+ }
389
+ $ api_query_args ['filter ' ] = $ es_query_args ['filter ' ] ?? $ api_query_args ['filter ' ];
390
+ $ api_query_args ['size ' ] = $ es_query_args ['size ' ] ?? $ api_query_args ['size ' ];
391
+ $ api_query_args ['from ' ] = $ es_query_args ['from ' ] ?? $ api_query_args ['from ' ];
392
+ if ( isset ( $ es_query_args ['query ' ]['bool ' ]['must_not ' ] ) ) {
393
+ $ api_query_args ['filter ' ] = array (
394
+ 'bool ' => array (
395
+ 'must_not ' => $ es_query_args ['query ' ]['bool ' ]['must_not ' ],
396
+ 'filter ' => array (
397
+ $ api_query_args ['filter ' ],
398
+ ),
399
+ ),
400
+ );
401
+ }
402
+ if ( isset ( $ es_query_args ['query ' ]['bool ' ]['filter ' ] ) && is_array ( $ es_query_args ['query ' ]['bool ' ]['filter ' ] ) ) {
403
+ $ new_filter = array (
404
+ 'bool ' => array (
405
+ 'filter ' => $ es_query_args ['query ' ]['bool ' ]['filter ' ],
406
+ ),
407
+ );
408
+ if ( ! empty ( $ api_query_args ['filter ' ] ) ) {
409
+ $ new_filter ['bool ' ]['filter ' ][] = $ api_query_args ['filter ' ];
410
+ }
411
+ $ api_query_args ['filter ' ] = $ new_filter ;
412
+ }
413
+
414
+ return $ api_query_args ;
415
+ }
416
+
417
+ /**
418
+ * Trigger jetpack_instant_search_options for compatibility with Instant Search.
419
+ *
420
+ * @param array $api_query_args Array of API query arguments.
421
+ *
422
+ * @return array
423
+ */
424
+ private function trigger_instant_search_query_args_filter ( array $ api_query_args ): array {
425
+ // this will trigger jetpack_instant_search_options filter
426
+ $ options = Helper::generate_initial_javascript_state ();
427
+
428
+ if ( isset ( $ options ['adminQueryFilter ' ] ) ) {
429
+ $ api_query_args ['filter ' ] = array (
430
+ 'bool ' => array (
431
+ 'filter ' => $ api_query_args ['filter ' ],
432
+ 'must ' => $ options ['adminQueryFilter ' ],
433
+ ),
434
+ );
435
+ }
436
+
437
+ return $ api_query_args ;
438
+ }
439
+
334
440
/**
335
441
* Return array of languages to search on after executing the dedicated filter.
336
442
*
@@ -419,6 +525,7 @@ private function build_es_filters( array $args ): array {
419
525
protected function instant_api ( array $ es_args ) {
420
526
$ instant_search = new Instant_Search ();
421
527
$ instant_search ->jetpack_blog_id = $ this ->jetpack_blog_id ;
528
+
422
529
return $ instant_search ->instant_api ( $ es_args );
423
530
}
424
531
0 commit comments