Quantcast
Channel: realize.be - search
Viewing all articles
Browse latest Browse all 2

Random results with Apache Solr and Drupal

$
0
0

The schema.xml that comes with the Drupal Apache Solr module doesn't define the random_* field compared to the default xml included in the apachesolr package. We needed that functionality for a project where we wanted to display 3 blocks which showed random results based on a couple of fields available in the node, in our case the author, title and a cck field. With 300k nodes, a random result was giving a nicer experience instead of seeing the same results coming back over and over. Adding random order is pretty easy in a few simple steps: http://lucene.apache.org/solr/api/org/apache/solr/schema/RandomSortField...

Implementing the tags from that manual did not have a lot of success, however, after some fiddling, following changes in the xml seem to do the trick. Feel free to add comments and suggestions.

   <!-- goes in types -->
    <fieldType name="rand" class="solr.RandomSortField" indexed="true" />

  <!-- goes in fields -->
   <dynamicField name="random*" type="rand" indexed="true" stored="true"/>

After indexing your nodes, try to run following query on your solr admin page:

http://localhost:port/solr/select?q=whatever&morekeyshere&sort=random_127789 desc

Our blocks are defined via hook_block which uses the apachesolr_search_execute() function to launch our query to the solr engine. With the hook_apachesolr_modify_query you can add a sort parameter and you'll get your random results.

<?php
function hook_apachesolr_modify_query(&$query, &$params, $caller) {
  if (
$caller == 'whatever') {
   
$seed = rand(1, 200);
   
$params['qt'] = 'standard';
   
$params['sort'] = 'random_'. $seed .' asc';
  }
}
?>

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images