Posted on

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 do this is to increase the title weight, maybe switch the default operator to AND and let the Relevanssi algorithm lift the best results.

If that is not enough, you can use this code to give a boost for exact matches in titles and post content:

add_filter( 'relevanssi_results', 'rlv_exact_boost' );
function rlv_exact_boost( $results ) {
	$query = strtolower( get_search_query() );
	foreach ( $results as $post_id => $weight ) {
		$post = relevanssi_get_post( $post_id );

		// Boost cases where query appears in the title
		if ( stristr( $post->post_title, $query ) !== false ) {
			$results[ $post_id ] = $weight * 100;

      	// Boost cases where query is exactly the title
  		if ( strtolower( $post->post_title ) === $query ) {
			$results[ $post_id ] = $weight * 100;

		// Boost query appearances in post content
		if ( stristr( $post->post_content, $query ) !== false ) {
			$results[ $post_id ] = $weight * 100;

		// Boost exact matches in a custom field
		if ( get_post_meta( $post_id, '_sku', true ) === $query ) {
			$results[ $post_id ] = $weight * 100;
	return $results;

Relevanssi has the basic title and content exact matching built-in.

Add this code to your site to boost posts with titles and content that match the search query.

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

add_filter( 'relevanssi_hits_filter', function( $hits ) {
    $exact_title = array();
    $other_posts = array();
    foreach ( $hits[0] as $hit ) {
        if ( strtolower( $hit->post_title ) === strtolower( $hits[1] ) ) {
            $exact_title[] = $hit;
        } else {
            $other_posts[] = $hit;
    $hits[0] = array_merge( $exact_title, $other_posts );
    return $hits;
} );

13 comments Adding extra boost for exact title matches

  1. Hi mr. Saari,
    I’m using your plugin on a woocommerce install. I’m using it only to products. How to modify this code to give extra weight to product title instead of post title?
    thank you

  2. Hi Mikko,
    I got the most matched on top of result. If my search is “iphone X”, so results are:

    Iphone x Blue
    Iphone x Black
    Iphone 7
    Iphone 6

    How to only display match 100% results? (Remove Iphone 7 and Iphone 6 in result)?

    1. Will, try adding this to your theme functions.php:

      <pre lang="php">add_filter('relevanssi_results', 'rlv_exact_boost');
      function rlv_exact_boost($results) {
      	$query = strtolower(get_search_query());
      	foreach ($results as $post_id => $weight) {
      		$post = relevanssi_get_post($post_id);
      		if (stristr($post->post_title, $query) === false) $results[$post_id] = $weight * 0;
      	return $results;

      This should remove all posts where the serach query isn’t found in the title (that may be too harsh, but it’s a start).

  3. Hi Mikko,
    How can I show all results that contain a partial word, ex:
    I have some products 6MB200 and 6MB200-4 when Im search “6MB200” only show one result on the query “6MB200-4”.

  4. I’m struggling a bit to give Titles a higher boost and have tried a few things with no luck. Here’s some more information on the problem:
    -When I search for the word “discounts” it prioritizes a lot of content over the page that is titled “Discounts & Policies”. Here’s a screenshare:
    -Here are the weights I tried. They did not make any adjustment to the order the Discounts & Policies page appeared on the screen.
    –Content: 1
    –Titles: 1000
    –Comment text: 0
    –Tag weight: 1
    –Category weight: 1
    -When I do the search, there is a split second where I can see Discounts & Policies at the top of the results before it reorders everything. I’ve tried to capture a screenshare that shows this for you. I’m not sure why it displays more correct results and then changes.

    Let me know if there’s any other information I can provide to help. Thank you!

    1. Stephanie, that’s a weird one – looks like Relevanssi is indeed returning the correct results, then something changes it. I did some debugging on the page, and if I look at what the server returns, the page the browser receives does have the Discounts & Policies post first. So, Relevanssi is functioning correctly and no amount of adjusting Relevanssi settings can help here – Relevanssi is already returning correct results.

      I couldn’t figure out what’s reordering the results on the page; I would ask the theme support, perhaps they can help better.

        1. Sorry to bother you again. We have gone through our theme and all of our plugins without finding any issues, and we still get the same results (where Discounts & Policies appears at the top and then moves), even when Relevanssi is turned off this happens. We’re able to adjust other weighting (such as turning off the content completely), so we’re not sure why this works but this other weighting does not.

          I’m willing to purchase the premium version of Relevanssi if that will fix my problem. I just am hesitant without knowing what is causing this to not order correctly by weight.

          Thoughts? Thank you!

          1. I took another look, and figured out something. When I search for “discount”, the posts are divided into two divs: “blog-odd” for odd-numbered posts and “blog-even” for even-numbered posts. For some reason, all the even numbered posts go in the “blog-odd” div and vice versa, and then the “blog-odd” div is displayed first. So, first you’re seeing posts number 2, 4, 6, 8, and 10, and then you’ll see the “blog-even” div with posts 1, 3, 5, 7, and 9.

            The way this is supposed to work is that the blog-odd and blog-even classes should have float: left and width: 50%, so they appear next to each other. However, in your custom CSS you have set blog-odd and blog-even to be width: 100%, so the odd posts appear first and the even posts after them. If you remove that width: 100%, you’ll see that it actually works pretty well.

            This still leaves the question why the odd posts are sorted into the even div and vice versa, but that’s probably just some small mistake in the logic. The individual posts get “odd” and “even” labels correctly, they just get pushed into the wrong div.

            So take another look at your theme, this is probably a theme feature.

  5. I’m having same issue as Stephanie above.. I have change search from “or” to “and” removed partial word and gave the settings below to weights…

    and I search four different times for three seperate phrases before making edits and after and the results never changed – not once – same result as before and no matter how i search – page is here…

    notice rejoice is in title and the results don’t change no matter what you search for…

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *