You are here

function apachesolr_do_query in Apache Solr Search 6

Same name and namespace in other branches
  1. 8 apachesolr.module \apachesolr_do_query()
  2. 5.2 apachesolr.module \apachesolr_do_query()
  3. 6.3 apachesolr.module \apachesolr_do_query()
  4. 6.2 apachesolr.module \apachesolr_do_query()
  5. 7 apachesolr.module \apachesolr_do_query()

Execute a search based on a query object.

Normally this function is used with the default (dismax) handler for keyword searches. The $final_query that's returned will have been modified by both hook_apachesolr_prepare_query() and hook_apachesolr_modify_query().

Parameters

$caller: String, name of the calling module or function for use as a cache namespace.

$current_query: A query object from apachesolr_drupal_query(). It will be modified by hook_apachesolr_prepare_query() and then cached in apachesolr_current_query().

$params: Array of parameters to pass to Solr. Must include at least 'rows'.

$page: For paging into results, using $params['rows'] results per page.

Return value

array($final_query, $response)

Throws

Exception

1 call to apachesolr_do_query()
apachesolr_search_execute in ./apachesolr_search.module
Execute a search results based on keyword, filter, and sort strings.

File

./apachesolr.module, line 1355
Integration with the Apache Solr search application.

Code

function apachesolr_do_query($caller, $current_query, &$params = array(
  'rows' => 10,
), $page = 0) {

  // Allow modules to alter the query prior to statically caching it.
  // This can e.g. be used to add available sorts.
  foreach (module_implements('apachesolr_prepare_query') as $module) {
    $function_name = $module . '_apachesolr_prepare_query';
    $function_name($current_query, $params, $caller);
  }

  // Cache the original query. Since all the built queries go through
  // this process, all the hook_invocations will happen later
  $query = apachesolr_current_query($current_query, $caller);

  // This hook allows modules to modify the query and params objects.
  apachesolr_modify_query($query, $params, $caller);
  $params['start'] = $page * $params['rows'];
  if (!$query) {
    return array(
      NULL,
      array(),
    );
  }

  // Final chance for the caller to modify the query and params. The signature
  // is: CALLER_finalize_query(&$query, &$params);
  $function = $caller . '_finalize_query';
  if (function_exists($function)) {
    $function($query, $params);
  }
  $keys = $query
    ->get_query_basic();
  if ($keys == '' && isset($params['fq'])) {

    // Move the fq params to q.alt for better performance.
    $qalt = array();
    foreach ($params['fq'] as $delta => $value) {

      // Move the fq param if it has no local params and is not negative.
      if (!preg_match('/^(?:\\{!|-)/', $value)) {
        $qalt[] = '(' . $value . ')';
        unset($params['fq'][$delta]);
      }
    }
    if ($qalt) {
      $params['q.alt'] = implode(' ', $qalt);
    }
  }

  // This is the object that does the communication with the solr server.
  $solr = apachesolr_get_solr();

  // We must run htmlspecialchars() here since converted entities are in the index
  // and thus bare entities &, > or < won't match. Single quotes are converted
  // too, but not double quotes since the dismax parser looks at them for
  // phrase queries.
  $keys = htmlspecialchars($keys, ENT_NOQUOTES, 'UTF-8');
  $keys = str_replace("'", '&#039;', $keys);
  $response = $solr
    ->search($keys, $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, $caller);
  return array(
    $query,
    $response,
  );
}