Contact Krimson

Call us: +32 (0) 3 298 69 98

Email us: info@krimson.be

Apache Solr custom sorting

Skip the general crap - give me the details please

Apache Solr is a great solution for implementing faceted search into a Drupal website. (have a quick read on faceted-search and facets if you don't know what we're talking about)
Through the work of Robert Douglas and others Drupal has this functionality virtually out of the box.
Installing a Solr Server can sometimes be a hassle though, so we recommend reading the lucene tutorial or the krimson tutorial prior to the attempt. Or just try the acquia distribution which connects to acquia's farm of solr servers with one click. Thus far the good news.
The problem with current Drupal solution is that it does not yet provide all the power that Apache Solr gives us. We have tackled the fact that it is currently only possible to sort search results on a strict set of only 4 fields (relevancy, title, author, date) which are in fact the only fields common to All nodes.

Technical
For project xyz we needed our results to be sorted on custom CCK field points.
sort.jpg
It is possible to add a sort type using 'hook_apachesolr_prepare_query', this is fully explained on the acquia blog. This fixes our problem to add a sort type, however it does not solve the issue of having to disable or delete some of the standard sort types. In order to solve that we dove back into the code and found that we can overwrite the class that implements the sort functions. The class is named as follows:

list($module, $class) = variable_get('apachesolr_query_class', array('apachesolr', 'Solr_Base_Query'));

So we copied the standard class from the apache solr module and edited it as to support the new functionality we had added. To keep it as maintainable as possible we made the least possible changes. (we only changed 'default_sorts()', 'get_available_sorts()' and 'set_available_sort()' ).

Once we had this solved we wanted to extend this idea to a more generic solution for ANY CCK field, that meets the requirements specified here.

To be more generic we wanted the module to support the following functions:

  • add / edit sort types
  • enable / disable sort types
  • delete sort types (not the standard sort types)

In order to add this we had to keep a few extra fields about a sort type (standard, status), so we saved the sort types using 'variable_set'.
The next challenge was to get the fields that are mapped to our solr instance. We found that this was implemented in the standard solr class ( Drupal_Apache_Solr_Service->getLuke()->fields ), so now we can provide the user with a list of fields to choose from (basic knowledge of how the fields are mapped is still required though).

So thank you Drupal - You can download the module here.
These upcoming weeks we will decide whether to add this to the current apache solr module or provide it as a separate module.

AttachmentSize
apachesolr_custom_sort.zip15.12 KB