Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelia/8fc76ae6e2eda6d69d8cc7f21086f6eb to your computer and use it in GitHub Desktop.
Save felipeelia/8fc76ae6e2eda6d69d8cc7f21086f6eb to your computer and use it in GitHub Desktop.
Add boosting to ElasticPress queries. THIS IS NOT COMPLETE YET.
<?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