Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have approached this multi-domain search requirement a different way now anyway.
Instead I add radio buttons to the search forms for searching all domains or a specific one. Then I just use the default score sort.
Not exactly the same result but works well. Probably even more user friendly.
Multiple sort fields would be a useful addition though at some point.
I have faked out the sort to allow multiple sort fields like this:
1) I create my own custom override of the Solr_Base_Query.php class. It inherits from Solr_Base_Query, and it's defined in the custom module's install:
function example_solr_service_install() {
variable_set('apachesolr_query_class', array('example_solr_service', 'Example_Custom_Solr_Query'));
}
2) In the Example_Custom_Solr_Query object, I muck with the values it returns from get_solrsort, like so:
public function get_solrsort() {
return array(
'#name' => $this->solrsort['#name'],
'#direction' => $this->solrsort['#direction'] .',sort_title asc',
);
}
In this case, I'm sorting on the #name and #direction fed to apachesolr_search_execute, but I return a little extra to the direction. I've found that the function that calls get_solrsort() doesn't care what's in these fields or validate them in any way, it just puts them together with a space between them and sends them to the solr engine.
It's a hack, but it works.
(NB: Solr doesn't seem to like spaces around the commas separating the items you're sorting on.)
The recent changes in 7.x to the query class should bring it much closer to handling multiple sorts
Basically, omeone just needs to go and make it handle multiple solrsort[]= params instead of a single string and handle merging too/from the internal params.
This will get broken by subsequent calls to setSolrsort(), but it does work to get multiple sorts working in 7.x-1.x. It is still a bit clunky, in that $query->sortstring remains blank.
/**
* Implements hook_apachesolr_query_alter().
*/
function example_apachesolr_query_alter($query) {
// Use addParam() rather than setSolrsort() for multiple.
$query->addParam('sort', 'sort_title asc');
$query->addParam('sort', 'score desc');
// Becomes: sort_title asc,score desc
}
Is there a possibility yet to achieve anything like in #9 (sending something like this to Solr: sort_title asc,score desc) ? Or could someone explain #10 in more detail?
Comments
Comment #1
pwolanin CreditAttribution: pwolanin commentedcurrent code does not support multiple sorts - the sort string parsing would need to be fixed up
Comment #2
Agileware CreditAttribution: Agileware commentedThat's what I expected.
I have approached this multi-domain search requirement a different way now anyway.
Instead I add radio buttons to the search forms for searching all domains or a specific one. Then I just use the default score sort.
Not exactly the same result but works well. Probably even more user friendly.
Multiple sort fields would be a useful addition though at some point.
Comment #3
jcfiala CreditAttribution: jcfiala commentedI have faked out the sort to allow multiple sort fields like this:
1) I create my own custom override of the Solr_Base_Query.php class. It inherits from Solr_Base_Query, and it's defined in the custom module's install:
2) In the Example_Custom_Solr_Query object, I muck with the values it returns from get_solrsort, like so:
In this case, I'm sorting on the #name and #direction fed to apachesolr_search_execute, but I return a little extra to the direction. I've found that the function that calls get_solrsort() doesn't care what's in these fields or validate them in any way, it just puts them together with a space between them and sends them to the solr engine.
It's a hack, but it works.
(NB: Solr doesn't seem to like spaces around the commas separating the items you're sorting on.)
Comment #4
pwolanin CreditAttribution: pwolanin commentedComment #5
pwolanin CreditAttribution: pwolanin commentedComment #6
wouters_f CreditAttribution: wouters_f commentedMaybe It could be usefull to get soem of the best sort solutions in the apachesolr_sort module.
I already added some functionalities here:
http://drupal.org/sandbox/wouters_frederik/1124756
Comment #7
pwolanin CreditAttribution: pwolanin commentedThe recent changes in 7.x to the query class should bring it much closer to handling multiple sorts
Basically, omeone just needs to go and make it handle multiple solrsort[]= params instead of a single string and handle merging too/from the internal params.
Comment #8
Agileware CreditAttribution: Agileware commentedThe solution in #3 worked for me in D6, thanks for the idea.
Comment #9
ryandekker CreditAttribution: ryandekker commentedThis will get broken by subsequent calls to setSolrsort(), but it does work to get multiple sorts working in 7.x-1.x. It is still a bit clunky, in that
$query->sortstring
remains blank.Comment #10
Nick_vhFields that are set multiple now get indexed as a single to support the sorting functionality. Marking as fixed until reopened
Comment #11
Nick_vhComment #12
Anonymous (not verified) CreditAttribution: Anonymous commentedIs there a possibility yet to achieve anything like in #9 (sending something like this to Solr:
sort_title asc,score desc
) ? Or could someone explain #10 in more detail?