From fc02cba2d46c8d4dbbb0f5bdc11bcbc181a48a78 Mon Sep 17 00:00:00 2001 From: Mohammed Razzaq Date: Tue, 26 Jul 2022 16:11:29 +0530 Subject: [PATCH 1/2] Add filter to change field used to decay scores --- includes/classes/Feature/Search/Search.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index 47ebbe9a26..4d3364e834 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -459,13 +459,24 @@ public function weight_recent( $formatted_args, $args ) { * @return {string} New decay function */ $decay_function = apply_filters( 'epwr_decay_function', 'exp', $formatted_args, $args ); - $date_score = array( + /** + * Filter search date weighting field + * + * @hook epwr_decay_field + * @param {string} $field Current decay field + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {string} New decay field + * @since 4.3.0 + */ + $field = apply_filters( 'epwr_decay_field', 'post_date_gmt', $formatted_args, $args ); + $date_score = array( 'function_score' => array( 'query' => $formatted_args['query'], 'functions' => array( array( $decay_function => array( - 'post_date_gmt' => array( + $field => array( /** * Filter search date weighting scale * From e633b972f35049eeb37344e2090b85085ceed1a7 Mon Sep 17 00:00:00 2001 From: Mohammed Razzaq Date: Wed, 27 Jul 2022 00:52:30 +0530 Subject: [PATCH 2/2] code improvement --- includes/classes/Feature/Search/Search.php | 180 +++++++++++---------- 1 file changed, 91 insertions(+), 89 deletions(-) diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index 4d3364e834..5979f17d06 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -447,109 +447,111 @@ public function is_decaying_enabled() { * @return array */ public function weight_recent( $formatted_args, $args ) { - if ( ! empty( $args['s'] ) ) { - if ( $this->is_decaying_enabled() ) { - /** - * Filter search date weighting scale - * - * @hook epwr_decay_function - * @param {string} $decay_function Current decay function - * @param {array} $formatted_args Formatted Elasticsearch arguments - * @param {array} $args WP_Query arguments - * @return {string} New decay function - */ - $decay_function = apply_filters( 'epwr_decay_function', 'exp', $formatted_args, $args ); - /** - * Filter search date weighting field - * - * @hook epwr_decay_field - * @param {string} $field Current decay field - * @param {array} $formatted_args Formatted Elasticsearch arguments - * @param {array} $args WP_Query arguments - * @return {string} New decay field - * @since 4.3.0 - */ - $field = apply_filters( 'epwr_decay_field', 'post_date_gmt', $formatted_args, $args ); - $date_score = array( - 'function_score' => array( - 'query' => $formatted_args['query'], - 'functions' => array( - array( - $decay_function => array( - $field => array( - /** - * Filter search date weighting scale - * - * @hook epwr_scale - * @param {string} $scale Current scale - * @param {array} $formatted_args Formatted Elasticsearch arguments - * @param {array} $args WP_Query arguments - * @return {string} New scale - */ - 'scale' => apply_filters( 'epwr_scale', '14d', $formatted_args, $args ), - /** - * Filter search date weighting decay - * - * @hook epwr_decay - * @param {float} $decay Current decay - * @param {array} $formatted_args Formatted Elasticsearch arguments - * @param {array} $args WP_Query arguments - * @return {float} New decay - */ - 'decay' => apply_filters( 'epwr_decay', 0.25, $formatted_args, $args ), - /** - * Filter search date weighting offset - * - * @hook epwr_offset - * @param {string} $offset Current offset - * @param {array} $formatted_args Formatted Elasticsearch arguments - * @param {array} $args WP_Query arguments - * @return {string} New offset - */ - 'offset' => apply_filters( 'epwr_offset', '7d', $formatted_args, $args ), - ), - ), - ), - array( + if ( empty( $args['s'] ) ) { + return $formatted_args; + } + if ( ! $this->is_decaying_enabled() ) { + return $formatted_args; + } + /** + * Filter search date weighting scale + * + * @hook epwr_decay_function + * @param {string} $decay_function Current decay function + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {string} New decay function + */ + $decay_function = apply_filters( 'epwr_decay_function', 'exp', $formatted_args, $args ); + /** + * Filter search date weighting field + * + * @hook epwr_decay_field + * @param {string} $field Current decay field + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {string} New decay field + * @since 4.3.0 + */ + $field = apply_filters( 'epwr_decay_field', 'post_date_gmt', $formatted_args, $args ); + $date_score = array( + 'function_score' => array( + 'query' => $formatted_args['query'], + 'functions' => array( + array( + $decay_function => array( + $field => array( /** - * Filter search date weight + * Filter search date weighting scale * - * @since 3.5.6 - * @hook epwr_weight - * @param {float} $weight Current weight + * @hook epwr_scale + * @param {string} $scale Current scale * @param {array} $formatted_args Formatted Elasticsearch arguments * @param {array} $args WP_Query arguments - * @return {float} New weight + * @return {string} New scale */ - 'weight' => apply_filters( 'epwr_weight', 0.001, $formatted_args, $args ), + 'scale' => apply_filters( 'epwr_scale', '14d', $formatted_args, $args ), + /** + * Filter search date weighting decay + * + * @hook epwr_decay + * @param {float} $decay Current decay + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {float} New decay + */ + 'decay' => apply_filters( 'epwr_decay', 0.25, $formatted_args, $args ), + /** + * Filter search date weighting offset + * + * @hook epwr_offset + * @param {string} $offset Current offset + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {string} New offset + */ + 'offset' => apply_filters( 'epwr_offset', '7d', $formatted_args, $args ), ), ), + ), + array( /** - * Filter search date weighting score mode - * - * @hook epwr_score_mode - * @param {string} $score_mode Current score mode - * @param {array} $formatted_args Formatted Elasticsearch arguments - * @param {array} $args WP_Query arguments - * @return {string} New score mode - */ - 'score_mode' => apply_filters( 'epwr_score_mode', 'sum', $formatted_args, $args ), - /** - * Filter search date weighting boost mode + * Filter search date weight * - * @hook epwr_boost_mode - * @param {string} $boost_mode Current boost mode + * @since 3.5.6 + * @hook epwr_weight + * @param {float} $weight Current weight * @param {array} $formatted_args Formatted Elasticsearch arguments * @param {array} $args WP_Query arguments - * @return {string} New boost mode + * @return {float} New weight */ - 'boost_mode' => apply_filters( 'epwr_boost_mode', 'multiply', $formatted_args, $args ), + 'weight' => apply_filters( 'epwr_weight', 0.001, $formatted_args, $args ), ), - ); + ), + /** + * Filter search date weighting score mode + * + * @hook epwr_score_mode + * @param {string} $score_mode Current score mode + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {string} New score mode + */ + 'score_mode' => apply_filters( 'epwr_score_mode', 'sum', $formatted_args, $args ), + /** + * Filter search date weighting boost mode + * + * @hook epwr_boost_mode + * @param {string} $boost_mode Current boost mode + * @param {array} $formatted_args Formatted Elasticsearch arguments + * @param {array} $args WP_Query arguments + * @return {string} New boost mode + */ + 'boost_mode' => apply_filters( 'epwr_boost_mode', 'multiply', $formatted_args, $args ), + ), + ); - $formatted_args['query'] = $date_score; - } - } + $formatted_args['query'] = $date_score; return $formatted_args; }