Apache Solr and Drupal Default's Search


This is my first official Trellon blog post! It's been a good first few weeks. I have had a chance to work on some exciting projects. One of which involved using the awesome ApacheSolr Drupal Module which adds support for much better search performance and faceted search.

Find the ApacheSolr Search

Maybe it's just me, but I had the hardest time finding out how to search via the ApacheSolr module. I could see on the administration pages that the content was all indexed, and I had added the appropriate blocks, but every time I searched the site, it was just the normal results with no facets!

After reading some issues that alluded to this path search/apachesolr_search, it hit me that the ApacheSolr search is a different page (it appears as a separate Search tab on the search page). This is due to how the core Drupal search works and how it gets extended. I did waste a fair amount of time trying to realize this very fundamental task, so I started a ticket asking for more clarity in the documentation.

How To Integrate Your Search Form

One issue that comes up from ApacheSolr being a separate search page is that the search form does not go to it by default. This is the $search_box in the page.tpl.php template. The way we change this behavior is to add a new submission handler to that form. You can create a custom module for this or put it in your custom theme.

* Implementation of hook_form_alter()
function custommodule_form_alter(&$form, $form_state, $form_id) {
// Add submission handler to redirect to apachesolr
if ($form_id == 'search_theme_form') {
// Add a submit, because the search form redirects to a specific place
$form['#submit'][] = 'custommodule_search_submit';

* Implementation of form submit function
function custommodule_search_submit($form, &$form_state) {
// Get form ID
$form_id = $form['form_id']['#value'];
// Create new redirect
$form_state['redirect'] = 'search/apachesolr_search/'. trim($form_state['values'][$form_id]);

If we break this down.

  • The first function is just a simple implementation of hook_form_alter which allows you to hook into forms and change them around (alter them).
  • The contents of that function contain a conditional to look for the specific form. This was found by looking at the form's output (HTML) and looking for the form ID element's value.
  • Then it adds a submission handler (the name of a function). Reference the FAPI Quickstart for more information.
  • The next step is to define the submission handler function we pointed our form to. This is the second function you will see, and it is basically the same thing as the original handler you will find in the search module.
  • The only thing to do in this function is to set a redirect for the form to the new ApachSolr page. Note that the original submission handler of this function will be performed as well, but prior to this one.

And voila! When you search through the Search Box, it will redirect you to the ApacheSolr search. It may be beneficial to point out that this will not change other searching locations, including the default search found at the search page.

This content can also be referenced at this issue: http://drupal.org/node/326375#comment-1643362


Hey Alan, maybe I'm missing

Hey Alan, maybe I'm missing something but you can't use this exactly as shown above in template.php, correct? I'm under the impression that hook_form_alter() can't be used in template.php and must be done via a module. Any insight is appreciated.

- Nick

Correct Nick, hooks such as

Correct Nick, hooks such as hook_form_alter() must be implemented in a custom module.

There's some documentation in this area of drupal.org which might be helpful to you getting started writing a custom module: http://drupal.org/node/416986

Thanks...I already have it in

Thanks...I already have it in a module, I'd just prefer it in template.php since all this module does is use hook_form_alter() to modify an action on the search form. Seems like a waste :)

Just set the enable

Just set the enable ApacheSolr to be default search on the Advanced setting page of the site configuration, Apache Solr.

Make Apache Solr Search the default:
Hides core node search, and makes the search block submit to Apache Solr Search

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options