You are here

function apachesolr_modify_query in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 5.2 apachesolr.module \apachesolr_modify_query()
  2. 6 apachesolr.module \apachesolr_modify_query()

This hook allows modules to modify the query and params objects.

Example:

function my_module_apachesolr_modify_query(&$query, &$params, $caller) { // I only want to see articles by the admin! $query->add_filter("uid", 1);

}

3 calls to apachesolr_modify_query()
apachesolr_do_query in ./apachesolr.module
Execute a search based on a query object.
apachesolr_mlt_suggestions in ./apachesolr.module
Performs a moreLikeThis query using the settings and retrieves documents.
apachesolr_search_browse in ./apachesolr_search.module
Execute an empty search (match all documents) and show a listing of all enabled facets.

File

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

Code

function apachesolr_modify_query(&$query, &$params, $caller) {
  if (empty($query)) {

    // This should only happen if Solr is not set up - avoids fatal errors.
    return;
  }

  // Call the hooks first because otherwise any modifications to the
  // $query object don't end up in the $params.
  foreach (module_implements('apachesolr_modify_query') as $module) {
    $function_name = $module . '_apachesolr_modify_query';
    $function_name($query, $params, $caller);
  }

  // TODO: The query object should hold all the params.
  // Add array of fq parameters.
  if ($query && ($fq = $query
    ->get_fq())) {
    foreach ($fq as $delta => $values) {
      if (is_array($values) || is_object($values)) {
        foreach ($values as $value) {
          $params['fq'][$delta][] = $value;
        }
      }
    }
  }

  // Add sort if present.
  if ($query) {
    $sort = $query
      ->get_solrsort();
    $sortstring = $sort['#name'] . ' ' . $sort['#direction'];

    // We don't bother telling Solr to do its default sort.
    if ($sortstring != 'score desc') {
      $params['sort'] = $sortstring;
    }
  }
}