Created
July 5, 2023 17:11
-
-
Save felipeelia/8fc76ae6e2eda6d69d8cc7f21086f6eb to your computer and use it in GitHub Desktop.
Add boosting to ElasticPress queries. THIS IS NOT COMPLETE YET.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: ElasticPress - Custom Query Boosting | |
* Description: Add Boosting query to ElasticPress | |
* Version: 1.0.0 | |
* Author: 10up | Felipe Elia | |
* Author URI: https://10up.com/ | |
* Text Domain: elasticpress-custom-query-boosting | |
* Domain Path: /languages | |
* | |
* @package ElasticPressCustomQueryBoosting | |
*/ | |
namespace ElasticPressCustomQueryBoosting; | |
/** | |
* Adds 'boosting' to the Elasticsearch query | |
* | |
* @param array $formatted_args Formatted Elasticsearch query. | |
* @param array $args WP_Query arguments | |
* @param \WP_Query $query WP_Query object | |
* @return array | |
*/ | |
function add_boosting_query( $formatted_args, $args, $query ) { | |
if ( ! $query->is_main_query() ) { | |
return $formatted_args; | |
} | |
if ( ! isset( $formatted_args['query'] ) ) { | |
$formatted_args['query'] = []; | |
} | |
$boosting = [ | |
'positive' => [ | |
'term' => [ | |
'meta._stock_status.value' => 'instock', | |
], | |
], | |
'negative' => [ | |
'term' => [ | |
'meta._stock_status.value' => 'outofstock', | |
], | |
], | |
'negative_boost' => 0.05, | |
]; | |
$formatted_args['query']['boosting'] = $boosting; | |
return $formatted_args; | |
} | |
add_filter( 'ep_post_formatted_args', __NAMESPACE__ . '\\add_boosting_query', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment