/**
 * Preprocesses a search's database query before it is executed.
 *
 * @param SelectQueryInterface $db_query
 *   The database query to be executed for the search. Will have "item_id" and
 *   "score" columns in its result.
 * @param SearchApiQueryInterface $query
 *   The search query that is being executed.
 *
 * @see SearchApiDbService::preQuery()
 */
function hook_search_api_db_query_alter(SelectQueryInterface &$db_query, SearchApiQueryInterface $query)
{
    // If the option was set on the query, add additional SQL conditions.
    if ($custom = $query->getOption('custom_sql_conditions')) {
        foreach ($custom as $condition) {
            $db_query->condition($condition['field'], $condition['value'], $condition['operator']);
        }
    }
}
/**
 * Implements hook_search_api_query_alter().
 *
 * This example hook implementation shows how a custom module could fix the
 * problem with Views contextual filters in a specific context.
 */
function example_search_api_query_alter(SearchApiQueryInterface $query)
{
    // Check whether this is an appropriate automcomplete query.
    if ($query->getOption('search id') === 'search_api_autocomplete:example') {
        // If it is, add the necessary filters that would otherwise be added by
        // contextual filters. This is easy if the argument comes from the global
        // user or a similar global source. If the argument comes from the URL or
        // some other page-specific source, however, you would need to somehow pass
        // that information along to this function.
        global $user;
        $query->condition('group', $user->data['group']);
    }
}
/**
 * Alter the search results before they are returned.
 *
 * @param array $results
 *   The results returned by the server, which may be altered. The data
 *   structure is the same as returned by SearchApiQueryInterface::execute().
 * @param SearchApiQueryInterface $query
 *   The search query that was executed.
 */
function hook_search_api_results_alter(array &$results, SearchApiQueryInterface $query)
{
    if ($query->getOption('search id') == 'search_api_views:my_search_view:page') {
        // Log the number of results.
        $vars = array('@keys' => $query->getOriginalKeys(), '@num' => $results['result count']);
        watchdog('my_module', 'Search view with query "@keys" had @num results.', $vars, WATCHDOG_DEBUG);
    }
}
/**
 * Lets modules alter a Solr search request before sending it.
 *
 * Apache_Solr_Service::search() is called afterwards with these parameters.
 * Please see this method for details on what should be altered where and what
 * is set afterwards.
 *
 * @param array $call_args
 *   An associative array containing all four arguments to the
 *   Apache_Solr_Service::search() call ("query", "offset", "limit" and
 *   "params") as references.
 * @param SearchApiQueryInterface $query
 *   The SearchApiQueryInterface object representing the executed search query.
 */
function hook_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface $query)
{
    if ($query->getOption('foobar')) {
        $call_args['params']['foo'] = 'bar';
    }
}