Skip to main contentSkip to footer

My Relevanssi engine has the AND operator enabled, with a fallback to OR when the search finds no matches. I’d like the search to display first the AND results and later the OR results.

For example, if I search for “wedding party”, I’d like the search to show first the entries matching both words and later the entries matching only “wedding” or only “party”.

Is it possible to configure or program a search like that?

What you’re asking for is pretty much what the OR search aims to do. It’s not strict, though, so if some post seems to be valuable even if it doesn’t have all the search terms, it will be high on the list, but most of the time if all search terms are good search terms, AND matches will be on the top.

If you really want to push it further, perhaps something like this with the relevanssi_match filter hook:

add_filter( 'relevanssi_match', 'rlv_and_boost' );
function rlv_and_boost( $match ) {
    global $rlv_and_database;
    $multi_term_boost = 5;
    if ( ! isset( $rlv_and_database[ $match->doc ] ) ) {
        $rlv_and_database[ $match->doc ] = true;
    } else {
        $match->weight *= $multi_term_boost;
    }
    return $match;
}

With this function in place, posts that match more than one search term will get a bonus. The more search terms the post matches, the bigger the bonus. You can adjust the boost factor until you get results you like.

This was asked here.

Your account

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

Search

Popular Resources

Indexing trashed posts
By default, Relevanssi does not index trashed posts. If you want to enable searching for trashed posts in the WordPress…
The search form shortcode
Relevanssi includes a search form shortcode that can be used to display a search form. The shortcode is searchform and…

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