Getting Apache Solr to show all results and facets without searching

Avatar-eric-london
Created by Eric.London on 2011-07-07
Tags:
New Comment
 
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:

Solr All Results

It appears to work, but anyone know of a better way to accomplish this?! :)

Comments

 
  • You could do this with
    Created by Anonymous on 2011-07-08
    You could do this with hook_menu and something akin to the following:

    
    apachesolr_search_execute(NULL, NULL, NULL, 'search/apachesolr_search', $page=0);
    


    See http://drupalconnect.com/blog/cmjns/replacing-drupal-static-navigation-apache-solr-facets

    Could be useful for you.

    @webkenny
    • thank Kenny
      Created by Eric.London on 2011-07-09
      Hi Kenny,

      Good tip! I thought about doing that.. but I'd probably have to do another hook_menu_alter() to overwrite the page callback for the main solr search page to be able to execute my own code.

      We are using apachesolr_search_execute() on the same project to send search data in SMS, works great :)

      Cheers,
      -E