Home / The S-Files: Adding a custom sort to Acquia Search (Apache Solr)

The S-Files: Adding a custom sort to Acquia Search (Apache Solr)

Tech Support Case Studies

We've received several inquiries about adding custom sorts to the the sorting block on search results for our popular Acquia Search product. This is fortunately quite easy, though it currently involves preparing a custom module that implements one hook:

function hook_apachesolr_prepare_query(&$query) {
// You can get the 'solr_field_name' from q=admin/reports/apachesolr
$query->set_available_sort('solr_field_name', array(
// how the sort link is to appear in the sorts block
'title' => t('Name of sort'),
'default' => 'asc', // or 'desc'

Not all fields can be sorted on. According to the Solr Wiki:

Sorting can be done on the "score" of the document, or on any
multiValued="false" indexed="true" field provided that field is either
non-tokenized (ie: has no Analyzer) or uses an Analyzer that only
produces a single Term (ie: uses the KeywordTokenizer)

Maybe we'll see the appearance of a contributed module that lets people define their own custom sorts. Acquia partner Krimson was working on such a module but I haven't seen it appear on Drupal.org.


Posted on by Stijn Vanden Brande (not verified).