You are here

function hook_apachesolr_query_alter in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 apachesolr.api.php \hook_apachesolr_query_alter()
  2. 6.3 apachesolr.api.php \hook_apachesolr_query_alter()

Alter the query after it's prepared and cached.

Any module performing a search should call drupal_alter('apachesolr_query', $query). That function then invokes this hook. It allows modules to modify the query object and its parameters.

A module implementing HOOK_apachesolr_query_alter() may set $query->abort_search to TRUE to flag the query to be aborted.

  • This report displays the active solr index fields and can help you create Solr filters based on the data currently in your system

Parameters

DrupalSolrQueryInterface $query: An object implementing DrupalSolrQueryInterface. No need for &.

See also

/admin/reports/apachesolr

1 function implements hook_apachesolr_query_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

apachesolr_access_apachesolr_query_alter in apachesolr_access/apachesolr_access.module
Implements hook_apachesolr_query_alter().
2 invocations of hook_apachesolr_query_alter()
apachesolr_do_query in ./apachesolr.module
Execute a keyword search based on a query object.
apachesolr_search_mlt_suggestions in ./apachesolr_search.module
Performs a moreLikeThis query using the settings and retrieves documents.

File

./apachesolr.api.php, line 195
Exposed Hooks in 7.x:

Code

function hook_apachesolr_query_alter(DrupalSolrQueryInterface $query) {

  // I only want to see articles by the admin.
  //
  // NOTE: this "is_uid" filter does NOT refer to the English word "is"
  // It is a combination of flags representing Integer-Single, which is
  // abbreviated with the letters i and s.
  //
  // @see the <dynamicField> definitions in schema.xml or schema-solr3.xml
  $query
    ->addFilter("is_uid", 1);

  // Only search titles.
  $query
    ->replaceParam('qf', 'label');

  // Restrict results to a single content type (use machine name).
  $query
    ->addFilter('bundle', 'my_content_type');

  // Exclude results by setting the third argument of addFilter to TRUE.
  // This filter will return all content types EXCEPT my_content_type nodes.
  $query
    ->addFilter('bundle', 'my_content_type', TRUE);

  // Restrict results to several content types (use machine names).
  // You could also solve this using the SolrFilterSubQuery object and append it
  // to the original query.
  $content_types = array(
    'content_type_1',
    'content_type_2',
  );
  $query
    ->addFilter('bundle', '(' . implode(' OR ', $content_types) . ')');
}