Skip to content

Commit

Permalink
Add/jetpack search custom results (#36378)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
robfelty authored Nov 8, 2024
1 parent 9fa1273 commit 54793bd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Search: Add ability to customize results
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
}
Expand Down
23 changes: 23 additions & 0 deletions projects/packages/search/src/instant-search/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ function generateApiQueryString( {
isInCustomizer = false,
additionalBlogIds = [],
highlightFields = [ 'title', 'content', 'comments' ],
customResults = [],
} ) {
if ( query === null ) {
query = '';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Search: add ability to customize order of results
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Search: Added ability to customize results
1 change: 0 additions & 1 deletion projects/plugins/search/jetpack-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
Expand Down

0 comments on commit 54793bd

Please sign in to comment.