| Project: | Apache Solr Search Integration |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
Currently in RC2 it is really cumbersome to add a simple custom sort due to the way the apachesolr_search.module API works. This patch makes it really simple to add custom sorts without breaking BC. Therefore, I'd like this patch to be considered for 1.x.
Explanation
While it is possible to interfere with hooks at various places, it is not possible to simply add a sort. I tried various ways and all failed (without this patch): You could add a link to the sort block with hook_apachesolr_sort_links_alter() but then the Solr_Base_Query class wouldn't know about it. You can add your sort to the available sorts in Solr_Base_Query in hook_apachesolr_modify_query() but then it won't appear in the search block as the query object gets statically cached before hook_apachesolr_modify_query() runs. Even if you do both, it won't work because the $sortstring variable from $_GET is parsed in the constructor of Solr_Base_Query before any hooks can modify the query and the sort is only applied if the sort is part of the available_sorts which are impossible to extend via hooks at this time. So with the current API, there are only two ways to add a sort and both are ugly: You can extend the Solr_Base_Query which sucks because this way only one module at a time could add a sort. Or you could manually get your sort string from $_GET and then set the sort in hook_apachesolr_modifiy_query() which would works but also duplicates a lot of code.
Solution
So this very simple patch a) makes parse_sortstring() public and b) adds an alter hook that allows modules to alter the Solr_Base_Query objecte before it gets statically cached and c) runs parse_sortstring after that hook has run.
This patch is completely backward-compatible and should not break any existing implementations in any way as it only adds a hook.
With this patch applied, it is really simple to add a sort, e.g. to be able to sort on nid you'd just have to do
<?php
function mymodule_apachesolr_current_query_alter(&$query) {
$query->set_available_sort('nid', array(
'title' => t('Nid'),
'default' => 'asc',
));
}
?>and that's it.
It would be great if this patch could still get into RC3 as it really simplifies the process a lot without breaking BC.
This patch was sponsored by XARXA Media GmbH.
| Attachment | Size | Status | Test result | Operations |
|---|---|---|---|---|
| apachesolr_alter_query.patch | 1.79 KB | Ignored: Check issue status. | None | None |
Comments
#1
looks like a good start, but probably $query->set_available_sort() shoudl trigger internally re-parsing the sort string.
#2
This patch is based on a suggestion by DamZ and now just calls parse_sortstring() whenever a new sort is added or removed via set_available_sort(). The example in the OP on how to add a custom sort still applies.
#3
Removed the argument to parse_sortstring() alltogether.
#4
I agree, this is a very simple approach.
Something that implements a UI for picking-up custom sorts (only the idea, not the patch yet): #420490: Custom sortings
#5
Even better patch attached after more feedback from DamZ: We now reparse the sortstring simply whenever new sorts are added to the base query object. Also added documentation for the hook (now called hook_apachesolr_prepare_query) in README.txt.
#6
looking pretty good.
note for the README:
function my_module_apachesolr_prepare_query(&$query) {this does not actually need the &, since we require PHP 5 and all objects are passed by reference. Maybe it's ok to leave it to emphasize this fact. Also, i would be useful to include the other params in the example.
#7
Thanks for this - after applying the patch from #5, I implemented the new
hook_apachesolr_prepare_query()as follows:<?phpfunction mymodule_apachesolr_prepare_query(&$query) {
$query->set_available_sort('dm_cck_field_date', array(
'title' => t('Date'),
'default' => 'asc',
));
}
?>
And this allowed me to force my search results to be sorted by a custom CCK date field (previously, setting
$solrsortto'dm_cck_field_date asc'had no effect).Great job - let's get this committed! (I'll wait for more experienced solr devs to set RTBC, though).
#8
committed to 6.x-1.x
#9
Tested & ported to 5.x-2.x in #274214 and #274218 together with other small fixes.
#10
Now in DRUPAL-6--2.
#11
Automatically closed -- issue fixed for 2 weeks with no activity.