Creating custom Apache SOLR search modules in Drupal 7 programmatically

We can create custom modules which using apachesolr search modules in Drupal 7 programmatically. This is important if we want making custom search pages in Drupal 7 without /search/site as ApacheSOLR modules give. To create custom apache solr search, we need to know how the logic of the new apachesolr. There are a lot difference between apachesolr on Drupal 6 compared with Drupal 7. Don't worry, this difference make us easy. Let make it !

I will create custom search page with apache solr which it can be accessed from mysite.com/server/<keyword>.
This module named as customsearch and need apachesolr module already installed.

1. Creating hook menu for customsearch
This function will controlling site path for custom searching using apachesolr Drupal 7.

<?php
/**
* Implementation of hook_menu().
*/
function customsearch_menu() {
   
$items = array();
   
$items['server/%'] = array(
           
'title' => TRUE,
           
'access callback' => TRUE,
           
'page callback' => 'customsearch_customsolr_search',
           
'access arguments' => array('search content'),
           
'type' => MENU_CALLBACK,
        );
   
    return
$items;
}
?>

2. Creating function for grabbing keyword from mysite.com/server/keyword by reading it path

<?php
function customsearch_get_keys() {
  static
$return;
  if (!isset(
$return)) {
   
$parts = explode('/', $_GET['q']);
    if (
count($parts) == 2) {
     
$return = array_pop($parts);
    }
    else {
     
$return = empty($_REQUEST['keys']) ? '' : $_REQUEST['keys'];
    }
  }
  return
$return;
}
?>

3. Creating function callback for custom search apachesolr drupal7 programmatically

UPDATED! thanks for slava!

<?php
function customsearch_customsolr_search($keys = null) {
   
$keys = customsearch_get_keys();
   
$filters = isset($conditions['filters']) ? $conditions['filters'] : '';
   
$solrsort = isset($_GET['solrsort']) ? $_GET['solrsort'] : '';
   
$empty_search_behavior = isset($conditions['apachesolr_search_browse']) ? $conditions['apachesolr_search_browse'] : '';
   
$nodeTitle = check_plain($keys);
   
drupal_set_title(ucfirst($nodeTitle));

    try {
       

//$results = apachesolr_search_search_execute($keys, $filters, $solrsort, $custom_path, $page);
        // For perfomance reasons, do not run a search if search is empty and
        // $empty_search_behavior condition is 'browse'
       
if (!$keys && !$filters && $empty_search_behavior == 'browse') {
           
// Pass empty search behavior as string on to customsearch_theme()
           
return array('apachesolr_search_browse' => $empty_search_behavior);
        }

       

// Update from Slava's comments
       
$params = array('q'=> keys, 'fq' => filters);
       
$results = apachesolr_search_run('search', $params, $solrsort, 'weightloss/', pager_find_page());

       

//$results = apachesolr_search_run($keys, $filters, $solrsort, 'weightloss/', pager_find_page());
       
       
if (!$keys && !$filters && $empty_search_behavior == 'blocks') {
           
// If search was empty and empty search behavior is 'blocks', discard
            // the result items and pass behavior on to customsearch_theme()
           
$results['apachesolr_search_browse'] = $empty_search_behavior;
        }

        return

customsearch_search_page($results);
    }
    catch (
Exception $e) {
       
watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
    }
}
?>

4. Now, theming your custom search page in customsearch modules

<?php
/**
* Implements hook_theme().
*/
function customsearch_theme() {
  return array(
   
'search_results__customsearch' => array(
     
'function' => 'theme_customsearch_results',
    ));
}

/**
* Implements hook_search_page().
*/
function customsearch_search_page($results) {
  if (empty(
$results['apachesolr_search_browse'])) {
   
$output = array(
     
'#theme' => 'search_results',
     
'#results' => $results,
     
'#module' => 'customsearch',
    );
  } else {
   
$output = apachesolr_search_page_browse($results['apachesolr_search_browse']);
  }
  return
$output;
}

function

theme_customsearch_results($variables) {
 
extract($variables);
 
$output = '';
 
$query = customsearch_get_keys();
  if (
$search_results) {
   
$output .= '<h2>Custom search for '.$query.'</h2>';
   
$output .=<<<EOFM
<ol class="search-results {$module}-results">
 
{$search_results}
</ol>
{$pager}
EOFM;
  } else {
   
$output .= "<h2>" . t('Your search yielded no results') . "</h2>";
   
$output .= apachesolr_search_help('apachesolr_search#noresults', drupal_help_arg());
  }

  return

$output;
}
?>

Now, you have custom apachesolr page in Drupal 7.

Cheers!



You should follow me here

Comments

Hi, thanks for the article. Could you please tell me, what is the EOFM and what is it needed for?
$output .=<<<EOFM<ol class="search-results {$module}-results">  {$search_results}</ol>{$pager}EOFM;

Thanks in advance.

Hi Slava,

It's taken from solr search module. I think you better leave it .

Hi, ok.
btw, you have an error in $results = apachesolr_search_run($keys, $filters, $solrsort, 'weightloss/', pager_find_page());

parameters are wrong, you should pass keys & filters as $params array, it should be like this:
$params = array('q'=> keys, 'fq' => filters);
$results = apachesolr_search_run('search', $params, $solrsort, 'weightloss/', pager_find_page());

Anyway, thanks a lot for the article, it gives me clear vision on search customization for D7.

Thanks Slava!

I recently update this articles.

Greetings

Should that be
$params = array('q'=> $keys, 'fq' => $filters);?

I got a search result page for the word "keys" otherwise.

Awesome article OP, thanks very much!

Hello.

I tried to run your code but the function apachesolr_search_run() is not defined.

Which contrib module did you run?

Im using apachesolr-7.x-1.0-beta8. Is this ok?

Yes, it should be okay. Try to activate or check your code.

Hi, thanks for article it's really helpful.

There is a question related to function customsearch_customsolr_search($keys = null)
where does the $conditions array come from?

Thanks

Wow, that's right!

You're right, $conditions array not define in params function.
Actually, i grab it from apachesolr_module with some little changes.

If you need some conditions, you can define it.
Deleting $conditions also have no effects here unless you need to set filtering, etc.

Thanks for good question!

If you're new to Solr, you'd define this like so to search only for content type MyContentType:

$conditions['filters'] = 'bundle:MyContentType';

Thanks for this post. This helped me writing my custom apachesolr search page a lot!

Add new comment