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.

  • http://lud.icro.us/ John

    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.