function apachesolr_modify_query in Apache Solr Search 6
Same name and namespace in other branches
- 5.2 apachesolr.module \apachesolr_modify_query()
 - 6.2 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);
}
2 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.
 
File
- ./
apachesolr.module, line 1255  - 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())) {
    $params['fq'] = $fq;
  }
  // 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 asc') {
      $params['sort'] = $sortstring;
    }
  }
}