Skip to content

Commit

Permalink
Search: improve page and documents score (#726)
Browse files Browse the repository at this point in the history
Update wp-elasticsearch.php
  • Loading branch information
EarthlingDavey authored Oct 9, 2024
1 parent 9eaa863 commit 2675b66
Showing 1 changed file with 56 additions and 24 deletions.
80 changes: 56 additions & 24 deletions public/app/themes/clarity/inc/admin/plugins/wp-elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,65 @@ public function removeFilterWeightRecent($formatted_args, $args)
return $formatted_args;
}

// The following code is a copy/paste from an ElasticPress query.
// "functions": [
// {
// "exp": {
// "post_date_gmt": {
// "scale": "14d",
// "decay": 0.25,
// "offset": "7d"
// }
// }
// },
// {
// "weight": 0.001
// }
// ],

// Remove it, so that we can use our own script_score.
unset($formatted_args['query']['function_score']['functions']);


/**
* Filter search date weighting scale
* Apply a script_score to the query.
*
* This function is copy/pasted from ElasticPress.
* Using it, ensures we are targeting the correct array key.
*
* @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
* For post types: blog, event & news - apply a *severe* decay script to the score.
* For other post types - apply a *mild* decay script to the score.
*
* @see https://opensearch.org/docs/latest/query-dsl/specialized/script-score/#decay-functions
* @see https://opensearch.org/docs/latest/query-dsl/compound/function-score/#decay-functions
*/
$decay_name = apply_filters('epwr_decay_function', 'exp', $formatted_args, $args);

// Map over the functions.
$formatted_args['query']['function_score']['functions'] = array_map(function ($f) use ($decay_name) {
// Identify the decay function.
if (isset($f[$decay_name])) {
// Add a filter so that the function will only apply to specific post types.
$f['filter'] = [
'terms' => [
"post_type.raw" => $this::RECENT_WEIGHT_POST_TYPES
]
];
}
return $f;
}, $formatted_args['query']['function_score']['functions']);

$formatted_args['query']['function_score']["script_score"] = [
"script" => [
'source' => "
if (
doc['post_type.raw'].value == 'blog'
|| doc['post_type.raw'].value == 'event'
|| doc['post_type.raw'].value == 'news'
) {
return _score * decayDateExp(params.severe.origin, params.severe.scale, params.severe.offset, params.severe.decay, doc.post_modified_gmt.value);
} else {
return _score * decayDateExp(params.mild.origin, params.mild.scale, params.mild.offset, params.mild.decay, doc.post_modified_gmt.value);
}",
'params' => [
"severe" => [
"scale" => "14d",
"decay" => 0.25, // the score to assign to a document at the scale + offset distance.
"offset" => "7d",
// Today's date in the format : strict_date_optional_time without time
"origin" => date('Y-m-d')
],
"mild" => [
"scale" => "183d",
"decay" => 0.6,
"offset" => "183d",
"origin" => date('Y-m-d')
],
]
]
];

return $formatted_args;
}
Expand Down

0 comments on commit 2675b66

Please sign in to comment.