7

I've been trying to get this figured out for a couple days now, and I feel like I'm close, but as of yet no cigar. I need to make the apache solr search on the site I'm working on to have two sorts, 'created' and 'score'. The default search sort is score, but I need to change it to created.

I've got as far as changing the default itself, but then change is added unconditionally, so obviously it will always occur over score. Do I have to rebuild all the URLs to use arg, or is there an easier way? Here's my code:

Note: there's some code to remove some filters, but that's not related to the problem. I included them just in case.

   function myhook_apachesolr_prepare_query(&$query, &$params) {
     // Remove sorts
     $query->remove_available_sort('type');
     $query->remove_available_sort('sort_title');
     $query->remove_available_sort('sort_name');
     // Rename/Reorder sorts
     $query->remove_available_sort('created');
     $query->remove_available_sort('score');
     $query->set_available_sort('created', array('title' => t('Most Recent'), 'default' =>     'desc'));
     $query->set_available_sort('score', array('title' => t('Most Relevant'), 'default' => 'asc'));

     // Change the default sort to by date
     $query->set_solrsort('created', 'desc');
   }
2

You can implement hook_apachesolr_query_alter() and add new sort criteria as below

/**
 * Implements hook_apachesolr_query_alter().
 */
function my_module_apachesolr_query_alter($query) {
  $query->addParam('sort', 'ss_date_field asc');
}
| improve this answer | |
1

I was able to do it in 6, using the following:

<?php

function MYMODULE_apachesolr_prepare_query($query, $caller) {

  $query->remove_available_sort('sort_name');
  $query->set_available_sort('changed', array('title' => t('Date'), 'default' => 'desc'));

}
?>

See this d.org page for more: http://drupal.org/node/715276

| improve this answer | |
  • Maybe I'm not making myself clear; I know how to do this step and it works, the problem is that when you do a search the default sort is still by score (or relevancy) instead of by date. The order they appear on the page is changed, but the default search sort is not. – bronzehedwick Sep 5 '12 at 13:21
1

The way we are using the sort options is to expose the sort options block, which I am not sure you have done. If you have, then it will generate the correct links for you - you don't have to do anything else than consider that the so

<?php
// solrsort is the query parameter the block uses
if (!isset($_GET['solrsort']) || empty($_GET['solrsort'])) { 
  $query->set_solrsort('created', 'desc');
}
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.