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!
Comments
Slava (not verified)
Tue, 08/30/2011 - 16:58
Permalink
Hi, thanks for the article.
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.
admin
Thu, 09/01/2011 - 11:50
Permalink
EOFM
Hi Slava,
It's taken from solr search module. I think you better leave it .
Slava (not verified)
Thu, 09/01/2011 - 16:53
Permalink
Hi, ok.
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.
admin
Sat, 09/03/2011 - 01:30
Permalink
Thanks
Thanks Slava!
I recently update this articles.
Greetings
Chris (not verified)
Thu, 12/15/2011 - 13:20
Permalink
Should that be$params = array
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!
Oleksii (not verified)
Wed, 05/09/2012 - 23:53
Permalink
EOFM here is just HEREDOC
EOFM here is just HEREDOC http://www.php.net/manual/en/language.types.string.php#language.types.st...
apt2a (not verified)
Thu, 09/08/2011 - 21:27
Permalink
What Contrib module did you use?
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?
admin
Fri, 09/09/2011 - 01:46
Permalink
Yes
Yes, it should be okay. Try to activate or check your code.
Vadim (not verified)
Thu, 09/22/2011 - 04:49
Permalink
customsearch_customsolr_search function question
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
admin
Tue, 09/27/2011 - 15:55
Permalink
neat!
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!
Chris (not verified)
Thu, 12/15/2011 - 13:43
Permalink
If you're new to Solr, you'd
If you're new to Solr, you'd define this like so to search only for content type MyContentType:
$conditions['filters'] = 'bundle:MyContentType';Oleksii (not verified)
Wed, 05/09/2012 - 23:51
Permalink
Thanks for this post. This
Thanks for this post. This helped me writing my custom apachesolr search page a lot!
Add new comment