In the Solr backend, it is rather easy to override details of the search process, by using either the hook_search_api_solr_query_alter() and hook_search_api_solr_search_results_alter() hooks or subclassing the service class and overriding the preQuery() and postQuery() methods. The advantage of these hooks/methods compared to Search API's normal query/results alter hooks is that they allow access to the internal, Solr-specific information while altering, thus adding a lot of additional capabilities.
While the possibilities are probably much more limited in the Database Search backend, I still think it's a good idea to introduce these here, too. (Only the results alter hook doesn't really make sense, since we can't really add any additional information. However, the postQuery() method still makes sense because otherwise service class-scoped overriding wouldn't be easily possible.)
Do you think this makes sense? Please comment!
Estimated Value and Story Points
This issue was identified as a Beta Blocker for Drupal 8. We sat down and figured out the value proposition and amount of work (story points) for this issue.
Value and Story points are in the scale of fibonacci. Our minimum is 1, our maximum is 21. The higher, the more value or work a certain issue has.
Value : 1
Story Points: 2
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | 2237829-17--query_alter_hooks_and_methods.patch | 11.41 KB | drunken monkey |
Comments
Comment #1
drunken monkeyThis patch would add that to the service class.
Additional argument in favor: Without this patch, this module doesn't define a single hook – and that's just un-Drupal-y! ;)
Comment #2
dmaciej commented+1
but I think it should be fine to add a hook after adding orderBy() statements, just before executing the query.
Comment #3
dmaciej commentedComment #4
dmaciej commentedYou should provide some hooks on the getFacets() method, to be able to alter the SQL queries for each facet too.
Comment #5
drunken monkeyThanks a lot for your feedback!
Altering the final query, after sorts, can already easily be accomplished with the use of
hook_query_TAG_alter()(specifically,hook_query_search_api_db_search_alter()).Additionally, we could move adding the sorts into its own method, so it can more easily be overridden by subclasses.
I think the same route makes sense for the facets (and autocomplete) queries: we should just set appropriate tags and metadata there, too, so people have all the information they need for altering those.
Patch attached, please review!
Comment #7
drunken monkey5: 2237829-5--query_alter_hooks_and_methods.patch queued for re-testing.
(Sorry, had a few un-pushed small fixes lying around locally.)
Comment #8
dmaciej commentedAltering facets using a query alter hook is really difficult and inefficient, because of the conditions structure. It should be possible to alter facets on the "flat" level, before it's added to the query.
Comment #9
drunken monkeyDon't really know what you mean with that? You can already alter the facets options that are passed in the query – where else would you want to alter facets?
Committed #5.
Comment #12
drunken monkeyNeeds to be ported to D8.
Comment #13
drunken monkeyComment #14
nick_vhComment #15
drunken monkeyPretty straight-forward port. Just removed the
postQuery()method since it's pretty pointless – you can just overridesearch()itself.Comment #16
AsadKamil commentedHi All,
It seems there is some whitespace error while applying patch to 8x branch.
Comment #17
drunken monkeyThanks a lot for catching that!
Fixed in the attached revision of the patch.
Comment #18
borisson_Comment #20
drunken monkeyExcellent, thanks for reviewing!
Committed.
Thanks again, everyone!