From 54793bd2de724b7f38910bf3e19d3111ebde5bc5 Mon Sep 17 00:00:00 2001 From: Robert Felty Date: Fri, 8 Nov 2024 16:10:40 +0100 Subject: [PATCH] Add/jetpack search custom results (#36378) * Adding option to specify custom results for jetpack search * Got it working * changelog * Added ability to use regex * changelog * changed data structure * changelog * fixed logic in api * updated TrainTracks events to give more information about the ui-algo * updated version numbers * fixed default value for customResults to empty array * fixed rebase bug --- .../add-jetpack-search-custom-results | 4 ++++ .../instant-search/components/search-app.jsx | 1 + .../components/search-result.jsx | 14 +++++++++-- .../search/src/instant-search/lib/api.js | 23 +++++++++++++++++++ .../add-jetpack-search-custom-results | 4 ++++ .../add-jetpack-search-custom-results | 4 ++++ projects/plugins/search/jetpack-search.php | 1 - 7 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 projects/packages/search/changelog/add-jetpack-search-custom-results create mode 100644 projects/plugins/jetpack/changelog/add-jetpack-search-custom-results create mode 100644 projects/plugins/search/changelog/add-jetpack-search-custom-results diff --git a/projects/packages/search/changelog/add-jetpack-search-custom-results b/projects/packages/search/changelog/add-jetpack-search-custom-results new file mode 100644 index 0000000000000..3efd3577a29ef --- /dev/null +++ b/projects/packages/search/changelog/add-jetpack-search-custom-results @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Search: Add ability to customize results diff --git a/projects/packages/search/src/instant-search/components/search-app.jsx b/projects/packages/search/src/instant-search/components/search-app.jsx index 6847a9be6a2d3..e265a5ff8a45d 100644 --- a/projects/packages/search/src/instant-search/components/search-app.jsx +++ b/projects/packages/search/src/instant-search/components/search-app.jsx @@ -229,6 +229,7 @@ class SearchApp extends Component { postsPerPage: this.props.options.postsPerPage, adminQueryFilter: this.props.options.adminQueryFilter, highlightFields: this.props.options.highlightFields, + customResults: this.props.options.customResults, isInCustomizer: this.props.isInCustomizer, } ); }; diff --git a/projects/packages/search/src/instant-search/components/search-result.jsx b/projects/packages/search/src/instant-search/components/search-result.jsx index 3f2a89ac846e9..cd78103512069 100644 --- a/projects/packages/search/src/instant-search/components/search-result.jsx +++ b/projects/packages/search/src/instant-search/components/search-result.jsx @@ -18,6 +18,17 @@ class SearchResult extends Component { } getCommonTrainTracksProps() { + let uiAlgo = 'jetpack-instant-search-ui/v1'; + if ( this.props.resultFormat === RESULT_FORMAT_PRODUCT ) { + uiAlgo = uiAlgo + '-product'; + } else if ( this.props.resultFormat === RESULT_FORMAT_EXPANDED ) { + uiAlgo = uiAlgo + '-expanded'; + } else { + uiAlgo = uiAlgo + '-minimal'; + } + if ( this.props.result.custom ) { + uiAlgo = uiAlgo + '-custom'; + } return { fetch_algo: this.props.railcar.fetch_algo, fetch_position: this.props.railcar.fetch_position, @@ -26,8 +37,7 @@ class SearchResult extends Component { rec_blog_id: this.props.railcar.rec_blog_id, rec_post_id: this.props.railcar.rec_post_id, session_id: this.props.railcar.session_id, - // TODO: Add a way to differentiate between different result formats - ui_algo: 'jetpack-instant-search-ui/v1', + ui_algo: uiAlgo, ui_position: this.props.index, }; } diff --git a/projects/packages/search/src/instant-search/lib/api.js b/projects/packages/search/src/instant-search/lib/api.js index db50b9b10256d..678b25d1733a8 100644 --- a/projects/packages/search/src/instant-search/lib/api.js +++ b/projects/packages/search/src/instant-search/lib/api.js @@ -263,6 +263,7 @@ function generateApiQueryString( { isInCustomizer = false, additionalBlogIds = [], highlightFields = [ 'title', 'content', 'comments' ], + customResults = [], } ) { if ( query === null ) { query = ''; @@ -333,6 +334,28 @@ function generateApiQueryString( { params.additional_blog_ids = additionalBlogIds; } + // Support customized search results by promoting certain documents to the top for specific queries + // By default we do exact matches, but also allow for regex, if the pattern + // starts with "regex:". For regex, we anchor the pattern to the start and + // end of the query. If the user really wants to match anywhere within the + // query, they can use for example ".*hello.*" + // + customResults.every( rule => { + let pattern = rule.pattern; + const postIds = rule.ids; + if ( pattern.startsWith( 'regex:' ) ) { + pattern = '^' + pattern.replace( 'regex:', '' ) + '$'; + if ( query.match( pattern ) ) { + params.custom_results = postIds; + return false; + } + } else if ( query === pattern ) { + params.custom_results = postIds; + return false; + } + return true; + } ); + if ( staticFilters && Object.keys( staticFilters ).length > 0 ) { params = { ...params, diff --git a/projects/plugins/jetpack/changelog/add-jetpack-search-custom-results b/projects/plugins/jetpack/changelog/add-jetpack-search-custom-results new file mode 100644 index 0000000000000..9aa98f34d497e --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-jetpack-search-custom-results @@ -0,0 +1,4 @@ +Significance: minor +Type: other + +Search: add ability to customize order of results diff --git a/projects/plugins/search/changelog/add-jetpack-search-custom-results b/projects/plugins/search/changelog/add-jetpack-search-custom-results new file mode 100644 index 0000000000000..a9875522e2ff6 --- /dev/null +++ b/projects/plugins/search/changelog/add-jetpack-search-custom-results @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Search: Added ability to customize results diff --git a/projects/plugins/search/jetpack-search.php b/projects/plugins/search/jetpack-search.php index d751aca452ac8..53b82104dd200 100644 --- a/projects/plugins/search/jetpack-search.php +++ b/projects/plugins/search/jetpack-search.php @@ -27,7 +27,6 @@ define( 'JETPACK_SEARCH_PLUGIN__FILE_RELATIVE_PATH', plugin_basename( __FILE__ ) ); define( 'JETPACK_SEARCH_PLUGIN__SLUG', 'jetpack-search' ); define( 'JETPACK_SEARCH_PLUGIN__VERSION', '3.0.1' ); - defined( 'JETPACK_CLIENT__AUTH_LOCATION' ) || define( 'JETPACK_CLIENT__AUTH_LOCATION', 'header' ); defined( 'JETPACK__API_BASE' ) || define( 'JETPACK__API_BASE', 'https://jetpack.wordpress.com/jetpack.' );