You are here

protected function SearchApiSolrAnySchemaBackend::preQuery in Search API Solr 8.2

Allow custom changes before sending a search query to Solr.

This allows subclasses to apply custom changes before the query is sent to Solr. Works exactly like hook_search_api_solr_query_alter().

Parameters

\Solarium\Core\Query\QueryInterface $solarium_query: The Solarium query object.

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

Overrides SearchApiSolrBackend::preQuery

File

src/Plugin/search_api/backend/SearchApiSolrAnySchemaBackend.php, line 47

Class

SearchApiSolrAnySchemaBackend
A read-only backend for any non-drupal schema.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function preQuery(SolariumQueryInterface $solarium_query, QueryInterface $query) {
  parent::preQuery($solarium_query, $query);

  // Do not alter the query if the index does not use the solr_document
  // datasource.
  $index = $query
    ->getIndex();
  if (!$index
    ->isValidDatasource('solr_document')) {
    return;
  }

  // Remove the filter queries that limit the results based on site and index.
  $solarium_query
    ->removeFilterQuery('index_filter');

  // Set requestHandler for the query type.
  $config = $index
    ->getDatasource('solr_document')
    ->getConfiguration();
  if (!empty($config['request_handler'])) {
    $solarium_query
      ->addParam('qt', $config['request_handler']);
  }

  // Set the default query, if necessary and configured.
  if (!$solarium_query
    ->getQuery() && !empty($config['default_query'])) {
    $solarium_query
      ->setQuery($config['default_query']);
  }
  $backend = $index
    ->getServerInstance()
    ->getBackend();
  if ($backend instanceof SearchApiSolrBackend) {
    $solr_config = $backend
      ->getConfiguration();

    // @todo Should we maybe not even check that setting and use this to
    //   auto-enable fields retrieval from Solr?
    if (!empty($solr_config['retrieve_data'])) {
      $fields_list = [];
      foreach ($backend
        ->getSolrFieldNames($index) as $solr_field_name) {
        $fields_list[] = $solr_field_name;
      }
      $extra_fields = [
        'language_field',
        'label_field',
        'url_field',
      ];
      foreach ($extra_fields as $config_key) {
        if (!empty($config[$config_key])) {
          $fields_list[] = $config[$config_key];
        }
      }
      $solarium_query
        ->setFields(array_unique($fields_list));
    }
  }
}