Skip to main contentSkip to footer

Improve AND searches with term balancing

An AND search requires every search term to appear in a post. However, a post that mentions one term many times can still rank highly even if the other terms appear only once.

Term balancing reduces the relevance scores of posts with an uneven distribution of search terms. This can help posts that cover the whole query rank above posts that mainly discuss just one part of it.

Enable term balancing

Add this code to your site to enable term balancing:

add_filter( 'relevanssi_term_balance_power', function() {
    return 2;
} );

You can use a PHP snippet plugin configured to run everywhere, a custom plugin, or your child theme’s functions.php. No reindexing is needed.

What changes in the results?

Suppose someone searches for AI expert. These posts each contain 30 occurrences of the search terms:

Post AI expert Effect on its score
Balanced 15 15 Unchanged
Moderately uneven 10 20 Small reduction
Strongly uneven 1 29 Large reduction

 

Without term balancing, repeated mentions of “expert” may give the last post a high score. With term balancing, that score is reduced because “AI” appears only once. The balanced post keeps its original score.

This also works with longer queries. For AI expert consultant, counts of 10 / 10 / 10 keep the original score, while counts of 1 / 1 / 28 receive a strong penalty.

Other relevance factors, such as title weights, still apply. Term balancing can change the order, but does not guarantee that every balanced post ranks above every uneven post.

Adjust the strength

Change the number after return in the snippet:

  • 1 applies a gentler adjustment.
  • 2 is a good starting point.
  • 3 applies a stronger adjustment.
  • 0 disables term balancing.

For example, use this gentler adjustment if relevant posts are being pushed too far down:

add_filter( 'relevanssi_term_balance_power', function() {
    return 1;
} );

Use one snippet at a time. To turn the feature off, remove or deactivate the snippet.

Compare a few searches in Relevanssi’s Admin Search with and without the snippet to see how it affects your content.

Which searches does this affect?

Term balancing applies to AND searches with two or more search terms. It does not change OR searches, automatic fallback to OR, or single-term searches.

It does not require the words to appear together as a phrase. It compares their occurrence counts across indexed fields, so an incidental mention can still contribute. Giving important content sections more weight remains a separate way to improve relevance.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Polylang attachment searching
If Polylang is in use and you haven’t enabled “Media translation“, your attachment files won’t have a language. That’s fine,…
YITH Badge Management
The Premium version of YITH Badge Management has a search feature for badges. Using Relevanssi breaks that badge search. Fortunately,…

Related Posts:

Comment Section:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed