Drupal 6: Getting Apache Solr to show all results and facets without searching
I’ve encountered this issue a few times recently when working with Apache Solr (in Drupal 6): clients want to to show all search results and allow the users to browse facets, instead of requiring a search phrase or filter to begin the search. The current version of the Apache Solr module appears to require at least one of these items.
- Querying Solr directly allows you to return all search results using the query/select URL. Example: http://example.com:8080/solr/select
- Entering a keyword works. Example: http://example.com/search/apachesolr_search/blah
- Entering a keyword AND taxonomy filter works. Example: http://example.com/search/apachesolr_search/blah?filters=tid%3A22
- Entering a taxonomy filter WITHOUT a keyword works. Example: http://example.com/search/apachesolr_search/?filters=tid%3A22
- No keyword AND no filter returns nothing (dang). Example: http://example.com/search/apachesolr_search
A quick look at apachesolr_search.module, in the function: apachesolr_search_view() shows this is not going to happen:
<?php
$keys = trim(search_get_keys());
// ...snip...
$filters = trim($_GET['filters']);
// ...snip...
if ($keys || $filters) {
// do search
}
?>
So what if there was a way to modify the $_GET values prior to the apache solr search page callback? hook_boot maybe? NOTE: hook_boot functions are only processed if the bootstrap column in the system table for your module is set to 1! Example SQL to update..
update {system}
set bootstrap = 1
where type = 'module' and name = 'MYMODULE'
I then created this hook_boot function in my module. It is run prior to the the apache solr search page callback, and allows me to tamper with $_GET variables. In this case, I just add a fake filter to allow the search to pass the above conditional statements.
<?php
function MYMODULE_boot() {
// check for search page
$search_path = 'search/apachesolr_search';
if (substr($_GET['q'], 0, strlen($search_path))==$search_path) {
// get search phrase
$search_phrase = substr($_GET['q'], strlen($search_path)+1);
// get filters
$filters = $_GET['filters'];
// check if filters AND search phrase do not exist
if (empty($filters) && empty($search_phrase)) {
// set fake filter
$_GET['filters'] = 1;
}
}
}
?>
Now going to the solr search page defaults to show all results and facets:
It appears to work, but anyone know of a better way to accomplish this?! :)