Skip to content

Commit c223877

Browse files
trakosmatticbot
authored andcommitted
Jetpack Search: additional filters support for Inline Search (#43441)
Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/15014401981 Upstream-Ref: Automattic/jetpack@54cdd85
1 parent fe69539 commit c223877

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [0.48.1-alpha] - unreleased
8+
## [0.49.0-alpha] - unreleased
99

1010
This is an alpha version! The changes listed here are not final.
1111

12+
### Added
13+
- Jetpack Search: add query filters support to Inline Search
14+
1215
## [0.48.0] - 2025-05-12
1316
### Added
1417
- Surface search corrections when correcting search terms. [#42473]
@@ -1218,7 +1221,7 @@ This is an alpha version! The changes listed here are not final.
12181221
- Updated package dependencies.
12191222
- Update PHPUnit configs to include just what needs coverage rather than include everything then try to exclude stuff that doesn't.
12201223

1221-
[0.48.1-alpha]: https://github.com/Automattic/jetpack-search/compare/v0.48.0...v0.48.1-alpha
1224+
[0.49.0-alpha]: https://github.com/Automattic/jetpack-search/compare/v0.48.0...v0.49.0-alpha
12221225
[0.48.0]: https://github.com/Automattic/jetpack-search/compare/v0.47.24...v0.48.0
12231226
[0.47.24]: https://github.com/Automattic/jetpack-search/compare/v0.47.23...v0.47.24
12241227
[0.47.23]: https://github.com/Automattic/jetpack-search/compare/v0.47.22...v0.47.23

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"link-template": "https://github.com/Automattic/jetpack-search/compare/v${old}...v${new}"
6464
},
6565
"branch-alias": {
66-
"dev-trunk": "0.48.x-dev"
66+
"dev-trunk": "0.49.x-dev"
6767
},
6868
"version-constants": {
6969
"::VERSION": "src/class-package.php"

src/class-package.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Search package general information
1212
*/
1313
class Package {
14-
const VERSION = '0.48.1-alpha';
14+
const VERSION = '0.49.0-alpha';
1515
const SLUG = 'search';
1616

1717
/**

src/inline-search/class-inline-search.php

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function filter__posts_pre_query( $posts, $query ) {
9191

9292
if ( ! is_array( $this->search_result ) ) {
9393
do_action( 'jetpack_search_abort', 'no_search_results_array', $this->search_result );
94+
9495
return $posts;
9596
}
9697

@@ -136,6 +137,7 @@ public function filter__posts_pre_query( $posts, $query ) {
136137
public function do_search( \WP_Query $query ) {
137138
if ( ! $this->should_handle_query( $query ) ) {
138139
do_action( 'jetpack_search_abort', 'search_attempted_non_search_query', $query );
140+
139141
return;
140142
}
141143

@@ -181,8 +183,8 @@ public function do_search( \WP_Query $query ) {
181183
*
182184
* @since 5.0.0
183185
*
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.
186188
*/
187189
$wp_query_args = apply_filters( 'jetpack_search_es_wp_query_args', $wp_query_args, $query );
188190

@@ -203,15 +205,17 @@ public function do_search( \WP_Query $query ) {
203205
}
204206

205207
// 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 );
207211

208212
// 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(
210214
'post_id',
211215
);
212216

213217
// Do the actual search query!
214-
$this->search_result = $this->search( $es_query_args );
218+
$this->search_result = $this->search( $api_query_args );
215219

216220
if ( is_wp_error( $this->search_result ) || ! is_array( $this->search_result ) || empty( $this->search_result['results'] ) || ! is_array( $this->search_result['results'] ) ) {
217221
$this->found_posts = 0;
@@ -331,6 +335,108 @@ public function convert_wp_query_to_api_args( array $args ) {
331335
);
332336
}
333337

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+
334440
/**
335441
* Return array of languages to search on after executing the dedicated filter.
336442
*
@@ -419,6 +525,7 @@ private function build_es_filters( array $args ): array {
419525
protected function instant_api( array $es_args ) {
420526
$instant_search = new Instant_Search();
421527
$instant_search->jetpack_blog_id = $this->jetpack_blog_id;
528+
422529
return $instant_search->instant_api( $es_args );
423530
}
424531

0 commit comments

Comments
 (0)