Random sorting of search results

Managed by | Updated .

Background

There are some circumstances when ranking and sorting of any type doesn't make sense and it is desirable to randomise the result ordering.

Funnelback supports this using the shuffle sort mode, which randomises the results returned.  When using random sorting a random seed must also be set to ensure that the results are randomised for the user's search session (to preserve the random order across result pages of an executed search) otherwise each page of results will be completely random.

Implementing random search

Funnelback supports randomised search results by using a combination of two query processor options. In order to use this the following is required:

  • -sort=shuffle (query processor option) or sort=shuffle (CGI parameter)
  • -rseed=N (query processor option) or rseed=N (CGI parameter), where N is an integer value for the random seed.

The rseed value MUST be passed between searches in the same session (eg. paginated results) otherwise every page of results will be completely randomised.

For truly randomised search the rseed value should be set dynamically for each search (so not as a query processor option) otherwise you'll always get the same random sort for all queries.

The rseed value isn't set automatically - if you set sort=shuffle you'll need a hook script to set this value, and you'll need to ensure your forms and all search UI links pass through the rseed value.

Modern UI - pre process hook script:

hook_pre_process.groovy
//def logger = org.apache.log4j.Logger.getLogger("com.funnelback.tassieTradePreProcessHook");
def q = transaction.question
// Set random seed (only if in shuffle mode, and if not already set)
if (q.inputParameterMap["sort"] != null) {
  if (q.inputParameterMap["sort"] == "shuffle") {
    if (q.inputParameterMap["rseed"] == null || q.inputParameterMap["rseed"] == "") {
      q.inputParameterMap["rseed"] = Math.abs(new Random().nextInt() % 9999 + 1).toString();
      //logger.info("Setting random seed: " + q.inputParameterMap["rseed"]);
    }
  }
}

Classic UI - pre perform query hook script:

pre_perform_query.pl
#Hook script to add a random seed if required
if ($qp_api->{'QUERY_STRING'}=~/(?i)sort=shuffle/) {
    if ($qp_api->{'QUERY_STRING'}!~/(?i)rseed=\d+/) {
        my $rs=int(rand(9999));
        $qp_api->{'QUERY_STRING'}.="&rseed=$rs";
        $orig_query_string.="&rseed=$rs";
    }
}
Was this artcle helpful?

Comments