You are here

function apachesolr_modify_query in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 6 apachesolr.module \apachesolr_modify_query()
  2. 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) { // 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 1125
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;
  }
  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;
    }
  }
}