Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add filter to change field used to decay scores #2907

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 91 additions & 78 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,98 +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 );
$date_score = array(
'function_score' => array(
'query' => $formatted_args['query'],
'functions' => array(
array(
$decay_function => array(
'post_date_gmt' => 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 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 weight
* Filter search date weighting offset
*
* @since 3.5.6
* @hook epwr_weight
* @param {float} $weight Current weight
* @hook epwr_offset
* @param {string} $offset Current offset
* @param {array} $formatted_args Formatted Elasticsearch arguments
* @param {array} $args WP_Query arguments
* @return {float} New weight
* @return {string} New offset
*/
'weight' => apply_filters( 'epwr_weight', 0.001, $formatted_args, $args ),
'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;
}
Expand Down