Posted on

Related searches feature

John Blackbourn wrote a cool related searches feature for Relevanssi Premium. It’s included from version 1.7.4.

In order to use the related searches, you must have search logging enabled and a good log of past searches. Then add the following code to your search results template in a suitable place:

if (function_exists('relevanssi_related'))
    relevanssi_related(get_search_query(), '<h3>Related Searches:</h3><ul><li>', '</li><li>', '</li></ul>', 5);

The first parameter is the search query and the last parameter is the number of searches to show. The rest are about the display.

The function looks at the search log and returns queries which have at least one matching token with the current search. Searching for “john” will match both “john blackbourn” and “john smith”. Searching for “john blackbourn” will match “john smith”, but won’t match “john”, because the function doesn’t suggest queries that consist of only one token.

Also, only queries that have at least two hits and have been made at least twice are included.

When you link to your search results pages like this, it’s highly recommended to tell Google not to display them. Either put robots meta field in your search results pages header saying “noindex”, or use robots.txt to prevent bots from accessing your search results pages. Instructions can be found at Robotstxt.org.

4 comments Related searches feature

  1. As Mikko points out, this feature works really well in theory but in practice your site will need a lot of previous search data before it starts showing useful suggestions (or any suggestions at all).

    The function was written more as an excercise for myself rather than a feature that would get used (although I am using on a couple of sites). A better way to determine related search terms would be to parse the content itself and pick out common related terms, but that’s a whole different ball game.

  2. Hi Mikko!
    unfortunately the function doesn’t work..switching on the error logs there is highlighted a SQL Error + a php notice:

    [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE query LIKE ‘%pediaria%’ AND query NOT IN (‘pediaria’, ‘pediaria’) AN’ at line 3]
    SELECT query FROM WHERE query LIKE ‘%pediaria%’ AND query NOT IN (‘pediaria’, ‘pediaria’) AND hits > 1 GROUP BY query HAVING count(query) > 1 ORDER BY count(query) DESC LIMIT 5

    Pediatra is my search key.

    Notice: Undefined index: relevanssi_log_table in /var/www/domini/aslcn2/wp-content/plugins/relevanssi-premium/relevanssi.php on line 1253

    I’m using Relevanssi Premium 1.10.14

    Thanks a lot in advance!

    1. Looks like there’s an error in the code. I’ll get that fixed in the next version, but meanwhile on line 1253 of relevanssi.php file, change

      FROM ” . $relevanssi_variables[‘relevanssi_log_table’] . ”

      to

      FROM ” . $relevanssi_variables[‘log_table’] . ”

      and it should work.

Leave a Reply

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