• We have a list of part numbers as a custom post type set by Toolset, “Parts” the title of the posts are the part numbers. If someone is searching for part number “210” the actual post titled “210” shows up at number 20 in the results.

    We have tried adjusted the keyword matching options, exact matching, as well as implementing this code below:
    add_filter( ‘relevanssi_match’, ‘rlv_prefer_whole_words’, 10, 3 );
    function rlv_prefer_whole_words( $match, $idf, $term ) {
    if ( $match->term === $term ) {
    $match->weight *= 1000;
    }
    return $match;
    }
    We still can’t get the actual post titled “210” to show up first. Here is what all posts are showing as their relevance score:
    MXP-210-00 part (Edit part)
    Smoke Goggles
    Score: 2402.1

    338-108-210-0 part (Edit part)
    BLADE
    Score: 2402.1

    Then here is the post we are wanting to show first followed by all the others:
    210 part (Edit part)
    CONNECTOR SMA, PLUG
    Score: 2402.1

    Same score.

    Any thoughts or help on how we can get exact matches first followed by those with it in the title or body content?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari

    (@msaari)

    All of these posts have the word 210 in the title, that’s why they are getting the same score. The filtering function boosts them all.

    Do you want “210” to find the “338-108-210-0” post in the first place? If not, go to Settings > Relevanssi > Indexing > Advanced indexing settings and set “Hyphens and dashes” to “remove” and rebuild the index. Now 210 won’t match to those partial part numbers at all. That should make the search more precise.

    Thread Starter zesgar

    (@zesgar)

    Thanks for the suggestion. We really need the post “210” to show up first followed by any of the other posts with 210 in the title. Example searching “210” would show the post “210” first then others with 210 in the title like the post “338-108-210-0” or “MXP-210-00”.

    Plugin Author Mikko Saari

    (@msaari)

    In that case do not adjust the hyphen indexing setting, but instead enable the “Exact match bonus” from the Relevanssi searching settings.

    Thread Starter zesgar

    (@zesgar)

    We already have the exact match bonus enabled in the settings and still the exact match isn’t showing first. I have also disabled all other plugins to ensure this wasn’t a conflict with another plugin and that didn’t help either. Thoughts?

    Plugin Author Mikko Saari

    (@msaari)

    Ah, I see. The exact match bonus doesn’t actually help here, it has the same problem the original function you used. It just checks if the search term appears in the title, and it does appear in all of the searches (it’s really made for multi-word search phrases, where it works much better).

    This should do the trick:

    add_filter( 'relevanssi_results', 'rlv_perfect_match_boost' );
    function rlv_perfect_match_boost( $results ) {
        $query = get_search_query();
        array_walk(
            $results,
            function( &$weight, $post_id ) use ( $query ) {
                if ( 0 === strcasecmp( $query, get_the_title( $post_id ) ) ) {
                    $weight *= 100;
                }
            }
        );
        return $results;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exact Single Word Search not showing first’ is closed to new replies.