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:
hook_apachesolr_prepare_query(&$query)
<?php
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.
Related Content
AcquiaBlog

2010 has been an inflection point for the Acquia partner program. We are doing more business than ever with partners, including case studies with Palantir.net, Blink Reaction, and IBM Global Services.
Bryan House
It is that phase of my life! I'm just turning 30 in a month, working with Drupal for 7 years and just had my third Acquia anniversary a week ago. Time to look back and evaluate how things went, all the good and bad things; even better if the wisdom can be shared with others. This was part of my thinking when I submitted the session titled "Come for the software, stay for the community" for Drupalcon Copenhagen.
Gábor Hojtsy
It sounded like a really simple request: "Is it easy to add a search filter for 'My posts'?". In other words, add a search result facet for posts by the current (logged in) user through the Apache Solr Search Integration module APIs?
But then the wheels start turning - we want not just one blind link, but a real facet link that tells us how many results we'll get. Also, if we are filtering by 'My posts' then we probably have an equal use case for the opposite filter 'Posts not by me'. So we really need a facet block with two links and facets counts.
Peter Wolanin







Comments
The module is now available
The module is now available on the krimson website. And we started the request for a CVS account.