The default sort option is relevancy, at the moment it is not possible to change this.
I've attached a patch creates a property for default search, and default search direction, and a setter for them both to SolrBaseQuery. This setter can be called from hook__apachesolr_query_prepare. Example usage:
function MYMODULE_apachesolr_query_prepare($query) {
$query->setDefaultSort('ds_created', 'desc');
}
Please note there is a snippet of code which has been shared on a previous issue (https://drupal.org/node/721258) -
/**
* Implements hook_apachesolr_prepare_query().
*/
function MYMODULE_apachesolr_query_prepare($query, $caller) {
// If no manual sort set in querystring, set default sort to created desc
if (!isset($_GET['solrsort'])) {
$query->setSolrSort('ds_created', 'desc');
}
}
This does change the default sort order, BUT it also prevents the relevancy sort order from being used. This is because the relevancy sort order link doesn't include the relevancy query parameters (?solrsort=score%20desc), and so the search order gets set to ds_created.
Comments
Comment #1
__cj commentedComment #2
__cj commentedComment #3
nick_vhThere's already a module that does this for you? It's apachesolr_sort. Let me know if that is sufficient and if you can, please close the ticket afterwards :-)
Comment #4
nick_vhClosing unless a strong argument comes back that we should put this in the main module
Comment #5
__cj commentedThat's what I'd assumed.
The Apache Solr Sort lets you change the order of the sort by options, and disable the sort option, although it doesn't seem possible to disable the default sort option.
I would strongly agree the UI for this belongs with the the Apache Solr Sort, and I intend on sending a patch their way when it's possible to change the default sort order.
All this patch does is provide an easy way to change the default sort order with code.
Comment #6
__cj commentedComment #7
__cj commentedAttached is an updated patch.
Comment #8
mas0h commentedApache solr sort module only works if a keyword is not given to the search box, so this patch will be useful when a search keyword is given, but what this patch do after applying it? I can't see any settings after applying the patch?!
Comment #9
__cj commentedThis patch only adds a function to set the default search order, which is hardcoded to relevancy in the code. There are no accompanying setting here.
Comment #10
nick_vhSpacing issues here in the comments.
Would it be useful to get a more simple getter function for the default sort?
Aside of that it looks good
Thanks for your contribution. It's very much appreciated and I hope you continue to contribute to our issue queue. We can use all the help we can get.
Comment #11
nick_vhComment #12
__cj commentedUpdated patch attached.
Comment #13
__cj commentedComment #16
DeFr commentedThis is missing some change to SolrBaseQuery::getSolrsortUrlQuery, to remove the hardcoded assumption of score being the default sort too.
Tentatively setting to NW to be able to RTBC the patch afterwards ;-)
Comment #17
a.sotirov commentedI just have made a new version of the patch for 7.x-1.12 which is basically the latest stable one.