wpfts_cluster_weights (Filter)

The wpfts_cluster_weights filter in WP Fast Total Search allows developers to modify the cluster weights used in calculating the relevance of search results. Clusters are logical groups of data, such as the post title (post_title), post content (post_content), excerpt (post_excerpt), and any other clusters defined by indexing rules. The cluster weight determines how important the data of that cluster is during a search. The higher the weight, the greater the influence the term match in that cluster has on the relevance of the result.

When to Use

This filter is useful in the following cases:

  • Adjusting the importance of different content parts during a search. For example, you can increase the weight of the title so that posts with title matches appear higher in the search results.
  • Dynamically changing cluster weights depending on query parameters or other conditions.

Arguments

  • $cluster_weights (array): An associative array of cluster weights. The array keys are the cluster names, and the values are weights (numbers from 0 to 1).
  • $wpq (object): The WP_Query object containing the parameters of the current query.

Return Value

  • $cluster_weights (array): The modified associative array of cluster weights.

Example

/**
 * Increases the weight of the post title and decreases the weight of the content.
 */
add_filter('wpfts_cluster_weights', 'my_wpfts_cluster_weights_filter', 10, 2);
function my_wpfts_cluster_weights_filter($cluster_weights, $wpq) {
  $cluster_weights['post_title'] = 1; // Maximum weight for the title.
  $cluster_weights['post_content'] = 0.2; // Lower weight for the content.
  return $cluster_weights;
}

Example of Dynamic Weight Change

add_filter( 'wpfts_cluster_weights', 'adjust_cluster_weights', 10, 2 );
 
function adjust_cluster_weights( $cluster_weights, $wpq ) {
	if ( is_tax( 'product_cat' ) ) { // Change weights if on a product category page
		$cluster_weights['product_title'] = 0.9;
		$cluster_weights['product_description'] = 0.6;
	}
 
	return $cluster_weights;
}

Important Notes

  • Cluster weights must be numbers from 0 to 1. A value of 0 means that the data of this cluster is not considered when calculating relevance.
  • The wpfts_cluster_weights filter is called before the search is executed.

This filter provides developers with the ability to fine-tune the search result ranking algorithm in WP Fast Total Search by changing the importance of different data clusters.