Posted on

Index only short numbers

Relevanssi has a minimum word length setting that is set to three characters. Any one- or two-letter words are not indexed unless you change this setting from the Relevanssi advanced indexing settings. Sometimes it would be helpful to ignore short words but to index one- and two-digit numbers.

You can do that with a bit of help. Adjust the minimum word length to 1 or 2 depending on your needs, add this function to your site and rebuild the index:

add_filter( 'relevanssi_remove_punctuation', function( $str) {
    return implode(
        ' ',
        array_filter(
            explode( ' ', $str ),
            function( $word ) {
                if ( relevanssi_strlen( $word ) < 3 ) {
                    return is_numeric( $word );
                }
                return true;
            }
        )
    );
} );

This function will let short numbers through while blocking short words.

3 comments Index only short numbers

  1. I’m not sure this snippet is working. After adding it to my functions.php file, and rebuilding the index, searching for two-digit numbers still returns “No Results”

    Default view (no search): https://tppr.me/5Ymd1D (notice the two and three-digit office numbers marked on the screenshot)
    Three-digit number search: https://tppr.me/QxdmvB (returns results)
    Two-digit number search: https://tppr.me/Cmlr9c

    Is there a chance that the provided function (that should enable indexing < 3 character words if they are numerical) is not working?

    1. Daniel, did you also adjust the minimum word length to 2 from the advanced indexing settings?

      The function works for two-digit numbers, but in your case, it’s possible this function runs before the one that removes punctuation, so “2-46” is not a two-digit number when this function runs. Try changing the priority of this function to bigger than 10 (add_filter( 'relevanssi_remove_punctuation', function( $str) {...}, 11 );).

      1. Hi Mikko, thank you for the prompt reply.

        Changing the priority of the function solved the issue.

        Thanks once again, appreciate the quick response!

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 *