From 6cf952ea5008189e48da22346b3cc8f972bb35a8 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 1 Jul 2022 15:43:01 -0300 Subject: [PATCH 01/15] Move search algorithms to separate classes --- includes/classes/Indexable.php | 41 +++ .../classes/Indexable/Comment/Comment.php | 81 +----- includes/classes/Indexable/Post/Post.php | 269 +++++------------- includes/classes/Indexable/Term/Term.php | 72 +---- includes/classes/Indexable/User/User.php | 75 +---- includes/classes/SearchAlgorithm.php | 31 ++ includes/classes/SearchAlgorithm/Basic.php | 112 ++++++++ .../classes/SearchAlgorithm/Version_350.php | 86 ++++++ .../classes/SearchAlgorithm/Version_400.php | 127 +++++++++ 9 files changed, 473 insertions(+), 421 deletions(-) create mode 100644 includes/classes/SearchAlgorithm.php create mode 100644 includes/classes/SearchAlgorithm/Basic.php create mode 100644 includes/classes/SearchAlgorithm/Version_350.php create mode 100644 includes/classes/SearchAlgorithm/Version_400.php diff --git a/includes/classes/Indexable.php b/includes/classes/Indexable.php index 00a620654f..37777da6d7 100644 --- a/includes/classes/Indexable.php +++ b/includes/classes/Indexable.php @@ -1172,4 +1172,45 @@ public function generate_mapping() { return []; } + + /** + * Get the search algorithm class 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 string Class name to be used + */ + public function get_search_algorithm_class( string $search_text, array $search_fields, array $query_vars ) : string { + $fallback_class = '\ElasticPress\SearchAlgorithm\Basic'; + + /** + * Filter the search algorithm class to be used + * + * @hook ep_{$slug}_search_algorithm_class + * @since 4.3.0 + * @param {string} $fallback_class Name of the search algorithm class 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 class name + */ + $search_algorithm_class = apply_filters( "ep_{$this->slug}_search_algorithm_class", $fallback_class, $search_text, $search_fields, $query_vars ); + + if ( ! is_a( $search_algorithm_class, '\ElasticPress\SearchAlgorithm', true ) ) { + _doing_it_wrong( + 'ep_term_search_algorithm_class', + sprintf( + /* translators: `Basic` class name */ + esc_html__( 'This filter only accepts classes that extend %s', 'elasticpress' ), + esc_html( $fallback_class ) + ), + '4.3.0' + ); + $search_algorithm_class = $fallback_class; + } + + return $search_algorithm_class; + } } diff --git a/includes/classes/Indexable/Comment/Comment.php b/includes/classes/Indexable/Comment/Comment.php index eb5312e6e5..495c28d23b 100644 --- a/includes/classes/Indexable/Comment/Comment.php +++ b/includes/classes/Indexable/Comment/Comment.php @@ -529,84 +529,9 @@ 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_class = $this->get_search_algorithm_class( $search, $prepared_search_fields, $query_vars ); + $search_algorithm = new $search_algorithm_class(); + $formatted_args['query'] = $search_algorithm->get_query( 'comment', $search, $prepared_search_fields, $query_vars ); } else { $formatted_args['query']['match_all'] = [ 'boost' => 1, diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 47d45e6771..06aae7cb6e 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1391,190 +1391,8 @@ public function format_args( $args, $wp_query ) { */ $search_fields = apply_filters( 'ep_search_fields', $search_fields, $args ); - $default_algorithm_version = '4.0'; - if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - $search_algorithm_version_option = get_site_option( 'ep_search_algorithm_version', $default_algorithm_version ); - } else { - $search_algorithm_version_option = get_option( 'ep_search_algorithm_version', $default_algorithm_version ); - } - - /** - * Filter the algorithm version to be used. - * - * @since 3.5 - * @hook ep_search_algorithm_version - * @param {string} $search_algorithm_version Algorithm version. - * @return {string} New algorithm version - */ - $search_algorithm_version = apply_filters( 'ep_search_algorithm_version', $search_algorithm_version_option ); - $search_text = ( ! empty( $args['s'] ) ) ? $args['s'] : ''; - if ( '4.0' === $search_algorithm_version ) { - $query = array( - 'bool' => array( - 'should' => array( - array( - 'multi_match' => array( - 'query' => $search_text, - 'type' => 'phrase', - 'fields' => $search_fields, - /** - * Filter boost for post match phrase query - * - * @hook ep_match_phrase_boost - * @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_match_phrase_boost', 3, $search_fields, $args ), - ), - ), - array( - 'multi_match' => array( - 'query' => $search_text, - 'fields' => $search_fields, - /** - * Filter boost for post match query - * - * @hook ep_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_match_boost', 1, $search_fields, $args ), - /** - * Filter fuzziness for post match query - * - * @hook ep_match_fuzziness - * @since 4.0.0 - * @param {string|int} $fuzziness Fuzziness - * @param {array} $prepared_search_fields Search fields - * @param {array} $query_vars Query variables - * @return {string} New boost - */ - 'fuzziness' => apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $args ), - 'operator' => 'and', - ), - ), - array( - 'multi_match' => [ - 'query' => $search_text, - 'type' => 'cross_fields', - 'fields' => $search_fields, - /** - * Filter boost for post match query - * - * @hook ep_match_cross_fields_boost - * @since 4.0.0 - * @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_match_cross_fields_boost', 1, $search_fields, $args ), - 'analyzer' => 'standard', - 'tie_breaker' => 0.5, - 'operator' => 'and', - ], - ), - ), - ), - ); - } elseif ( '3.5' === $search_algorithm_version ) { - $query = array( - 'bool' => array( - 'should' => array( - array( - 'multi_match' => array( - 'query' => $search_text, - 'type' => 'phrase', - 'fields' => $search_fields, - /** - * Filter boost for post match phrase query - * - * @hook ep_match_phrase_boost - * @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_match_phrase_boost', 3, $search_fields, $args ), - ), - ), - array( - 'multi_match' => array( - 'query' => $search_text, - 'fields' => $search_fields, - 'type' => 'phrase', - 'slop' => 5, - ), - ), - ), - ), - ); - } else { - $query = array( - 'bool' => array( - 'should' => array( - array( - 'multi_match' => array( - 'query' => $search_text, - 'type' => 'phrase', - 'fields' => $search_fields, - /** - * Filter boost for post match phrase query - * - * @hook ep_match_phrase_boost - * @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_match_phrase_boost', 4, $search_fields, $args ), - ), - ), - array( - 'multi_match' => array( - 'query' => $search_text, - 'fields' => $search_fields, - /** - * Filter boost for post match query - * - * @hook ep_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_match_boost', 2, $search_fields, $args ), - 'fuzziness' => 0, - 'operator' => 'and', - ), - ), - array( - 'multi_match' => array( - 'query' => $search_text, - 'fields' => $search_fields, - /** - * Filter fuzziness for post query - * - * @hook ep_fuzziness_arg - * @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_fuzziness_arg', 1, $search_fields, $args ), - ), - ), - ), - ), - ); - } - /** * We are using ep_integrate instead of ep_match_all. ep_match_all will be * supported for legacy code but may be deprecated and removed eventually. @@ -1582,28 +1400,12 @@ public function format_args( $args, $wp_query ) { * @since 1.3 */ - if ( ! empty( $args['s'] ) ) { + if ( ! empty( $search_text ) ) { add_filter( 'ep_formatted_args_query', [ $this, 'adjust_query_fuzziness' ], 100, 4 ); - /** - * Filter formatted Elasticsearch post query (only contains query part) - * - * @hook ep_formatted_args_query - * @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 - * - * @since 3.5.5 $search_text and $search_fields parameters added. - */ - $formatted_args['query'] = apply_filters( - 'ep_formatted_args_query', - $query, - $args, - $search_text, - $search_fields - ); + $search_algorithm_class = $this->get_search_algorithm_class( $search_text, $search_fields, $args ); + $search_algorithm = new $search_algorithm_class(); + $formatted_args['query'] = $search_algorithm->get_query( 'post', $search_text, $search_fields, $args ); } elseif ( ! empty( $args['ep_match_all'] ) || ! empty( $args['ep_integrate'] ) ) { $formatted_args['query']['match_all'] = array( 'boost' => 1, @@ -2260,4 +2062,67 @@ protected function apply_aggregations( $formatted_args, $agg, $use_filters, $fil return $formatted_args; } + + /** + * Get the search algorithm class 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 string Class name to be used + */ + public function get_search_algorithm_class( string $search_text, array $search_fields, array $query_vars ) : string { + $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' ); + + /** + * Filter the algorithm version to be used. + * + * @since 3.5 + * @hook ep_search_algorithm_version + * @param {string} $search_algorithm_version Algorithm version. + * @return {string} New algorithm version + */ + $search_algorithm_version = apply_filters( 'ep_search_algorithm_version', $search_algorithm_version_option ); + + switch ( $search_algorithm_version ) { + case '4.0': + $fallback_class = '\ElasticPress\SearchAlgorithm\Version_400'; + break; + case '3.5': + $fallback_class = '\ElasticPress\SearchAlgorithm\Version_350'; + break; + default: + $fallback_class = '\ElasticPress\SearchAlgorithm\Basic'; + break; + } + + /** + * Filter the search algorithm class to be used + * + * @hook ep_{$slug}_search_algorithm_class + * @since 4.3.0 + * @param {string} $fallback_class Name of the search algorithm class 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 class name + */ + $search_algorithm_class = apply_filters( "ep_{$this->slug}_search_algorithm_class", $fallback_class, $search_text, $search_fields, $query_vars ); + + if ( ! is_a( $search_algorithm_class, '\ElasticPress\SearchAlgorithm', true ) ) { + _doing_it_wrong( + 'ep_term_search_algorithm_class', + sprintf( + /* translators: `Basic` class name */ + esc_html__( 'This filter only accepts classes that extend %s', 'elasticpress' ), + esc_html( $fallback_class ) + ), + '4.3.0' + ); + $search_algorithm_class = $fallback_class; + } + + return $search_algorithm_class; + } } diff --git a/includes/classes/Indexable/Term/Term.php b/includes/classes/Indexable/Term/Term.php index 66e6f1a1c2..9d0f85d933 100644 --- a/includes/classes/Indexable/Term/Term.php +++ b/includes/classes/Indexable/Term/Term.php @@ -354,75 +354,9 @@ public function format_args( $query_vars ) { */ $prepared_search_fields = apply_filters( 'ep_term_search_fields', $prepared_search_fields, $query_vars ); - $query = [ - 'bool' => [ - 'should' => [ - [ - 'multi_match' => [ - 'query' => $search, - 'type' => 'phrase', - 'fields' => $prepared_search_fields, - /** - * Filter term match phrase boost amount - * - * @hook ep_term_match_phrase_boost - * @param {int} $boos Boost amount for match phrase - * @param {array} $query_vars Query variables - * @since 3.4 - * @return {int} New boost amount - */ - 'boost' => apply_filters( 'ep_term_match_phrase_boost', 4, $prepared_search_fields, $query_vars ), - ], - ], - [ - 'multi_match' => [ - 'query' => $search, - 'fields' => $prepared_search_fields, - /** - * Filter term match boost amount - * - * @hook ep_term_match_boost - * @param {int} $boost Boost amount for match - * @param {array} $query_vars Query variables - * @since 3.4 - * @return {int} New boost amount - */ - 'boost' => apply_filters( 'ep_term_match_boost', 2, $prepared_search_fields, $query_vars ), - 'fuzziness' => 0, - 'operator' => 'and', - ], - ], - [ - 'multi_match' => [ - 'fields' => $prepared_search_fields, - 'query' => $search, - /** - * Filter term fuzziness amount - * - * @hook ep_term_fuzziness_arg - * @param {int} $fuzziness Amount of fuziness to factor into search - * @param {array} $query_vars Query variables - * @since 3.4 - * @return {int} New boost amount - */ - 'fuzziness' => apply_filters( 'ep_term_fuzziness_arg', 1, $prepared_search_fields, $query_vars ), - ], - ], - ], - ], - ]; - - /** - * Filter Elasticsearch query used for Terms indexable - * - * @hook ep_term_formatted_args_query - * @param {array} $query Elasticsearch query - * @param {array} $query_vars Query variables - * @since 3.4 - * @return {array} New query - */ - $formatted_args['query'] = apply_filters( 'ep_term_formatted_args_query', $query, $query_vars ); - + $search_algorithm_class = $this->get_search_algorithm_class( $search, $prepared_search_fields, $query_vars ); + $search_algorithm = new $search_algorithm_class(); + $formatted_args['query'] = $search_algorithm->get_query( 'term', $search, $prepared_search_fields, $query_vars ); } else { $formatted_args['query']['match_all'] = [ 'boost' => 1, diff --git a/includes/classes/Indexable/User/User.php b/includes/classes/Indexable/User/User.php index 7f9167c415..a220c2cb27 100644 --- a/includes/classes/Indexable/User/User.php +++ b/includes/classes/Indexable/User/User.php @@ -477,78 +477,9 @@ public function format_args( $query_vars, $query ) { */ $prepared_search_fields = apply_filters( 'ep_user_search_fields', $prepared_search_fields, $query_vars ); - $query = array( - 'bool' => array( - 'should' => array( - array( - 'multi_match' => array( - 'query' => $query_vars['search'], - 'type' => 'phrase', - 'fields' => $prepared_search_fields, - /** - * Filter boost for user match phrase query - * - * @hook ep_user_match_phrase_boost - * @param {int} $boost Phrase boost - * @param {array} $prepared_search_fields Search fields - * @param {array} $query_vars Query variables - * @since 3.0 - * @return {int} New phrase boost - */ - 'boost' => apply_filters( 'ep_user_match_phrase_boost', 4, $prepared_search_fields, $query_vars ), - ), - ), - array( - 'multi_match' => array( - 'query' => $query_vars['search'], - 'fields' => $prepared_search_fields, - /** - * Filter boost for user match query - * - * @hook ep_user_match_boost - * @param {int} $boost Boost - * @param {array} $prepared_search_fields Search fields - * @param {array} $query_vars Query variables - * @since 3.0 - * @return {int} New boost - */ - 'boost' => apply_filters( 'ep_user_match_boost', 2, $prepared_search_fields, $query_vars ), - 'fuzziness' => 0, - 'operator' => 'and', - ), - ), - array( - 'multi_match' => array( - 'fields' => $prepared_search_fields, - 'query' => $query_vars['search'], - /** - * Filter fuzziness for user query - * - * @hook ep_user_fuzziness_arg - * @param {int} $fuzziness Fuzziness - * @param {array} $prepared_search_fields Search fields - * @param {array} $query_vars Query variables - * @since 3.0 - * @return {int} New fuzziness - */ - 'fuzziness' => apply_filters( 'ep_user_fuzziness_arg', 1, $prepared_search_fields, $query_vars ), - ), - ), - ), - ), - ); - - /** - * Filter formatted Elasticsearch user query (only contains query part) - * - * @hook ep_user_formatted_args_query - * @param {array} $query Current query - * @param {array} $query_vars Query variables - * @since 3.0 - * @return {array} New query - */ - $formatted_args['query'] = apply_filters( 'ep_user_formatted_args_query', $query, $query_vars ); - + $search_algorithm_class = $this->get_search_algorithm_class( $query_vars['search'], $prepared_search_fields, $query_vars ); + $search_algorithm = new $search_algorithm_class(); + $formatted_args['query'] = $search_algorithm->get_query( 'user', $query_vars['search'], $prepared_search_fields, $query_vars ); } else { $formatted_args['query']['match_all'] = [ 'boost' => 1, diff --git a/includes/classes/SearchAlgorithm.php b/includes/classes/SearchAlgorithm.php new file mode 100644 index 0000000000..37b1f21c69 --- /dev/null +++ b/includes/classes/SearchAlgorithm.php @@ -0,0 +1,31 @@ + [ + 'should' => [ + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'phrase', + 'fields' => $search_fields, + /** + * Filter match phrase boost amount + * + * @hook ep_{$indexable_slug}_match_phrase_boost + * @param {int} $boost Boost amount for match phrase + * @param {array} $query_vars Query variables + * @since 4.3.0 + * @return {int} New boost amount + */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 4, $search_fields, $query_vars ), + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'fields' => $search_fields, + /** + * Filter match boost amount + * + * @hook ep_{$indexable_slug}_match_boost + * @param {int} $boost Boost amount for match + * @param {array} $query_vars Query variables + * @since 4.3.0 + * @return {int} New boost amount + */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_boost", 2, $search_fields, $query_vars ), + 'fuzziness' => 0, + 'operator' => 'and', + ], + ], + [ + 'multi_match' => [ + 'fields' => $search_fields, + 'query' => $search_term, + /** + * Filter fuzziness amount + * + * @hook ep_{$indexable_slug}_fuzziness_arg + * @param {int} $fuzziness Amount of fuziness to factor into search + * @param {array} $query_vars Query variables + * @since 4.3.0 + * @return {int} New boost amount + */ + 'fuzziness' => apply_filters( "ep_{$indexable_slug}_fuzziness_arg", 1, $search_fields, $query_vars ), + ], + ], + ], + ], + ]; + + /** + * Filter formatted Elasticsearch query (only contains query part) + * + * @hook ep_{$indexable_slug}_formatted_args_query + * @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 + * + * @since 4.3.0 + */ + $query = apply_filters( + "ep_{$indexable_slug}_formatted_args_query", + $query, + $query_vars, + $search_term, + $search_fields + ); + + return $query; + } +} diff --git a/includes/classes/SearchAlgorithm/Version_350.php b/includes/classes/SearchAlgorithm/Version_350.php new file mode 100644 index 0000000000..737384e629 --- /dev/null +++ b/includes/classes/SearchAlgorithm/Version_350.php @@ -0,0 +1,86 @@ + [ + 'should' => [ + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'phrase', + 'fields' => $search_fields, + /** + * Filter boost for post match phrase query + * + * @hook ep_match_phrase_boost + * @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_match_phrase_boost', 3, $search_fields, $query_vars ), + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'fields' => $search_fields, + 'type' => 'phrase', + 'slop' => 5, + ], + ], + ], + ], + ]; + + /** + * Filter formatted Elasticsearch query (only contains query part) + * + * @hook ep_{$indexable_slug}_formatted_args_query + * @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 + * + * @since 4.3.0 + */ + $query = apply_filters( + "ep_{$indexable_slug}_formatted_args_query", + $query, + $query_vars, + $search_term, + $search_fields + ); + + return $query; + } +} diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php new file mode 100644 index 0000000000..d5689ab7d3 --- /dev/null +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -0,0 +1,127 @@ + [ + 'should' => [ + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'phrase', + 'fields' => $search_fields, + /** + * Filter boost for post match phrase query + * + * @hook ep_match_phrase_boost + * @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_match_phrase_boost', 3, $search_fields, $query_vars ), + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'fields' => $search_fields, + /** + * Filter boost for post match query + * + * @hook ep_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_match_boost', 1, $search_fields, $query_vars ), + /** + * Filter fuzziness for post match query + * + * @hook ep_match_fuzziness + * @since 4.0.0 + * @param {string|int} $fuzziness Fuzziness + * @param {array} $prepared_search_fields Search fields + * @param {array} $query_vars Query variables + * @return {string} New boost + */ + 'fuzziness' => apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars ), + 'operator' => 'and', + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'cross_fields', + 'fields' => $search_fields, + /** + * Filter boost for post match query + * + * @hook ep_match_cross_fields_boost + * @since 4.0.0 + * @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_match_cross_fields_boost', 1, $search_fields, $query_vars ), + 'analyzer' => 'standard', + 'tie_breaker' => 0.5, + 'operator' => 'and', + ], + ], + ], + ], + ]; + + /** + * Filter formatted Elasticsearch query (only contains query part) + * + * @hook ep_{$indexable_slug}_formatted_args_query + * @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 + * + * @since 4.3.0 + */ + $query = apply_filters( + "ep_{$indexable_slug}_formatted_args_query", + $query, + $query_vars, + $search_term, + $search_fields + ); + + return $query; + } +} From 1207d04089a1fcd0bf2da2389ebbf3c6b8641e57 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 1 Jul 2022 17:53:50 -0300 Subject: [PATCH 02/15] Fix compatibility with post filters --- includes/classes/SearchAlgorithm.php | 62 ++++++++- includes/classes/SearchAlgorithm/Basic.php | 124 +++++++++++------- .../classes/SearchAlgorithm/Version_350.php | 36 +---- .../classes/SearchAlgorithm/Version_400.php | 124 +++++++++--------- 4 files changed, 200 insertions(+), 146 deletions(-) diff --git a/includes/classes/SearchAlgorithm.php b/includes/classes/SearchAlgorithm.php index 37b1f21c69..8645e421d0 100644 --- a/includes/classes/SearchAlgorithm.php +++ b/includes/classes/SearchAlgorithm.php @@ -27,5 +27,65 @@ abstract class SearchAlgorithm { * @param array $query_vars Query vars * @return array ES `query` */ - abstract public function get_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array; + abstract protected function get_raw_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array; + + /** + * Wrapper for the `get_raw_query`, making sure the `ep_{$indexable_slug}_formatted_args_query` filter is applied. + * + * @param string $indexable_slug Indexable slug + * @param string $search_term Search term(s) + * @param array $search_fields Search fields + * @param array $query_vars Query vars + * @return array ES `query` + */ + public function get_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { + $query = $this->get_raw_query( $indexable_slug, $search_term, $search_fields, $query_vars ); + + /** + * Filter formatted Elasticsearch query (only contains query part) + * + * @hook ep_{$indexable_slug}_formatted_args_query + * @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 + * + * @since 4.3.0 + */ + $query = apply_filters( + "ep_{$indexable_slug}_formatted_args_query", + $query, + $query_vars, + $search_term, + $search_fields + ); + + // Backwards-compatibility. + if ( 'post' === $indexable_slug ) { + /** + * Filter formatted Elasticsearch query for posts. + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_formatted_args_query`. + * + * @hook ep_formatted_args_query + * @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 + * + * @since 3.5.5 $search_text and $search_fields parameters added. + */ + $query = apply_filters( + 'ep_formatted_args_query', + $query, + $query_vars, + $search_term, + $search_fields + ); + } + + return $query; + } } diff --git a/includes/classes/SearchAlgorithm/Basic.php b/includes/classes/SearchAlgorithm/Basic.php index a28d1a341b..2187013acc 100644 --- a/includes/classes/SearchAlgorithm/Basic.php +++ b/includes/classes/SearchAlgorithm/Basic.php @@ -28,7 +28,7 @@ class Basic extends \ElasticPress\SearchAlgorithm { * @param array $query_vars Query vars * @return array ES `query` */ - public function get_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { + protected function get_raw_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { $query = [ 'bool' => [ 'should' => [ @@ -37,32 +37,60 @@ public function get_query( string $indexable_slug, string $search_term, array $s 'query' => $search_term, 'type' => 'phrase', 'fields' => $search_fields, - /** - * Filter match phrase boost amount - * - * @hook ep_{$indexable_slug}_match_phrase_boost - * @param {int} $boost Boost amount for match phrase - * @param {array} $query_vars Query variables - * @since 4.3.0 - * @return {int} New boost amount - */ - 'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 4, $search_fields, $query_vars ), + 'boost' => ( 'post' === $indexable_slug ) ? + /** + * Filter boost for post match phrase query. + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_phrase_boost`. + * + * @hook ep_match_phrase_boost + * @param {int} $boost Phrase boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost amount + */ + apply_filters( 'ep_match_phrase_boost', 4, $search_fields, $query_vars ) : + /** + * Filter match phrase boost amount + * + * @since 4.3.0 + * @hook ep_{$indexable_slug}_match_phrase_boost + * @param {int} $boost Phrase boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost amount + */ + apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 4, $search_fields, $query_vars ), ], ], [ 'multi_match' => [ 'query' => $search_term, 'fields' => $search_fields, - /** - * Filter match boost amount - * - * @hook ep_{$indexable_slug}_match_boost - * @param {int} $boost Boost amount for match - * @param {array} $query_vars Query variables - * @since 4.3.0 - * @return {int} New boost amount - */ - 'boost' => apply_filters( "ep_{$indexable_slug}_match_boost", 2, $search_fields, $query_vars ), + 'boost' => ( 'post' === $indexable_slug ) ? + /** + * Filter boost for post match query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_boost`. + * + * @hook ep_match_boost + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + apply_filters( 'ep_match_boost', 2, $search_fields, $query_vars ) : + /** + * Filter match boost amount + * + * @since 4.3.0 + * @hook ep_{$indexable_slug}_match_boost + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + apply_filters( "ep_{$indexable_slug}_match_boost", 2, $search_fields, $query_vars ), 'fuzziness' => 0, 'operator' => 'and', ], @@ -71,42 +99,36 @@ public function get_query( string $indexable_slug, string $search_term, array $s 'multi_match' => [ 'fields' => $search_fields, 'query' => $search_term, - /** - * Filter fuzziness amount - * - * @hook ep_{$indexable_slug}_fuzziness_arg - * @param {int} $fuzziness Amount of fuziness to factor into search - * @param {array} $query_vars Query variables - * @since 4.3.0 - * @return {int} New boost amount - */ - 'fuzziness' => apply_filters( "ep_{$indexable_slug}_fuzziness_arg", 1, $search_fields, $query_vars ), + 'fuzziness' => ( 'post' === $indexable_slug ) ? + /** + * Filter fuzziness for post query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_fuzziness_arg`. + * + * @hook ep_fuzziness_arg + * @param {int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New fuzziness + */ + apply_filters( 'ep_fuzziness_arg', 1, $search_fields, $query_vars ) : + /** + * Filter fuzziness amount + * + * @since 4.3.0 + * @hook ep_{$indexable_slug}_fuzziness_arg + * @param {int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New fuzziness + */ + apply_filters( "ep_{$indexable_slug}_fuzziness_arg", 1, $search_fields, $query_vars ), ], ], ], ], ]; - /** - * Filter formatted Elasticsearch query (only contains query part) - * - * @hook ep_{$indexable_slug}_formatted_args_query - * @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 - * - * @since 4.3.0 - */ - $query = apply_filters( - "ep_{$indexable_slug}_formatted_args_query", - $query, - $query_vars, - $search_term, - $search_fields - ); - return $query; } } diff --git a/includes/classes/SearchAlgorithm/Version_350.php b/includes/classes/SearchAlgorithm/Version_350.php index 737384e629..665df0c71e 100644 --- a/includes/classes/SearchAlgorithm/Version_350.php +++ b/includes/classes/SearchAlgorithm/Version_350.php @@ -28,7 +28,7 @@ class Version_350 extends \ElasticPress\SearchAlgorithm { * @param array $query_vars Query vars * @return array ES `query` */ - public function get_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { + protected function get_raw_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { $query = [ 'bool' => [ 'should' => [ @@ -37,16 +37,10 @@ public function get_query( string $indexable_slug, string $search_term, array $s 'query' => $search_term, 'type' => 'phrase', 'fields' => $search_fields, - /** - * Filter boost for post match phrase query - * - * @hook ep_match_phrase_boost - * @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_match_phrase_boost', 3, $search_fields, $query_vars ), + 'boost' => ( 'post' === $indexable_slug ) ? + /** These filters are documented in /includes/classes/SearchAlgorithm/Basic.php */ + apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ) : + apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), ], ], [ @@ -61,26 +55,6 @@ public function get_query( string $indexable_slug, string $search_term, array $s ], ]; - /** - * Filter formatted Elasticsearch query (only contains query part) - * - * @hook ep_{$indexable_slug}_formatted_args_query - * @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 - * - * @since 4.3.0 - */ - $query = apply_filters( - "ep_{$indexable_slug}_formatted_args_query", - $query, - $query_vars, - $search_term, - $search_fields - ); - return $query; } } diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php index d5689ab7d3..f7c579fb5b 100644 --- a/includes/classes/SearchAlgorithm/Version_400.php +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -28,7 +28,7 @@ class Version_400 extends \ElasticPress\SearchAlgorithm { * @param array $query_vars Query vars * @return array ES `query` */ - public function get_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { + protected function get_raw_query( string $indexable_slug, string $search_term, array $search_fields, array $query_vars ) : array { $query = [ 'bool' => [ 'should' => [ @@ -37,44 +37,46 @@ public function get_query( string $indexable_slug, string $search_term, array $s 'query' => $search_term, 'type' => 'phrase', 'fields' => $search_fields, - /** - * Filter boost for post match phrase query - * - * @hook ep_match_phrase_boost - * @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_match_phrase_boost', 3, $search_fields, $query_vars ), + 'boost' => ( 'post' === $indexable_slug ) ? + /** These filters are documented in /includes/classes/SearchAlgorithm/Basic.php */ + apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ) : + apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), ], ], [ 'multi_match' => [ 'query' => $search_term, 'fields' => $search_fields, - /** - * Filter boost for post match query - * - * @hook ep_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_match_boost', 1, $search_fields, $query_vars ), - /** - * Filter fuzziness for post match query - * - * @hook ep_match_fuzziness - * @since 4.0.0 - * @param {string|int} $fuzziness Fuzziness - * @param {array} $prepared_search_fields Search fields - * @param {array} $query_vars Query variables - * @return {string} New boost - */ - 'fuzziness' => apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars ), 'operator' => 'and', + 'boost' => ( 'post' === $indexable_slug ) ? + /** These filters are documented in /includes/classes/SearchAlgorithm/Basic.php */ + apply_filters( 'ep_match_boost', 1, $search_fields, $query_vars ) : + apply_filters( "ep_{$indexable_slug}_match_boost", 1, $search_fields, $query_vars ), + 'fuzziness' => ( 'post' === $indexable_slug ) ? + /** + * Filter fuzziness for post match query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. + * + * @hook ep_match_fuzziness + * @since 4.0.0 + * @param {string|int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {string} New fuzziness + */ + apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars ) : + /** + * Filter fuzziness for match query + * + * @hook ep_{$indexable_slug}_match_fuzziness + * @since 4.3.0 + * @param {string|int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {string} New fuzziness + */ + apply_filters( "ep_{$indexable_slug}_match_fuzziness", 'auto', $search_fields, $query_vars ), ], ], [ @@ -82,17 +84,33 @@ public function get_query( string $indexable_slug, string $search_term, array $s 'query' => $search_term, 'type' => 'cross_fields', 'fields' => $search_fields, - /** - * Filter boost for post match query - * - * @hook ep_match_cross_fields_boost - * @since 4.0.0 - * @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_match_cross_fields_boost', 1, $search_fields, $query_vars ), + 'boost' => ( 'post' === $indexable_slug ) ? + /** + * Filter boost for post match cross_fields query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. + * + * @hook ep_match_cross_fields_boost + * @since 4.0.0 + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + apply_filters( 'ep_match_cross_fields_boost', 1, $search_fields, $query_vars ) : + /** + * Filter boost for post match cross_fields query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. + * + * @hook ep_{$indexable_slug}_match_cross_fields_boost + * @since 4.3.0 + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + apply_filters( "ep_{$indexable_slug}_match_cross_fields_boost", 1, $search_fields, $query_vars ), 'analyzer' => 'standard', 'tie_breaker' => 0.5, 'operator' => 'and', @@ -102,26 +120,6 @@ public function get_query( string $indexable_slug, string $search_term, array $s ], ]; - /** - * Filter formatted Elasticsearch query (only contains query part) - * - * @hook ep_{$indexable_slug}_formatted_args_query - * @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 - * - * @since 4.3.0 - */ - $query = apply_filters( - "ep_{$indexable_slug}_formatted_args_query", - $query, - $query_vars, - $search_term, - $search_fields - ); - return $query; } } From 922886caa594b71a212994533a77002f4d4b79b1 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 11:51:49 -0300 Subject: [PATCH 03/15] Move post filters to a separate function --- includes/classes/SearchAlgorithm/Basic.php | 163 ++++++++++-------- .../classes/SearchAlgorithm/Version_350.php | 28 ++- .../classes/SearchAlgorithm/Version_400.php | 141 ++++++++------- 3 files changed, 196 insertions(+), 136 deletions(-) diff --git a/includes/classes/SearchAlgorithm/Basic.php b/includes/classes/SearchAlgorithm/Basic.php index 2187013acc..30b1b1ff80 100644 --- a/includes/classes/SearchAlgorithm/Basic.php +++ b/includes/classes/SearchAlgorithm/Basic.php @@ -37,60 +37,34 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'query' => $search_term, 'type' => 'phrase', 'fields' => $search_fields, - 'boost' => ( 'post' === $indexable_slug ) ? - /** - * Filter boost for post match phrase query. - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_phrase_boost`. - * - * @hook ep_match_phrase_boost - * @param {int} $boost Phrase boost - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New boost amount - */ - apply_filters( 'ep_match_phrase_boost', 4, $search_fields, $query_vars ) : - /** - * Filter match phrase boost amount - * - * @since 4.3.0 - * @hook ep_{$indexable_slug}_match_phrase_boost - * @param {int} $boost Phrase boost - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New boost amount - */ - apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 4, $search_fields, $query_vars ), + /** + * Filter match phrase boost amount + * + * @since 4.3.0 + * @hook ep_{$indexable_slug}_match_phrase_boost + * @param {int} $boost Phrase boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost amount + */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 4, $search_fields, $query_vars ), ], ], [ 'multi_match' => [ 'query' => $search_term, 'fields' => $search_fields, - 'boost' => ( 'post' === $indexable_slug ) ? - /** - * Filter boost for post match query - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_boost`. - * - * @hook ep_match_boost - * @param {int} $boost Boost - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New boost - */ - apply_filters( 'ep_match_boost', 2, $search_fields, $query_vars ) : - /** - * Filter match boost amount - * - * @since 4.3.0 - * @hook ep_{$indexable_slug}_match_boost - * @param {int} $boost Boost - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New boost - */ - apply_filters( "ep_{$indexable_slug}_match_boost", 2, $search_fields, $query_vars ), + /** + * Filter match boost amount + * + * @since 4.3.0 + * @hook ep_{$indexable_slug}_match_boost + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_boost", 2, $search_fields, $query_vars ), 'fuzziness' => 0, 'operator' => 'and', ], @@ -99,36 +73,81 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'multi_match' => [ 'fields' => $search_fields, 'query' => $search_term, - 'fuzziness' => ( 'post' === $indexable_slug ) ? - /** - * Filter fuzziness for post query - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_fuzziness_arg`. - * - * @hook ep_fuzziness_arg - * @param {int} $fuzziness Fuzziness - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New fuzziness - */ - apply_filters( 'ep_fuzziness_arg', 1, $search_fields, $query_vars ) : - /** - * Filter fuzziness amount - * - * @since 4.3.0 - * @hook ep_{$indexable_slug}_fuzziness_arg - * @param {int} $fuzziness Fuzziness - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New fuzziness - */ - apply_filters( "ep_{$indexable_slug}_fuzziness_arg", 1, $search_fields, $query_vars ), + /** + * Filter fuzziness amount + * + * @since 4.3.0 + * @hook ep_{$indexable_slug}_fuzziness_arg + * @param {int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New fuzziness + */ + 'fuzziness' => apply_filters( "ep_{$indexable_slug}_fuzziness_arg", 1, $search_fields, $query_vars ), ], ], ], ], ]; + $query = $this->apply_legacy_filters( $query, $indexable_slug, $search_fields, $query_vars ); + + return $query; + } + + /** + * Apply legacy filters. + * + * @param array $query ES `query` + * @param string $indexable_slug Indexable slug + * @param array $search_fields Search term(s) + * @param array $query_vars Query vars + * @return array ES `query` + */ + protected function apply_legacy_filters( array $query, string $indexable_slug, array $search_fields, array $query_vars ) : array { + if ( 'post' !== $indexable_slug ) { + return $query; + } + + /** + * Filter boost for post match phrase query. + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_phrase_boost`. + * + * @hook ep_match_phrase_boost + * @param {int} $boost Phrase boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost amount + */ + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters( 'ep_match_phrase_boost', 4, $search_fields, $query_vars ); + + /** + * Filter boost for post match query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_boost`. + * + * @hook ep_match_boost + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + $query['bool']['should'][1]['multi_match']['boost'] = apply_filters( 'ep_match_boost', 2, $search_fields, $query_vars ); + + /** + * Filter fuzziness for post query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_fuzziness_arg`. + * + * @hook ep_fuzziness_arg + * @param {int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New fuzziness + */ + $query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters( 'ep_fuzziness_arg', 1, $search_fields, $query_vars ); + return $query; } } diff --git a/includes/classes/SearchAlgorithm/Version_350.php b/includes/classes/SearchAlgorithm/Version_350.php index 665df0c71e..4e2115f5ba 100644 --- a/includes/classes/SearchAlgorithm/Version_350.php +++ b/includes/classes/SearchAlgorithm/Version_350.php @@ -37,10 +37,8 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'query' => $search_term, 'type' => 'phrase', 'fields' => $search_fields, - 'boost' => ( 'post' === $indexable_slug ) ? - /** These filters are documented in /includes/classes/SearchAlgorithm/Basic.php */ - apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ) : - apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), + /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), ], ], [ @@ -55,6 +53,28 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a ], ]; + $query = $this->apply_legacy_filters( $query, $indexable_slug, $search_fields, $query_vars ); + + return $query; + } + + /** + * Apply legacy filters. + * + * @param array $query ES `query` + * @param string $indexable_slug Indexable slug + * @param array $search_fields Search term(s) + * @param array $query_vars Query vars + * @return array ES `query` + */ + protected function apply_legacy_filters( array $query, string $indexable_slug, array $search_fields, array $query_vars ) : array { + if ( 'post' !== $indexable_slug ) { + return $query; + } + + /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ); + return $query; } } diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php index f7c579fb5b..63a9ae9edb 100644 --- a/includes/classes/SearchAlgorithm/Version_400.php +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -37,10 +37,8 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'query' => $search_term, 'type' => 'phrase', 'fields' => $search_fields, - 'boost' => ( 'post' === $indexable_slug ) ? - /** These filters are documented in /includes/classes/SearchAlgorithm/Basic.php */ - apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ) : - apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), + /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_phrase_boost", 3, $search_fields, $query_vars ), ], ], [ @@ -48,35 +46,19 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'query' => $search_term, 'fields' => $search_fields, 'operator' => 'and', - 'boost' => ( 'post' === $indexable_slug ) ? - /** These filters are documented in /includes/classes/SearchAlgorithm/Basic.php */ - apply_filters( 'ep_match_boost', 1, $search_fields, $query_vars ) : - apply_filters( "ep_{$indexable_slug}_match_boost", 1, $search_fields, $query_vars ), - 'fuzziness' => ( 'post' === $indexable_slug ) ? - /** - * Filter fuzziness for post match query - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. - * - * @hook ep_match_fuzziness - * @since 4.0.0 - * @param {string|int} $fuzziness Fuzziness - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {string} New fuzziness - */ - apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars ) : - /** - * Filter fuzziness for match query - * - * @hook ep_{$indexable_slug}_match_fuzziness - * @since 4.3.0 - * @param {string|int} $fuzziness Fuzziness - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {string} New fuzziness - */ - apply_filters( "ep_{$indexable_slug}_match_fuzziness", 'auto', $search_fields, $query_vars ), + /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_boost", 1, $search_fields, $query_vars ), + /** + * Filter fuzziness for match query + * + * @hook ep_{$indexable_slug}_match_fuzziness + * @since 4.3.0 + * @param {string|int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {string} New fuzziness + */ + 'fuzziness' => apply_filters( "ep_{$indexable_slug}_match_fuzziness", 'auto', $search_fields, $query_vars ), ], ], [ @@ -84,33 +66,19 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'query' => $search_term, 'type' => 'cross_fields', 'fields' => $search_fields, - 'boost' => ( 'post' === $indexable_slug ) ? - /** - * Filter boost for post match cross_fields query - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. - * - * @hook ep_match_cross_fields_boost - * @since 4.0.0 - * @param {int} $boost Boost - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New boost - */ - apply_filters( 'ep_match_cross_fields_boost', 1, $search_fields, $query_vars ) : - /** - * Filter boost for post match cross_fields query - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. - * - * @hook ep_{$indexable_slug}_match_cross_fields_boost - * @since 4.3.0 - * @param {int} $boost Boost - * @param {array} $search_fields Search fields - * @param {array} $query_vars Query variables - * @return {int} New boost - */ - apply_filters( "ep_{$indexable_slug}_match_cross_fields_boost", 1, $search_fields, $query_vars ), + /** + * Filter boost for post match cross_fields query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. + * + * @hook ep_{$indexable_slug}_match_cross_fields_boost + * @since 4.3.0 + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + 'boost' => apply_filters( "ep_{$indexable_slug}_match_cross_fields_boost", 1, $search_fields, $query_vars ), 'analyzer' => 'standard', 'tie_breaker' => 0.5, 'operator' => 'and', @@ -120,6 +88,59 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a ], ]; + $query = $this->apply_legacy_filters( $query, $indexable_slug, $search_fields, $query_vars ); + + return $query; + } + + /** + * Apply legacy filters. + * + * @param array $query ES `query` + * @param string $indexable_slug Indexable slug + * @param array $search_fields Search term(s) + * @param array $query_vars Query vars + * @return array ES `query` + */ + protected function apply_legacy_filters( array $query, string $indexable_slug, array $search_fields, array $query_vars ) : array { + if ( 'post' !== $indexable_slug ) { + return $query; + } + + /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ); + + /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ + $query['bool']['should'][1]['multi_match']['boost'] = apply_filters( 'ep_match_boost', 1, $search_fields, $query_vars ); + + /** + * Filter fuzziness for post match query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. + * + * @hook ep_match_fuzziness + * @since 4.0.0 + * @param {string|int} $fuzziness Fuzziness + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {string} New fuzziness + */ + $query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars ); + + /** + * Filter boost for post match cross_fields query + * + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. + * + * @hook ep_match_cross_fields_boost + * @since 4.0.0 + * @param {int} $boost Boost + * @param {array} $search_fields Search fields + * @param {array} $query_vars Query variables + * @return {int} New boost + */ + $query['bool']['should'][2]['multi_match']['boost'] = apply_filters( 'ep_match_cross_fields_boost', 1, $search_fields, $query_vars ); + return $query; } } From b4d317cf8aef7e1e830842fff9f8f06450a4f8da Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 11:55:34 -0300 Subject: [PATCH 04/15] Call `apply_filters_deprecated` on legacy filters --- includes/classes/SearchAlgorithm/Basic.php | 6 +++--- .../classes/SearchAlgorithm/Version_350.php | 2 +- .../classes/SearchAlgorithm/Version_400.php | 18 ++++++++---------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/includes/classes/SearchAlgorithm/Basic.php b/includes/classes/SearchAlgorithm/Basic.php index 30b1b1ff80..c6e590e133 100644 --- a/includes/classes/SearchAlgorithm/Basic.php +++ b/includes/classes/SearchAlgorithm/Basic.php @@ -120,7 +120,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New boost amount */ - $query['bool']['should'][0]['multi_match']['boost'] = apply_filters( 'ep_match_phrase_boost', 4, $search_fields, $query_vars ); + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', 4, $search_fields, $query_vars, 'ep_post_match_phrase_boost' ); /** * Filter boost for post match query @@ -133,7 +133,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New boost */ - $query['bool']['should'][1]['multi_match']['boost'] = apply_filters( 'ep_match_boost', 2, $search_fields, $query_vars ); + $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_boost', 2, $search_fields, $query_vars, 'ep_post_match_boost' ); /** * Filter fuzziness for post query @@ -146,7 +146,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New fuzziness */ - $query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters( 'ep_fuzziness_arg', 1, $search_fields, $query_vars ); + $query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters_deprecated( 'ep_fuzziness_arg', 1, $search_fields, $query_vars, 'ep_post_fuzziness_arg' ); return $query; } diff --git a/includes/classes/SearchAlgorithm/Version_350.php b/includes/classes/SearchAlgorithm/Version_350.php index 4e2115f5ba..762353957f 100644 --- a/includes/classes/SearchAlgorithm/Version_350.php +++ b/includes/classes/SearchAlgorithm/Version_350.php @@ -73,7 +73,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a } /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ - $query['bool']['should'][0]['multi_match']['boost'] = apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ); + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', 3, $search_fields, $query_vars, 'ep_post_match_phrase_boost' ); return $query; } diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php index 63a9ae9edb..3f1116ea63 100644 --- a/includes/classes/SearchAlgorithm/Version_400.php +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -67,9 +67,7 @@ protected function get_raw_query( string $indexable_slug, string $search_term, a 'type' => 'cross_fields', 'fields' => $search_fields, /** - * Filter boost for post match cross_fields query - * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. + * Filter boost for match cross_fields query * * @hook ep_{$indexable_slug}_match_cross_fields_boost * @since 4.3.0 @@ -108,10 +106,10 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a } /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ - $query['bool']['should'][0]['multi_match']['boost'] = apply_filters( 'ep_match_phrase_boost', 3, $search_fields, $query_vars ); + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', 3, $search_fields, $query_vars, 'ep_post_match_phrase_boost' ); /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ - $query['bool']['should'][1]['multi_match']['boost'] = apply_filters( 'ep_match_boost', 1, $search_fields, $query_vars ); + $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_boost', 1, $search_fields, $query_vars, 'ep_post_match_boost' ); /** * Filter fuzziness for post match query @@ -125,21 +123,21 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {string} New fuzziness */ - $query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars ); + $query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters_deprecated( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars, 'ep_post_match_fuzziness' ); /** * Filter boost for post match cross_fields query * - * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_fuzziness`. + * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. * - * @hook ep_match_cross_fields_boost - * @since 4.0.0 + * @hook ep_{$indexable_slug}_match_cross_fields_boost + * @since 4.3.0 * @param {int} $boost Boost * @param {array} $search_fields Search fields * @param {array} $query_vars Query variables * @return {int} New boost */ - $query['bool']['should'][2]['multi_match']['boost'] = apply_filters( 'ep_match_cross_fields_boost', 1, $search_fields, $query_vars ); + $query['bool']['should'][2]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_cross_fields_boost', 1, $search_fields, $query_vars, 'ep_post_match_cross_fields_boost' ); return $query; } From f596f57de119aec5eab15b2d82cbda0e7859a813 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 12:16:02 -0300 Subject: [PATCH 05/15] Fix apply_filters_deprecated calls --- includes/classes/SearchAlgorithm/Basic.php | 18 +++++++++++--- .../classes/SearchAlgorithm/Version_350.php | 6 ++++- .../classes/SearchAlgorithm/Version_400.php | 24 +++++++++++++++---- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/includes/classes/SearchAlgorithm/Basic.php b/includes/classes/SearchAlgorithm/Basic.php index c6e590e133..a3d022164b 100644 --- a/includes/classes/SearchAlgorithm/Basic.php +++ b/includes/classes/SearchAlgorithm/Basic.php @@ -120,7 +120,11 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New boost amount */ - $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', 4, $search_fields, $query_vars, 'ep_post_match_phrase_boost' ); + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( + 'ep_match_phrase_boost', + [ 4, $search_fields, $query_vars ], + 'ep_post_match_phrase_boost' + ); /** * Filter boost for post match query @@ -133,7 +137,11 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New boost */ - $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_boost', 2, $search_fields, $query_vars, 'ep_post_match_boost' ); + $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( + 'ep_match_boost', + [ 2, $search_fields, $query_vars ], + 'ep_post_match_boost' + ); /** * Filter fuzziness for post query @@ -146,7 +154,11 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New fuzziness */ - $query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters_deprecated( 'ep_fuzziness_arg', 1, $search_fields, $query_vars, 'ep_post_fuzziness_arg' ); + $query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters_deprecated( + 'ep_fuzziness_arg', + [ 1, $search_fields, $query_vars ], + 'ep_post_fuzziness_arg' + ); return $query; } diff --git a/includes/classes/SearchAlgorithm/Version_350.php b/includes/classes/SearchAlgorithm/Version_350.php index 762353957f..67a70b50e5 100644 --- a/includes/classes/SearchAlgorithm/Version_350.php +++ b/includes/classes/SearchAlgorithm/Version_350.php @@ -73,7 +73,11 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a } /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ - $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', 3, $search_fields, $query_vars, 'ep_post_match_phrase_boost' ); + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( + 'ep_match_phrase_boost', + [ 3, $search_fields, $query_vars ], + 'ep_post_match_phrase_boost' + ); return $query; } diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php index 3f1116ea63..1dd611c1dd 100644 --- a/includes/classes/SearchAlgorithm/Version_400.php +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -106,10 +106,18 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a } /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ - $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', 3, $search_fields, $query_vars, 'ep_post_match_phrase_boost' ); + $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( + 'ep_match_phrase_boost', + [ 3, $search_fields, $query_vars ], + 'ep_post_match_phrase_boost' + ); /** This filter is documented in /includes/classes/SearchAlgorithm/Basic.php */ - $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_boost', 1, $search_fields, $query_vars, 'ep_post_match_boost' ); + $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( + 'ep_match_boost', + [ 1, $search_fields, $query_vars ], + 'ep_post_match_boost' + ); /** * Filter fuzziness for post match query @@ -123,7 +131,11 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {string} New fuzziness */ - $query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters_deprecated( 'ep_match_fuzziness', 'auto', $search_fields, $query_vars, 'ep_post_match_fuzziness' ); + $query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters_deprecated( + 'ep_match_fuzziness', + [ 'auto', $search_fields, $query_vars ], + 'ep_post_match_fuzziness' + ); /** * Filter boost for post match cross_fields query @@ -137,7 +149,11 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * @param {array} $query_vars Query variables * @return {int} New boost */ - $query['bool']['should'][2]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_cross_fields_boost', 1, $search_fields, $query_vars, 'ep_post_match_cross_fields_boost' ); + $query['bool']['should'][2]['multi_match']['boost'] = apply_filters_deprecated( + 'ep_match_cross_fields_boost', + [ 1, $search_fields, $query_vars ], + 'ep_post_match_cross_fields_boost' + ); return $query; } From c37129df3a00a6348176b727831cd7ea86b86149 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 13:21:38 -0300 Subject: [PATCH 06/15] Fix `@since` tag --- includes/classes/SearchAlgorithm/Version_400.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php index 1dd611c1dd..d35f9cb034 100644 --- a/includes/classes/SearchAlgorithm/Version_400.php +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -143,7 +143,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a * This filter exists to keep backwards-compatibility. Newer implementations should use `ep_post_match_cross_fields_boost`. * * @hook ep_{$indexable_slug}_match_cross_fields_boost - * @since 4.3.0 + * @since 4.0.0 * @param {int} $boost Boost * @param {array} $search_fields Search fields * @param {array} $query_vars Query variables From ef77bbdc710a3ca9b88dbd5926fc7b21579a1711 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 14:24:35 -0300 Subject: [PATCH 07/15] SearchAlgorithms factory --- elasticpress.php | 7 ++ includes/classes/Indexable.php | 39 +++------- .../classes/Indexable/Comment/Comment.php | 3 +- includes/classes/Indexable/Post/Post.php | 54 ++++--------- includes/classes/Indexable/Term/Term.php | 3 +- includes/classes/Indexable/User/User.php | 3 +- includes/classes/SearchAlgorithm.php | 30 ++++++-- includes/classes/SearchAlgorithm/Basic.php | 29 +++++++ .../classes/SearchAlgorithm/Version_350.php | 27 +++++++ .../classes/SearchAlgorithm/Version_400.php | 30 ++++++++ includes/classes/SearchAlgorithms.php | 76 +++++++++++++++++++ 11 files changed, 223 insertions(+), 78 deletions(-) create mode 100644 includes/classes/SearchAlgorithms.php diff --git a/elasticpress.php b/elasticpress.php index 4f5ac13c49..4cdb6159fa 100644 --- a/elasticpress.php +++ b/elasticpress.php @@ -157,6 +157,13 @@ function register_indexable_posts() { new Feature\Terms\Terms() ); } + + /** + * Register search algorithms + */ + SearchAlgorithms::factory()->register( new SearchAlgorithm\Basic() ); + SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_350() ); + SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_400() ); } add_action( 'plugins_loaded', __NAMESPACE__ . '\register_indexable_posts' ); diff --git a/includes/classes/Indexable.php b/includes/classes/Indexable.php index 37777da6d7..05f27090f2 100644 --- a/includes/classes/Indexable.php +++ b/includes/classes/Indexable.php @@ -1174,43 +1174,28 @@ public function generate_mapping() { } /** - * Get the search algorithm class that should be used. + * 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 string Class name to be used + * @return SearchAlgorithm Instance of search algorithm to be used */ - public function get_search_algorithm_class( string $search_text, array $search_fields, array $query_vars ) : string { - $fallback_class = '\ElasticPress\SearchAlgorithm\Basic'; - + public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm { /** - * Filter the search algorithm class to be used + * Filter the search algorithm to be used * - * @hook ep_{$slug}_search_algorithm_class + * @hook ep_{$indexable_slug}_search_algorithm * @since 4.3.0 - * @param {string} $fallback_class Name of the search algorithm class 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 class name + * @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_class = apply_filters( "ep_{$this->slug}_search_algorithm_class", $fallback_class, $search_text, $search_fields, $query_vars ); - - if ( ! is_a( $search_algorithm_class, '\ElasticPress\SearchAlgorithm', true ) ) { - _doing_it_wrong( - 'ep_term_search_algorithm_class', - sprintf( - /* translators: `Basic` class name */ - esc_html__( 'This filter only accepts classes that extend %s', 'elasticpress' ), - esc_html( $fallback_class ) - ), - '4.3.0' - ); - $search_algorithm_class = $fallback_class; - } + $search_algorithm = apply_filters( "ep_{$this->slug}_search_algorithm", 'basic', $search_text, $search_fields, $query_vars ); - return $search_algorithm_class; + return \ElasticPress\SearchAlgorithms::factory()->get( $search_algorithm ); } } diff --git a/includes/classes/Indexable/Comment/Comment.php b/includes/classes/Indexable/Comment/Comment.php index 495c28d23b..09fe91f660 100644 --- a/includes/classes/Indexable/Comment/Comment.php +++ b/includes/classes/Indexable/Comment/Comment.php @@ -529,8 +529,7 @@ public function format_args( $query_vars ) { */ $prepared_search_fields = apply_filters( 'ep_comment_search_fields', $prepared_search_fields, $query_vars ); - $search_algorithm_class = $this->get_search_algorithm_class( $search, $prepared_search_fields, $query_vars ); - $search_algorithm = new $search_algorithm_class(); + $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'] = [ diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 06aae7cb6e..b9c3940b92 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1401,10 +1401,9 @@ public function format_args( $args, $wp_query ) { */ if ( ! empty( $search_text ) ) { - add_filter( 'ep_formatted_args_query', [ $this, 'adjust_query_fuzziness' ], 100, 4 ); + add_filter( 'ep_post_formatted_args_query', [ $this, 'adjust_query_fuzziness' ], 100, 4 ); - $search_algorithm_class = $this->get_search_algorithm_class( $search_text, $search_fields, $args ); - $search_algorithm = new $search_algorithm_class(); + $search_algorithm = $this->get_search_algorithm( $search_text, $search_fields, $args ); $formatted_args['query'] = $search_algorithm->get_query( 'post', $search_text, $search_fields, $args ); } elseif ( ! empty( $args['ep_match_all'] ) || ! empty( $args['ep_integrate'] ) ) { $formatted_args['query']['match_all'] = array( @@ -2070,9 +2069,9 @@ protected function apply_aggregations( $formatted_args, $agg, $use_filters, $fil * @param string $search_text Search term(s) * @param array $search_fields Search fields * @param array $query_vars Query vars - * @return string Class name to be used + * @return SearchAlgorithm Instance of search algorithm to be used */ - public function get_search_algorithm_class( string $search_text, array $search_fields, array $query_vars ) : string { + public function get_search_algorithm_class( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm { $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' ); /** @@ -2083,46 +2082,21 @@ public function get_search_algorithm_class( string $search_text, array $search_f * @param {string} $search_algorithm_version Algorithm version. * @return {string} New algorithm version */ - $search_algorithm_version = apply_filters( 'ep_search_algorithm_version', $search_algorithm_version_option ); - - switch ( $search_algorithm_version ) { - case '4.0': - $fallback_class = '\ElasticPress\SearchAlgorithm\Version_400'; - break; - case '3.5': - $fallback_class = '\ElasticPress\SearchAlgorithm\Version_350'; - break; - default: - $fallback_class = '\ElasticPress\SearchAlgorithm\Basic'; - break; - } + $search_algorithm = apply_filters( 'ep_search_algorithm_version', $search_algorithm_version_option ); /** - * Filter the search algorithm class to be used + * Filter the search algorithm to be used * - * @hook ep_{$slug}_search_algorithm_class + * @hook ep_{$indexable_slug}_search_algorithm * @since 4.3.0 - * @param {string} $fallback_class Name of the search algorithm class 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 class name + * @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_class = apply_filters( "ep_{$this->slug}_search_algorithm_class", $fallback_class, $search_text, $search_fields, $query_vars ); - - if ( ! is_a( $search_algorithm_class, '\ElasticPress\SearchAlgorithm', true ) ) { - _doing_it_wrong( - 'ep_term_search_algorithm_class', - sprintf( - /* translators: `Basic` class name */ - esc_html__( 'This filter only accepts classes that extend %s', 'elasticpress' ), - esc_html( $fallback_class ) - ), - '4.3.0' - ); - $search_algorithm_class = $fallback_class; - } + $search_algorithm = apply_filters( "ep_{$this->slug}_search_algorithm", $search_algorithm, $search_text, $search_fields, $query_vars ); - return $search_algorithm_class; + return \ElasticPress\SearchAlgorithms::factory()->get( $search_algorithm ); } } diff --git a/includes/classes/Indexable/Term/Term.php b/includes/classes/Indexable/Term/Term.php index 9d0f85d933..197fbd346a 100644 --- a/includes/classes/Indexable/Term/Term.php +++ b/includes/classes/Indexable/Term/Term.php @@ -354,8 +354,7 @@ public function format_args( $query_vars ) { */ $prepared_search_fields = apply_filters( 'ep_term_search_fields', $prepared_search_fields, $query_vars ); - $search_algorithm_class = $this->get_search_algorithm_class( $search, $prepared_search_fields, $query_vars ); - $search_algorithm = new $search_algorithm_class(); + $search_algorithm = $this->get_search_algorithm( $search, $prepared_search_fields, $query_vars ); $formatted_args['query'] = $search_algorithm->get_query( 'term', $search, $prepared_search_fields, $query_vars ); } else { $formatted_args['query']['match_all'] = [ diff --git a/includes/classes/Indexable/User/User.php b/includes/classes/Indexable/User/User.php index a220c2cb27..54d5052ba0 100644 --- a/includes/classes/Indexable/User/User.php +++ b/includes/classes/Indexable/User/User.php @@ -477,8 +477,7 @@ public function format_args( $query_vars, $query ) { */ $prepared_search_fields = apply_filters( 'ep_user_search_fields', $prepared_search_fields, $query_vars ); - $search_algorithm_class = $this->get_search_algorithm_class( $query_vars['search'], $prepared_search_fields, $query_vars ); - $search_algorithm = new $search_algorithm_class(); + $search_algorithm = $this->get_search_algorithm( $query_vars['search'], $prepared_search_fields, $query_vars ); $formatted_args['query'] = $search_algorithm->get_query( 'user', $query_vars['search'], $prepared_search_fields, $query_vars ); } else { $formatted_args['query']['match_all'] = [ diff --git a/includes/classes/SearchAlgorithm.php b/includes/classes/SearchAlgorithm.php index 8645e421d0..faa84dc59c 100644 --- a/includes/classes/SearchAlgorithm.php +++ b/includes/classes/SearchAlgorithm.php @@ -18,6 +18,27 @@ * SearchAlgorithm abstract class */ abstract class SearchAlgorithm { + /** + * Return the Search Algorithm slug. + * + * @return string + */ + abstract public function get_slug() : string; + + /** + * Return the Search Algorithm human readable name. + * + * @return string + */ + abstract public function get_name() : string; + + /** + * Return the Search Algorithm description. + * + * @return string + */ + abstract public function get_description() : string; + /** * Return the Elasticsearch `query` clause. * @@ -77,12 +98,11 @@ public function get_query( string $indexable_slug, string $search_term, array $s * * @since 3.5.5 $search_text and $search_fields parameters added. */ - $query = apply_filters( + $query = apply_filters_deprecated( 'ep_formatted_args_query', - $query, - $query_vars, - $search_term, - $search_fields + [ $query, $query_vars, $search_term, $search_fields ], + '4.3.0', + 'ep_post_formatted_args_query' ); } diff --git a/includes/classes/SearchAlgorithm/Basic.php b/includes/classes/SearchAlgorithm/Basic.php index a3d022164b..7602a8a499 100644 --- a/includes/classes/SearchAlgorithm/Basic.php +++ b/includes/classes/SearchAlgorithm/Basic.php @@ -18,6 +18,32 @@ * Basic search algorithm class. */ class Basic extends \ElasticPress\SearchAlgorithm { + /** + * Search algorithm slug. + * + * @return string + */ + public function get_slug() : string { + return 'basic'; + } + + /** + * Search algorithm name. + * + * @return string + */ + public function get_name() : string { + return esc_html__( 'Basic', 'elasticpress' ); + } + + /** + * Search algorithm description. + * + * @return string + */ + public function get_description() : string { + return esc_html__( 'Basic', 'elasticpress' ); + } /** * Return the Elasticsearch `query` clause. @@ -123,6 +149,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', [ 4, $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_phrase_boost' ); @@ -140,6 +167,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_boost', [ 2, $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_boost' ); @@ -157,6 +185,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][2]['multi_match']['fuzziness'] = apply_filters_deprecated( 'ep_fuzziness_arg', [ 1, $search_fields, $query_vars ], + '4.3.0', 'ep_post_fuzziness_arg' ); diff --git a/includes/classes/SearchAlgorithm/Version_350.php b/includes/classes/SearchAlgorithm/Version_350.php index 67a70b50e5..5c52b646cc 100644 --- a/includes/classes/SearchAlgorithm/Version_350.php +++ b/includes/classes/SearchAlgorithm/Version_350.php @@ -18,6 +18,32 @@ * EP version 3.5.0 search algorithm class. */ class Version_350 extends \ElasticPress\SearchAlgorithm { + /** + * Search algorithm slug. + * + * @return string + */ + public function get_slug() : string { + return '3.5'; + } + + /** + * Search algorithm name. + * + * @return string + */ + public function get_name() : string { + return esc_html__( 'Version 3.5', 'elasticpress' ); + } + + /** + * Search algorithm description. + * + * @return string + */ + public function get_description() : string { + return esc_html__( 'Version 3.5', 'elasticpress' ); + } /** * Return the Elasticsearch `query` clause. @@ -76,6 +102,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', [ 3, $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_phrase_boost' ); diff --git a/includes/classes/SearchAlgorithm/Version_400.php b/includes/classes/SearchAlgorithm/Version_400.php index d35f9cb034..083dceb828 100644 --- a/includes/classes/SearchAlgorithm/Version_400.php +++ b/includes/classes/SearchAlgorithm/Version_400.php @@ -18,6 +18,32 @@ * EP version 4.0.0 search algorithm class. */ class Version_400 extends \ElasticPress\SearchAlgorithm { + /** + * Search algorithm slug. + * + * @return string + */ + public function get_slug() : string { + return '4.0'; + } + + /** + * Search algorithm name. + * + * @return string + */ + public function get_name() : string { + return esc_html__( 'Version 4.0', 'elasticpress' ); + } + + /** + * Search algorithm description. + * + * @return string + */ + public function get_description() : string { + return esc_html__( 'Version 4.0', 'elasticpress' ); + } /** * Return the Elasticsearch `query` clause. @@ -109,6 +135,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][0]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_phrase_boost', [ 3, $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_phrase_boost' ); @@ -116,6 +143,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][1]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_boost', [ 1, $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_boost' ); @@ -134,6 +162,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][1]['multi_match']['fuzziness'] = apply_filters_deprecated( 'ep_match_fuzziness', [ 'auto', $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_fuzziness' ); @@ -152,6 +181,7 @@ protected function apply_legacy_filters( array $query, string $indexable_slug, a $query['bool']['should'][2]['multi_match']['boost'] = apply_filters_deprecated( 'ep_match_cross_fields_boost', [ 1, $search_fields, $query_vars ], + '4.3.0', 'ep_post_match_cross_fields_boost' ); diff --git a/includes/classes/SearchAlgorithms.php b/includes/classes/SearchAlgorithms.php new file mode 100644 index 0000000000..8515e76031 --- /dev/null +++ b/includes/classes/SearchAlgorithms.php @@ -0,0 +1,76 @@ +registered_search_algorithms[ $search_algorithm->get_slug() ] = $search_algorithm; + } + + /** + * Get a search algorithm instance given a slug + * + * @param string $slug Search Algorithm slug + * @return SearchAlgorithm + */ + public function get( string $slug ) { + return ( ! empty( $this->registered_search_algorithms[ $slug ] ) ) ? + $this->registered_search_algorithms[ $slug ] : + $this->registered_search_algorithms['basic']; + } + + /** + * Get all search algorithm instances + * + * @param boolean $slug_only True returns an array of only string slugs. + * @return array + */ + public function get_all( $slug_only = false ) { + if ( $slug_only ) { + return array_keys( $this->registered_search_algorithms ); + } + + return $this->registered_search_algorithms; + } + + /** + * Return singleton instance of class + * + * @return object + */ + public static function factory() { + static $instance = false; + + if ( ! $instance ) { + $instance = new self(); + } + + return $instance; + } +} From e5ca47b188aa10016bedf52d6f827a0d7b3ff991 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 15:06:56 -0300 Subject: [PATCH 08/15] Tests for SearchAlgorithms --- includes/classes/SearchAlgorithms.php | 16 +++++ tests/php/TestSearchAlgorithms.php | 85 +++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tests/php/TestSearchAlgorithms.php diff --git a/includes/classes/SearchAlgorithms.php b/includes/classes/SearchAlgorithms.php index 8515e76031..2f84197f6d 100644 --- a/includes/classes/SearchAlgorithms.php +++ b/includes/classes/SearchAlgorithms.php @@ -45,6 +45,22 @@ public function get( string $slug ) { $this->registered_search_algorithms['basic']; } + /** + * Unregister a search algorithm. + * + * A search algorithm can only be unregistered if it is not the only one left. + * + * @param string $slug Search Algorithm slug + * @return bool Whether the search algorithm was unregistered or not. + */ + public function unregister( string $slug ) { + if ( isset( $this->registered_search_algorithms[ $slug ] ) && count( $this->registered_search_algorithms ) >= 2 ) { + unset( $this->registered_search_algorithms[ $slug ] ); + return true; + } + return false; + } + /** * Get all search algorithm instances * diff --git a/tests/php/TestSearchAlgorithms.php b/tests/php/TestSearchAlgorithms.php new file mode 100644 index 0000000000..6880208f69 --- /dev/null +++ b/tests/php/TestSearchAlgorithms.php @@ -0,0 +1,85 @@ +getMockForAbstractClass( SearchAlgorithm::class ); + $stub->expects( $this->any() ) + ->method( 'get_slug' ) + ->will( $this->returnValue( 'stub' ) ); + + SearchAlgorithms::factory()->register( $stub ); + + $this->assertSame( $stub, SearchAlgorithms::factory()->get( 'stub' ) ); + + /** + * Test getting a non-existent search algorithm. `Basic` should be used. + */ + $this->assertSame( 'basic', SearchAlgorithms::factory()->get( 'foobar' )->get_slug() ); + } + + /** + * Test unregistering search algorithms + * + * @depends testRegisterAndGetSearchAlgorithms + * @group searchAlgorithms + */ + public function testUnregisterSearchAlgorithms() { + $this->assertTrue( SearchAlgorithms::factory()->unregister( 'stub' ) ); + $this->assertFalse( SearchAlgorithms::factory()->unregister( 'foobar' ) ); + + /** + * Store these search algorithms to register them back later. + */ + $basic = SearchAlgorithms::factory()->get( 'basic' ); + $version_35 = SearchAlgorithms::factory()->get( '3.5' ); + + $this->assertTrue( SearchAlgorithms::factory()->unregister( 'basic' ) ); + $this->assertTrue( SearchAlgorithms::factory()->unregister( '3.5' ) ); + + /** + * This is the last one remaining and will not be unregistered + */ + $this->assertFalse( SearchAlgorithms::factory()->unregister( '4.0' ) ); + + SearchAlgorithms::factory()->register( $basic ); + SearchAlgorithms::factory()->register( $version_35 ); + } + + /** + * Test getting all search algorithms + * + * @depends testUnregisterSearchAlgorithms + * @group searchAlgorithms + */ + public function testGetAll() { + $this->assertEqualsCanonicalizing( [ 'basic', '3.5', '4.0' ], SearchAlgorithms::factory()->get_all( true ) ); + + $search_algorithms = SearchAlgorithms::factory()->get_all(); + $this->assertCount( 3, $search_algorithms ); + foreach ( $search_algorithms as $search_algorithm ) { + $this->assertInstanceOf( SearchAlgorithm::class, $search_algorithm ); + } + } +} From ef7b3b9afb2b1e3ce25dc6d27cfd4bbaaf904a6b Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Jul 2022 16:10:31 -0300 Subject: [PATCH 09/15] Tests: Basic Search Algorithm --- .../TestBasicSearchAlgorithm.php | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php diff --git a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php new file mode 100644 index 0000000000..a486de4239 --- /dev/null +++ b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php @@ -0,0 +1,175 @@ +assertSame( 'basic', $basic->get_slug() ); + } + + /** + * Test default query + * + * @group searchAlgorithms + */ + public function testGetQuery() { + $basic = new Basic(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + + $model = $this->getModel( $search_term, $search_fields); + + $this->assertEquals( $model, $query ); + } + + /** + * Test filters + * + * @group searchAlgorithms + */ + public function testFilters() { + $basic = new Basic(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $test_filter = function() { + return 1234; + }; + + /** + * Test the `ep_{$indexable_slug}_match_phrase_boost` filter. + */ + add_filter( 'ep_indexable_match_phrase_boost', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); + + remove_filter( 'ep_indexable_match_phrase_boost', $test_filter ); + + /** + * Test the `ep_{$indexable_slug}_match_boost` filter. + */ + add_filter( 'ep_indexable_match_boost', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] ); + + remove_filter( 'ep_indexable_match_boost', $test_filter ); + + /** + * Test the `ep_{$indexable_slug}_fuzziness_arg` filter. + */ + add_filter( 'ep_indexable_fuzziness_arg', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['fuzziness'] ); + + remove_filter( 'ep_indexable_fuzziness_arg', $test_filter ); + + } + + /** + * Test deprecated/legacy filters + * + * @expectedDeprecated ep_match_phrase_boost + * @expectedDeprecated ep_match_boost + * @expectedDeprecated ep_fuzziness_arg + * @group searchAlgorithms + */ + public function testLegacyFilters() { + $basic = new Basic(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $test_filter = function() { + return 1234; + }; + + /** + * Test the `ep_match_phrase_boost` filter. + */ + add_filter( 'ep_match_phrase_boost', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); + + remove_filter( 'ep_match_phrase_boost', $test_filter ); + + /** + * Test the `ep_match_boost` filter. + */ + add_filter( 'ep_match_boost', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] ); + + remove_filter( 'ep_match_boost', $test_filter ); + + /** + * Test the `ep_fuzziness_arg` filter. + */ + add_filter( 'ep_fuzziness_arg', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['fuzziness'] ); + + remove_filter( 'ep_fuzziness_arg', $test_filter ); + } + + protected function getModel( $search_term, $search_fields ) { + return [ + 'bool' => [ + 'should' => [ + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'phrase', + 'fields' => $search_fields, + 'boost' => 4, + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'fields' => $search_fields, + 'boost' => 2, + 'fuzziness' => 0, + 'operator' => 'and', + ], + ], + [ + 'multi_match' => [ + 'fields' => $search_fields, + 'query' => $search_term, + 'fuzziness' => 1, + ], + ], + ], + ], + ]; + } + + +} \ No newline at end of file From ceca48861295b6943afde65c5a5abe3cfbf71bd5 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 26 Jul 2022 14:16:21 -0300 Subject: [PATCH 10/15] Tests for 3.5 and 4.0 --- .../TestBasicSearchAlgorithm.php | 3 - .../TestVersion_350SearchAlgorithm.php | 122 +++++++++++ .../TestVersion_400SearchAlgorithm.php | 197 ++++++++++++++++++ 3 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php create mode 100644 tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php diff --git a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php index a486de4239..214df15bc2 100644 --- a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php @@ -86,7 +86,6 @@ public function testFilters() { $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['fuzziness'] ); remove_filter( 'ep_indexable_fuzziness_arg', $test_filter ); - } /** @@ -170,6 +169,4 @@ protected function getModel( $search_term, $search_fields ) { ], ]; } - - } \ No newline at end of file diff --git a/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php b/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php new file mode 100644 index 0000000000..03e2d390b9 --- /dev/null +++ b/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php @@ -0,0 +1,122 @@ +assertSame( '3.5', $basic->get_slug() ); + } + + /** + * Test default query + * + * @group searchAlgorithms + */ + public function testGetQuery() { + $basic = new Version_350(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + + $model = $this->getModel( $search_term, $search_fields); + + $this->assertEquals( $model, $query ); + } + + /** + * Test filters + * + * @group searchAlgorithms + */ + public function testFilters() { + $basic = new Version_350(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $test_filter = function() { + return 1234; + }; + + /** + * Test the `ep_{$indexable_slug}_match_phrase_boost` filter. + */ + add_filter( 'ep_indexable_match_phrase_boost', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); + + remove_filter( 'ep_indexable_match_phrase_boost', $test_filter ); + } + + /** + * Test deprecated/legacy filters + * + * @expectedDeprecated ep_match_phrase_boost + * @group searchAlgorithms + */ + public function testLegacyFilters() { + $basic = new Version_350(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $test_filter = function() { + return 1234; + }; + + /** + * Test the `ep_match_phrase_boost` filter. + */ + add_filter( 'ep_match_phrase_boost', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); + + remove_filter( 'ep_match_phrase_boost', $test_filter ); + } + + protected function getModel( $search_term, $search_fields ) { + return [ + 'bool' => [ + 'should' => [ + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'phrase', + 'fields' => $search_fields, + 'boost' => 3, + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'fields' => $search_fields, + 'type' => 'phrase', + 'slop' => 5, + ], + ], + ], + ], + ]; + } +} \ No newline at end of file diff --git a/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php b/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php new file mode 100644 index 0000000000..adffb43f2b --- /dev/null +++ b/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php @@ -0,0 +1,197 @@ +assertSame( '4.0', $basic->get_slug() ); + } + + /** + * Test default query + * + * @group searchAlgorithms + */ + public function testGetQuery() { + $basic = new Version_400(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + + $model = $this->getModel( $search_term, $search_fields); + + $this->assertEquals( $model, $query ); + } + + /** + * Test filters + * + * @group searchAlgorithms + */ + public function testFilters() { + $basic = new Version_400(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $test_filter = function() { + return 1234; + }; + + /** + * Test the `ep_{$indexable_slug}_match_phrase_boost` filter. + */ + add_filter( 'ep_indexable_match_phrase_boost', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); + + remove_filter( 'ep_indexable_match_phrase_boost', $test_filter ); + + /** + * Test the `ep_{$indexable_slug}_match_boost` filter. + */ + add_filter( 'ep_indexable_match_boost', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] ); + + remove_filter( 'ep_indexable_match_boost', $test_filter ); + + /** + * Test the `ep_{$indexable_slug}_match_fuzziness` filter. + */ + add_filter( 'ep_indexable_match_fuzziness', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['fuzziness'] ); + + remove_filter( 'ep_indexable_match_fuzziness', $test_filter ); + + /** + * Test the `ep_{$indexable_slug}_match_cross_fields_boost` filter. + */ + add_filter( 'ep_indexable_match_cross_fields_boost', $test_filter ); + + $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['boost'] ); + + remove_filter( 'ep_indexable_match_cross_fields_boost', $test_filter ); + } + + /** + * Test deprecated/legacy filters + * + * @expectedDeprecated ep_match_phrase_boost + * @expectedDeprecated ep_match_boost + * @expectedDeprecated ep_match_fuzziness + * @expectedDeprecated ep_match_cross_fields_boost + * @group searchAlgorithms + */ + public function testLegacyFilters() { + $basic = new Version_400(); + + $search_term = 'search_term'; + $search_fields = [ 'post_title', 'post_content' ]; + + $test_filter = function() { + return 1234; + }; + + /** + * Test the `ep_match_phrase_boost` filter. + */ + add_filter( 'ep_match_phrase_boost', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); + + remove_filter( 'ep_match_phrase_boost', $test_filter ); + + /** + * Test the `ep_match_boost` filter. + */ + add_filter( 'ep_match_boost', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] ); + + remove_filter( 'ep_match_boost', $test_filter ); + + /** + * Test the `ep_match_fuzziness` filter. + */ + add_filter( 'ep_match_fuzziness', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['fuzziness'] ); + + remove_filter( 'ep_match_fuzziness', $test_filter ); + + /** + * Test the `ep_match_cross_fields_boost` filter. + */ + add_filter( 'ep_match_cross_fields_boost', $test_filter ); + + $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['boost'] ); + + remove_filter( 'ep_match_cross_fields_boost', $test_filter ); + } + + protected function getModel( $search_term, $search_fields ) { + return [ + 'bool' => [ + 'should' => [ + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'phrase', + 'fields' => $search_fields, + 'boost' => 3, + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'fields' => $search_fields, + 'operator' => 'and', + 'boost' => 1, + 'fuzziness' => 'auto', + ], + ], + [ + 'multi_match' => [ + 'query' => $search_term, + 'type' => 'cross_fields', + 'fields' => $search_fields, + 'boost' => 1, + 'analyzer' => 'standard', + 'tie_breaker' => 0.5, + 'operator' => 'and', + ], + ], + ], + ], + ]; + } +} \ No newline at end of file From 6cbb54ea7e40262cc409a7141ea521c170290420 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 26 Jul 2022 14:42:46 -0300 Subject: [PATCH 11/15] Tests for SearchAlgorithm + docs --- tests/php/TestSearchAlgorithm.php | 78 +++++++++++++++++++ .../TestBasicSearchAlgorithm.php | 12 ++- .../TestVersion_350SearchAlgorithm.php | 12 ++- .../TestVersion_400SearchAlgorithm.php | 12 ++- 4 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 tests/php/TestSearchAlgorithm.php diff --git a/tests/php/TestSearchAlgorithm.php b/tests/php/TestSearchAlgorithm.php new file mode 100644 index 0000000000..224ad46436 --- /dev/null +++ b/tests/php/TestSearchAlgorithm.php @@ -0,0 +1,78 @@ +stub = $this->getMockForAbstractClass( SearchAlgorithm::class ); + $this->stub->expects( $this->any() ) + ->method( 'get_raw_query' ) + ->will( $this->returnValue( [] ) ); + + parent::setUp(); + } + + /** + * Test filters + * + * @group searchAlgorithms + */ + public function testFilters() { + $test_filter = function() { + return [ 'changed' ]; + }; + + /** + * Test the `ep_{$indexable_slug}_formatted_args_query` filter. + */ + add_filter( 'ep_indexable_formatted_args_query', $test_filter ); + + $query = $this->stub->get_query( 'indexable', '', [], [] ); + $this->assertEquals( [ 'changed' ], $query ); + + remove_filter( 'ep_indexable_formatted_args_query', $test_filter ); + } + + /** + * Test deprecated/legacy filters + * + * @expectedDeprecated ep_formatted_args_query + * @group searchAlgorithms + */ + public function testLegacyFilters() { + $test_filter = function() { + return [ 'changed' ]; + }; + + /** + * Test the `ep_formatted_args_query` filter. + */ + add_filter( 'ep_formatted_args_query', $test_filter ); + + $query = $this->stub->get_query( 'post', '', [], [] ); + $this->assertEquals( [ 'changed' ], $query ); + + remove_filter( 'ep_formatted_args_query', $test_filter ); + } +} diff --git a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php index 214df15bc2..9a2cb38cc1 100644 --- a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php @@ -2,6 +2,7 @@ /** * Test basic search algorithm * + * @since 4.3.0 * @package elasticpress */ @@ -137,7 +138,14 @@ public function testLegacyFilters() { remove_filter( 'ep_fuzziness_arg', $test_filter ); } - protected function getModel( $search_term, $search_fields ) { + /** + * ES Query model + * + * @param string $search_term Search term + * @param string $search_fields Search fields + * @return array + */ + protected function getModel( string $search_term, string $search_fields ) : array { return [ 'bool' => [ 'should' => [ @@ -169,4 +177,4 @@ protected function getModel( $search_term, $search_fields ) { ], ]; } -} \ No newline at end of file +} diff --git a/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php b/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php index 03e2d390b9..ef844f8fbb 100644 --- a/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php @@ -2,6 +2,7 @@ /** * Test EP v3.5 search algorithm * + * @since 4.3.0 * @package elasticpress */ @@ -95,7 +96,14 @@ public function testLegacyFilters() { remove_filter( 'ep_match_phrase_boost', $test_filter ); } - protected function getModel( $search_term, $search_fields ) { + /** + * ES Query model + * + * @param string $search_term Search term + * @param string $search_fields Search fields + * @return array + */ + protected function getModel( string $search_term, string $search_fields ) : array { return [ 'bool' => [ 'should' => [ @@ -119,4 +127,4 @@ protected function getModel( $search_term, $search_fields ) { ], ]; } -} \ No newline at end of file +} diff --git a/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php b/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php index adffb43f2b..0cbfcce748 100644 --- a/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php @@ -2,6 +2,7 @@ /** * Test EP v4.0 search algorithm * + * @since 4.3.0 * @package elasticpress */ @@ -158,7 +159,14 @@ public function testLegacyFilters() { remove_filter( 'ep_match_cross_fields_boost', $test_filter ); } - protected function getModel( $search_term, $search_fields ) { + /** + * ES Query model + * + * @param string $search_term Search term + * @param string $search_fields Search fields + * @return array + */ + protected function getModel( string $search_term, string $search_fields ) : array { return [ 'bool' => [ 'should' => [ @@ -194,4 +202,4 @@ protected function getModel( $search_term, $search_fields ) { ], ]; } -} \ No newline at end of file +} From c2a6533435b1bfb14c971d91186e40d5f4bdb9e0 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 26 Jul 2022 14:53:18 -0300 Subject: [PATCH 12/15] Fix type --- tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php | 4 ++-- tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php | 4 ++-- tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php index 9a2cb38cc1..dc73e75a1c 100644 --- a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php @@ -142,10 +142,10 @@ public function testLegacyFilters() { * ES Query model * * @param string $search_term Search term - * @param string $search_fields Search fields + * @param array $search_fields Search fields * @return array */ - protected function getModel( string $search_term, string $search_fields ) : array { + protected function getModel( string $search_term, array $search_fields ) : array { return [ 'bool' => [ 'should' => [ diff --git a/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php b/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php index ef844f8fbb..d1362cafd2 100644 --- a/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestVersion_350SearchAlgorithm.php @@ -100,10 +100,10 @@ public function testLegacyFilters() { * ES Query model * * @param string $search_term Search term - * @param string $search_fields Search fields + * @param array $search_fields Search fields * @return array */ - protected function getModel( string $search_term, string $search_fields ) : array { + protected function getModel( string $search_term, array $search_fields ) : array { return [ 'bool' => [ 'should' => [ diff --git a/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php b/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php index 0cbfcce748..61261c96de 100644 --- a/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestVersion_400SearchAlgorithm.php @@ -163,10 +163,10 @@ public function testLegacyFilters() { * ES Query model * * @param string $search_term Search term - * @param string $search_fields Search fields + * @param array $search_fields Search fields * @return array */ - protected function getModel( string $search_term, string $search_fields ) : array { + protected function getModel( string $search_term, array $search_fields ) : array { return [ 'bool' => [ 'should' => [ From 6876dc62952fbbe09aa771bdcda2e24686ac5204 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 26 Jul 2022 14:59:01 -0300 Subject: [PATCH 13/15] Fix method name --- includes/classes/Indexable/Post/Post.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index b9c3940b92..89d0bcb841 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -2063,7 +2063,7 @@ protected function apply_aggregations( $formatted_args, $agg, $use_filters, $fil } /** - * Get the search algorithm class that should be used. + * Get the search algorithm that should be used. * * @since 4.3.0 * @param string $search_text Search term(s) @@ -2071,7 +2071,7 @@ protected function apply_aggregations( $formatted_args, $agg, $use_filters, $fil * @param array $query_vars Query vars * @return SearchAlgorithm Instance of search algorithm to be used */ - public function get_search_algorithm_class( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm { + public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm { $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' ); /** From ca8e7ecf494b69dbcef72f0cb8bb94a32b500dbd Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 26 Jul 2022 15:11:17 -0300 Subject: [PATCH 14/15] Tests for filters ep_search_algorithm_version and ep_post_search_algorithm --- tests/php/indexables/TestPost.php | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index ac79723102..69d98d4618 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -6865,6 +6865,63 @@ public function testMetaWithoutValue() { $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( $expected_result, $query->post_count ); + } + + /** + * Test the get_search_algorithm implementation + */ + public function testGetSearchAlgorithm() { + /** + * Test default search algorithm + */ + $version_40 = \ElasticPress\SearchAlgorithms::factory()->get( '4.0' ); + + $post_indexable = \ElasticPress\Indexables::factory()->get( 'post' ); + $search_algorithm = $post_indexable->get_search_algorithm( '', [], [] ); + + $this->assertSame( $version_40, $search_algorithm ); + + /** + * Test setting a diffent algorithm through the `ep_search_algorithm_version` filter + */ + $version_35 = \ElasticPress\SearchAlgorithms::factory()->get( '3.5' ); + + $set_version_35 = function() { + return '3.5'; + }; + + add_filter( 'ep_search_algorithm_version', $set_version_35 ); + + $search_algorithm = $post_indexable->get_search_algorithm( '', [], [] ); + $this->assertSame( $version_35, $search_algorithm ); + + remove_filter( 'ep_search_algorithm_version', $set_version_35 ); + + /** + * Test setting a non-existent algorithm through the `ep_search_algorithm_version` filter + * It should use `basic` + */ + $basic = \ElasticPress\SearchAlgorithms::factory()->get( 'basic' ); + + $set_non_existent_version = function() { + return 'foobar'; + }; + + add_filter( 'ep_search_algorithm_version', $set_non_existent_version ); + + $search_algorithm = $post_indexable->get_search_algorithm( '', [], [] ); + $this->assertSame( $basic, $search_algorithm ); + + remove_filter( 'ep_search_algorithm_version', $set_non_existent_version ); + + /** + * Test the `ep_{$indexable_slug}_search_algorithm` filter + */ + add_filter( 'ep_post_search_algorithm', $set_version_35 ); + + $search_algorithm = $post_indexable->get_search_algorithm( '', [], [] ); + $this->assertSame( $version_35, $search_algorithm ); + remove_filter( 'ep_post_search_algorithm', $set_version_35 ); } } From e847bfbe539a78249a1e22d2791b0c65655e7bff Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 26 Jul 2022 16:25:26 -0300 Subject: [PATCH 15/15] Rename from `basic` to `default` --- elasticpress.php | 2 +- .../Feature/Autosuggest/Autosuggest.php | 2 +- .../{Basic.php => DefaultAlgorithm.php} | 12 +++---- includes/classes/SearchAlgorithms.php | 2 +- tests/php/TestSearchAlgorithms.php | 10 +++--- ...thm.php => TestDefaultSearchAlgorithm.php} | 32 +++++++++---------- 6 files changed, 30 insertions(+), 30 deletions(-) rename includes/classes/SearchAlgorithm/{Basic.php => DefaultAlgorithm.php} (95%) rename tests/php/searchAlgorithms/{TestBasicSearchAlgorithm.php => TestDefaultSearchAlgorithm.php} (78%) diff --git a/elasticpress.php b/elasticpress.php index e005b91a93..07c5d11563 100644 --- a/elasticpress.php +++ b/elasticpress.php @@ -161,7 +161,7 @@ function register_indexable_posts() { /** * Register search algorithms */ - SearchAlgorithms::factory()->register( new SearchAlgorithm\Basic() ); + SearchAlgorithms::factory()->register( new SearchAlgorithm\DefaultAlgorithm() ); SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_350() ); SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_400() ); } diff --git a/includes/classes/Feature/Autosuggest/Autosuggest.php b/includes/classes/Feature/Autosuggest/Autosuggest.php index 3c0bd9cc90..356b647202 100644 --- a/includes/classes/Feature/Autosuggest/Autosuggest.php +++ b/includes/classes/Feature/Autosuggest/Autosuggest.php @@ -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' ] ); diff --git a/includes/classes/SearchAlgorithm/Basic.php b/includes/classes/SearchAlgorithm/DefaultAlgorithm.php similarity index 95% rename from includes/classes/SearchAlgorithm/Basic.php rename to includes/classes/SearchAlgorithm/DefaultAlgorithm.php index 7602a8a499..b6e1580de7 100644 --- a/includes/classes/SearchAlgorithm/Basic.php +++ b/includes/classes/SearchAlgorithm/DefaultAlgorithm.php @@ -1,6 +1,6 @@ registered_search_algorithms[ $slug ] ) ) ? $this->registered_search_algorithms[ $slug ] : - $this->registered_search_algorithms['basic']; + $this->registered_search_algorithms['default']; } /** diff --git a/tests/php/TestSearchAlgorithms.php b/tests/php/TestSearchAlgorithms.php index 6880208f69..96782c5955 100644 --- a/tests/php/TestSearchAlgorithms.php +++ b/tests/php/TestSearchAlgorithms.php @@ -36,7 +36,7 @@ public function testRegisterAndGetSearchAlgorithms() { /** * Test getting a non-existent search algorithm. `Basic` should be used. */ - $this->assertSame( 'basic', SearchAlgorithms::factory()->get( 'foobar' )->get_slug() ); + $this->assertSame( 'default', SearchAlgorithms::factory()->get( 'foobar' )->get_slug() ); } /** @@ -52,10 +52,10 @@ public function testUnregisterSearchAlgorithms() { /** * Store these search algorithms to register them back later. */ - $basic = SearchAlgorithms::factory()->get( 'basic' ); + $default = SearchAlgorithms::factory()->get( 'default' ); $version_35 = SearchAlgorithms::factory()->get( '3.5' ); - $this->assertTrue( SearchAlgorithms::factory()->unregister( 'basic' ) ); + $this->assertTrue( SearchAlgorithms::factory()->unregister( 'default' ) ); $this->assertTrue( SearchAlgorithms::factory()->unregister( '3.5' ) ); /** @@ -63,7 +63,7 @@ public function testUnregisterSearchAlgorithms() { */ $this->assertFalse( SearchAlgorithms::factory()->unregister( '4.0' ) ); - SearchAlgorithms::factory()->register( $basic ); + SearchAlgorithms::factory()->register( $default ); SearchAlgorithms::factory()->register( $version_35 ); } @@ -74,7 +74,7 @@ public function testUnregisterSearchAlgorithms() { * @group searchAlgorithms */ public function testGetAll() { - $this->assertEqualsCanonicalizing( [ 'basic', '3.5', '4.0' ], SearchAlgorithms::factory()->get_all( true ) ); + $this->assertEqualsCanonicalizing( [ 'default', '3.5', '4.0' ], SearchAlgorithms::factory()->get_all( true ) ); $search_algorithms = SearchAlgorithms::factory()->get_all(); $this->assertCount( 3, $search_algorithms ); diff --git a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php b/tests/php/searchAlgorithms/TestDefaultSearchAlgorithm.php similarity index 78% rename from tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php rename to tests/php/searchAlgorithms/TestDefaultSearchAlgorithm.php index dc73e75a1c..f2b2df1c16 100644 --- a/tests/php/searchAlgorithms/TestBasicSearchAlgorithm.php +++ b/tests/php/searchAlgorithms/TestDefaultSearchAlgorithm.php @@ -1,6 +1,6 @@ assertSame( 'basic', $basic->get_slug() ); + $this->assertSame( 'default', $default->get_slug() ); } /** @@ -31,12 +31,12 @@ public function testGetSlug() { * @group searchAlgorithms */ public function testGetQuery() { - $basic = new Basic(); + $default = new DefaultAlgorithm(); $search_term = 'search_term'; $search_fields = [ 'post_title', 'post_content' ]; - $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $query = $default->get_query( 'indexable', $search_term, $search_fields, [] ); $model = $this->getModel( $search_term, $search_fields); @@ -49,7 +49,7 @@ public function testGetQuery() { * @group searchAlgorithms */ public function testFilters() { - $basic = new Basic(); + $default = new DefaultAlgorithm(); $search_term = 'search_term'; $search_fields = [ 'post_title', 'post_content' ]; @@ -63,7 +63,7 @@ public function testFilters() { */ add_filter( 'ep_indexable_match_phrase_boost', $test_filter ); - $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $query = $default->get_query( 'indexable', $search_term, $search_fields, [] ); $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); remove_filter( 'ep_indexable_match_phrase_boost', $test_filter ); @@ -73,7 +73,7 @@ public function testFilters() { */ add_filter( 'ep_indexable_match_boost', $test_filter ); - $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $query = $default->get_query( 'indexable', $search_term, $search_fields, [] ); $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] ); remove_filter( 'ep_indexable_match_boost', $test_filter ); @@ -83,7 +83,7 @@ public function testFilters() { */ add_filter( 'ep_indexable_fuzziness_arg', $test_filter ); - $query = $basic->get_query( 'indexable', $search_term, $search_fields, [] ); + $query = $default->get_query( 'indexable', $search_term, $search_fields, [] ); $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['fuzziness'] ); remove_filter( 'ep_indexable_fuzziness_arg', $test_filter ); @@ -98,7 +98,7 @@ public function testFilters() { * @group searchAlgorithms */ public function testLegacyFilters() { - $basic = new Basic(); + $default = new DefaultAlgorithm(); $search_term = 'search_term'; $search_fields = [ 'post_title', 'post_content' ]; @@ -112,7 +112,7 @@ public function testLegacyFilters() { */ add_filter( 'ep_match_phrase_boost', $test_filter ); - $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $query = $default->get_query( 'post', $search_term, $search_fields, [] ); $this->assertEquals( 1234, $query['bool']['should'][0]['multi_match']['boost'] ); remove_filter( 'ep_match_phrase_boost', $test_filter ); @@ -122,7 +122,7 @@ public function testLegacyFilters() { */ add_filter( 'ep_match_boost', $test_filter ); - $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $query = $default->get_query( 'post', $search_term, $search_fields, [] ); $this->assertEquals( 1234, $query['bool']['should'][1]['multi_match']['boost'] ); remove_filter( 'ep_match_boost', $test_filter ); @@ -132,7 +132,7 @@ public function testLegacyFilters() { */ add_filter( 'ep_fuzziness_arg', $test_filter ); - $query = $basic->get_query( 'post', $search_term, $search_fields, [] ); + $query = $default->get_query( 'post', $search_term, $search_fields, [] ); $this->assertEquals( 1234, $query['bool']['should'][2]['multi_match']['fuzziness'] ); remove_filter( 'ep_fuzziness_arg', $test_filter );