Support » Plugin: Relevanssi - A Better Search » Problem indexing "&" character

  • Resolved millerbrothers

    (@millerbrothers)


    One site we are utilizing Relevanssi on has many pages that contain product names that include the “&” character. For instance, “C & D Valves Co” would be a search term we’d like to get results on. However, we do not get any hits when entering that term or variations of it.

    For the moment, we’ve resorted to adding a bit of content at the foot of the page that writes the product name out as “C and D” so that it can show up in the Relevanssi results. But I have a feeling we can do better than this.

    What settings might I need to change to allow Relevannsi to find my search terms with the “&” character? I had seen a topic that dealt with the “remove_puncuation” function, but on reviewing the code for that function I do not see any reference to removing the “&” character. Commenting it out didn’t seem to make a difference either. I’ll keep searching for answers, but I appreciate your expert response if/when you have the time to reply. Thanks!

    http://wordpress.org/extend/plugins/relevanssi/

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

    (@msaari)

    Something like “C & D Valves Co” is a very difficult search term, as by default it becomes “C D Valves Co”, which for Relevanssi is equal to “Valves”, because Relevanssi only indexes words that are three characters or longer. (So that “C and D” thing should not do much good, either.)

    You were looking at the right place. It’s in this:

    $a = preg_replace('/:punct:+/u', ' ', $a);

    That includes the ampersands.

    If you want to keep the ampersands, you can do this:

    add_filter('relevanssi_remove_punctuation', 'fixampersands1', 9);
    function fixampersands($a) {
        $a = str_replace('&', 'ZZZZZZZZZZ', $a),
        return $a;
    }
    add_filter('relevanssi_remove_punctuation', 'fixampersands2', 11);
    function fixampersands($a) {
        $a = str_replace('ZZZZZZZZZZ', '&', $a),
        return $a;
    }

    This will keep the ampersands in the index (you need to reindex the whole database before this does anything, by the way). It will not help with searches like “C & D Valves Co”, though, as that’ll still appear as “Valves” to Relevanssi, unless you make Relevanssi index words that are just one character long (which causes other problems).

    Hello. I’m having a similar problem with the custom tag “9/11”, but I can’t find any trace of “relevanssi_remove_punctuation” in the code for v3.1.3. Have I lost my mind?

    Something must have been wrong with my editor’s search/grep – I see the code now.

    Just a couple small fixes to the “keep ampersands” code above (duplicate function names and commas instead of semi colons):

    add_filter('relevanssi_remove_punctuation', 'fixampersands1', 9);
    function fixampersands1($a) {
        $a = str_replace('&', 'ZZZZZZZZZZ', $a);
        return $a;
    }
    add_filter('relevanssi_remove_punctuation', 'fixampersands2', 11);
    function fixampersands2($a) {
        $a = str_replace('ZZZZZZZZZZ', '&', $a);
        return $a;
    }
    Plugin Author Mikko Saari

    (@msaari)

    Heh, thanks for the fix.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Problem indexing "&" character’ is closed to new replies.