function apachesolr_multisitesearch_search in Apache Solr Search 5
Implementation of hook_search().
File
- contrib/
apachesolr_multisitesearch/ apachesolr_multisitesearch.module, line 5
Code
function apachesolr_multisitesearch_search($op = 'search', $keys = NULL) {
switch ($op) {
case 'name':
return t('Multisite');
case 'reset':
ApacheSolrUpdate::reset('apachesolr');
return;
case 'status':
$change = ApacheSolrUpdate::get_change('apachesolr');
$last = ApacheSolrUpdate::get_last('apachesolr');
$total = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE status = 1'));
$remaining = db_result(db_query('SELECT COUNT(*) FROM {node} n ' . 'LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid ' . 'WHERE n.status = 1 AND ((GREATEST(n.created, n.changed, c.last_comment_timestamp) = %d AND n.nid > %d ) OR (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d))', $change, $last, $change, $change, $change));
return array(
'remaining' => $remaining,
'total' => $total,
);
case 'search':
global $pager_total;
// This is the object that does the communication with the solr server.
$solr =& apachesolr_get_solr(variable_get('apachesolr_host', 'localhost'), variable_get('apachesolr_port', 8983), variable_get('apachesolr_path', '/solr'));
// This is the object that knows about the query coming from the user.
$query =& apachesolr_drupal_query($keys);
$results = array();
try {
$params = array(
//'qt' => 'standard',
'fl' => '*,score',
'rows' => variable_get('apachesolr_rows', 10),
'facet' => 'true',
'facet.mincount' => 1,
'facet.sort' => 'true',
);
// We have to add the site explicitly because it is needed in conjunction
// with the hash when doing multisite faceting.
$params['facet.field'][] = 'site';
// TODO: This adds all of the possible facets to the query. Not all
// of these facets have their blocks enabled, so the list should be
// filtered by the actual enabled blocks, otherwise we're putting
// unneeded strain on the Solr server.
foreach (module_implements('apachesolr_facets') as $module) {
$function = $module . '_apachesolr_facets';
$result = call_user_func_array($function, array());
if (isset($result) && is_array($result)) {
foreach ($result as $facet) {
$params['facet.field'][] = $facet;
}
}
}
// Facet limits
$facet_query_limits = variable_get('apachesolr_facet_query_limits', array());
foreach ($facet_query_limits as $fieldname => $limit) {
$params['f.' . $fieldname . '.facet.limit'] = $limit;
}
if (isset($_GET['solrsort'])) {
$sort = check_plain($_GET['solrsort']);
}
// Validate sort parameter
if (isset($sort) && preg_match('/^([a-z0-9_]+ (asc|desc)(,)?)+$/i', $sort)) {
$params['sort'] = $sort;
}
if ($fields = apachesolr_cck_fields()) {
foreach ($fields as $name => $field) {
$index_key = apachesolr_index_key($field);
$params['facet.field'][] = $index_key;
}
}
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$params['start'] = $page * $params['rows'];
/**
* This hook allows modules to modify the query are params objects.
*
* Example:
*
*<code>
* function my_module_apachesolr_modify_query(&$query, &$params) {
* // I only want to see articles by the admin!
* $query->add_field("uid", 1);
*
* }
* </code>
*/
foreach (module_implements('apachesolr_modify_query') as $module) {
$function_name = "{$module}_apachesolr_modify_query";
$function_name($query, $params);
}
if (!$query) {
return array();
}
$response = $solr
->search($query
->get_query(), $params['start'], $params['rows'], $params);
// The response is cached so that it is accessible to the blocks and anything
// else that needs it beyond the initial search.
apachesolr_static_response_cache($response);
apachesolr_has_searched(TRUE);
$total = $response->response->numFound;
pager_query("SELECT %d", $params['rows'], 0, NULL, $total);
if ($total > 0) {
$extra = array();
foreach ($response->response->docs as $doc) {
$extra += node_invoke_nodeapi($doc, 'search result');
$extra['score'] = $doc->score;
$snippet = search_excerpt($keys, $doc->body);
if (trim($snippet) == '...') {
$snippet = '';
}
$results[] = array(
'link' => $doc->url,
// TODO: This will break in multisite - the type name has to be saved to the index.
'type' => $doc->type,
'title' => $doc->title,
'user' => $doc->name,
'date' => $doc->changed,
'node' => $doc,
'extra' => $extra,
'score' => $doc->score,
'snippet' => $snippet,
);
}
// Hook to allow modifications of the retrieved results
foreach (module_implements('apachesolr_process_results') as $module) {
$function = $module . '_apachesolr_process_results';
call_user_func_array($function, array(
&$results,
));
}
}
// Set breadcrumb
drupal_set_breadcrumb($query
->get_breadcrumb());
return $results;
} catch (Exception $e) {
watchdog('Apache Solr', $e
->getMessage(), NULL, WATCHDOG_ERROR);
apachesolr_failure(t('Search'), $query
->get_query());
}
break;
}
// switch
}