Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move search algorithms to separate classes #2880

Merged
merged 17 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions elasticpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ function register_indexable_posts() {
new Feature\Terms\Terms()
);
}

/**
* Register search algorithms
*/
SearchAlgorithms::factory()->register( new SearchAlgorithm\DefaultAlgorithm() );
SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_350() );
SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_400() );
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\register_indexable_posts' );

Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function setup() {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_filter( 'ep_post_mapping', [ $this, 'mapping' ] );
add_filter( 'ep_post_sync_args', [ $this, 'filter_term_suggest' ], 10 );
add_filter( 'ep_fuzziness_arg', [ $this, 'set_fuzziness' ], 10, 3 );
add_filter( 'ep_post_fuzziness_arg', [ $this, 'set_fuzziness' ], 10, 3 );
add_filter( 'ep_weighted_query_for_post_type', [ $this, 'adjust_fuzzy_fields' ], 10, 3 );
add_filter( 'ep_saved_weighting_configuration', [ $this, 'epio_send_autosuggest_public_request' ] );
add_filter( 'wp', [ $this, 'epio_send_autosuggest_allowed' ] );
Expand Down
26 changes: 26 additions & 0 deletions includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,4 +1172,30 @@ public function generate_mapping() {

return [];
}

/**
* Get the search algorithm that should be used.
*
* @since 4.3.0
* @param string $search_text Search term(s)
* @param array $search_fields Search fields
* @param array $query_vars Query vars
* @return SearchAlgorithm Instance of search algorithm to be used
*/
public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm {
/**
* Filter the search algorithm to be used
*
* @hook ep_{$indexable_slug}_search_algorithm
* @since 4.3.0
* @param {string} $search_algorithm Slug of the search algorithm used as fallback
* @param {string} $search_term Search term
* @param {array} $search_fields Fields to be searched
* @param {array} $query_vars Query variables
* @return {string} New search algorithm slug
*/
$search_algorithm = apply_filters( "ep_{$this->slug}_search_algorithm", 'basic', $search_text, $search_fields, $query_vars );

return \ElasticPress\SearchAlgorithms::factory()->get( $search_algorithm );
}
}
80 changes: 2 additions & 78 deletions includes/classes/Indexable/Comment/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,84 +529,8 @@ public function format_args( $query_vars ) {
*/
$prepared_search_fields = apply_filters( 'ep_comment_search_fields', $prepared_search_fields, $query_vars );

$query = [
'bool' => [
'should' => [
[
'multi_match' => [
'query' => $search,
'type' => 'phrase',
'fields' => $prepared_search_fields,
/**
* Filter boost for comment match phrase query
*
* @hook ep_comment_match_phrase_boost
* @since 3.6.0
* @param {int} $boost Phrase boost
* @param {array} $prepared_search_fields Search fields
* @param {array} $query_vars Query variables
* @return {int} New phrase boost
*/
'boost' => apply_filters( 'ep_comment_match_phrase_boost', 4, $prepared_search_fields, $query_vars ),
],
],
[
'multi_match' => [
'query' => $search,
'fields' => $prepared_search_fields,
/**
* Filter boost for comment match query
*
* @hook ep_comment_match_boost
* @param {int} $boost Boost
* @param {array} $prepared_search_fields Search fields
* @param {array} $query_vars Query variables
* @return {int} New boost
*/
'boost' => apply_filters( 'ep_comment_match_boost', 2, $prepared_search_fields, $query_vars ),
'fuzziness' => 0,
'operator' => 'and',
],
],
[
'multi_match' => [
'fields' => $prepared_search_fields,
'query' => $search,
/**
* Filter fuzziness for post query
*
* @hook ep_comment_fuzziness_arg
* @since 3.6.0
* @param {int} $fuzziness Fuzziness
* @param {array} $prepared_search_fields Search fields
* @param {array} $query_vars Query variables
* @return {int} New fuzziness
*/
'fuzziness' => apply_filters( 'ep_comment_fuzziness_arg', 1, $prepared_search_fields, $query_vars ),
],
],
],
],
];

/**
* Filter formatted Elasticsearch post query (only contains query part)
*
* @hook ep_comment_formatted_args_query
* @since 3.6.0
* @param {array} $query Current query
* @param {array} $query_vars Query variables
* @param {string} $search_text Search text
* @param {array} $search_fields Search fields
* @return {array} New query
*/
$formatted_args['query'] = apply_filters(
'ep_comment_formatted_args_query',
$query,
$query_vars,
$search,
$prepared_search_fields
);
$search_algorithm = $this->get_search_algorithm( $search, $prepared_search_fields, $query_vars );
$formatted_args['query'] = $search_algorithm->get_query( 'comment', $search, $prepared_search_fields, $query_vars );
} else {
$formatted_args['query']['match_all'] = [
'boost' => 1,
Expand Down
Loading