You are here

function hook_search_api_solr_query_alter in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 search_api_solr.api.php \hook_search_api_solr_query_alter()
  2. 8 search_api_solr.api.php \hook_search_api_solr_query_alter()
  3. 7 search_api_solr.api.php \hook_search_api_solr_query_alter()
  4. 4.x search_api_solr.api.php \hook_search_api_solr_query_alter()

Lets modules alter the Solarium select query before executing it.

After this hook, the select query will be finally converted into an expression that will be processed by the lucene query parser. Therefore you can't modify the 'q' parameter here, because it gets overwritten by that conversion. If you need to modify the 'q' parameter you should implement an event listener instead of this hook that handles the solarium events (our connector injects the drupal event handler into solarium) or implement hook_search_api_solr_converted_query() instead. If you want to force a different parser like edismax you must set the 'defType' parameter accordingly.

To get a list of solrium events,

Parameters

\Solarium\Core\Query\QueryInterface $solarium_query: The Solarium query object, as generated from the Search API query.

\Drupal\search_api\Query\QueryInterface $query: The Search API query object representing the executed search query.

See also

http://solarium.readthedocs.io/en/stable/customizing-solarium/#plugin-sy...

1 invocation of hook_search_api_solr_query_alter()
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

./search_api_solr.api.php, line 34
Hooks provided by the Search API Solr search module.

Code

function hook_search_api_solr_query_alter(\Solarium\Core\Query\QueryInterface $solarium_query, \Drupal\search_api\Query\QueryInterface $query) {
  if ($query
    ->getOption('my_custom_boost')) {

    // If the Search API query has a 'my_custom_boost' option, use the edsimax
    // query handler and add some boost queries.

    /** @var array $solr_field_names
             maps search_api field names to real field names in the Solr index
        */
    $solr_field_names = $query
      ->getIndex()
      ->getServerInstance()
      ->getBackend()
      ->getSolrFieldNames($query
      ->getIndex());

    /** @var \Solarium\Component\EdisMax $edismax */
    $edismax = $solarium_query
      ->getEDisMax();
    $keys = $query
      ->getKeys();
    if (is_array($keys)) {
      $keys = implode(' ', $keys);
    }
    if ($keys) {
      $boost_queries['title_exact_phrase'] = [
        'query' => $solr_field_names['title'] . ':' . $solarium_query
          ->getHelper()
          ->escapePhrase($keys) . '^11.0',
      ];
      $edismax
        ->addBoostQueries($boost_queries);
    }

    // Boost documents by date.
    // @see https://www.drupal.org/project/search_api_solr/issues/2855329
    $boost_functions = 'recip(abs(ms(NOW/HOUR,' . $solr_field_names['modified'] . ')),3.16e-11,1,.4)^3';
    $edismax
      ->setBoostFunctions($boost_functions);

    // Avoid the conversion into a lucene parser expression, keep edismax.
    $solarium_query
      ->addParam('defType', 'edismax');
  }
}