Adding extra boost for exact title matches

Every now and then somebody wants to see exact title matches higher in the results. Usually the best way to go about with this is to simply increase the title weight, maybe switch the default operator to AND and let the Relevanssi algorithm lift the best results on top.

If that is not enough, you can use this code to give a boost for exact title matches:

add_filter('relevanssi_results', 'rlv_exact_boost');
function rlv_exact_boost($results) {
	$query = strtolower(get_search_query());
	foreach ($results as $post_id => $weight) {
		$title = strtolower(get_the_title($post_id));
		if ($title == $query) $results[$post_id] = $weight * 1000;
	}
	return $results;
}

This code, placed in for example theme functions.php, gives a solid boost for posts with titles that match the search query. That is usually enough to lift the exact title matches on top of the search results.

(If you want to be 100% sure, you can write a relevanssi_hits_filter function that moves the exact title matches on top of the results.)