You are here

protected function SearchApiSolrBackend::setFields in Search API Solr 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setFields()
  2. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setFields()

Set the list of fields Solr should return as result.

Parameters

\Solarium\QueryType\Select\Query\Query $solarium_query: The solr query.

array $fields_to_be_retrieved: The field values to be retrieved from Solr.

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

bool $highlight:

Throws

\Drupal\search_api\SearchApiException

1 call to SearchApiSolrBackend::setFields()
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

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 1743

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setFields(Query $solarium_query, array $fields_to_be_retrieved, QueryInterface $query, $highlight = TRUE) {
  $required_fields = $this
    ->getRequiredFields($query);
  $returned_fields = [];
  $highlight_fields = [
    '*',
  ];
  if (!empty($this->configuration['retrieve_data'])) {
    $field_names = $this
      ->getSolrFieldNamesKeyedByLanguage($query
      ->getLanguages(), $query
      ->getIndex());

    // If Search API provides information about the fields to retrieve, limit
    // the fields accordingly. ...
    foreach ($fields_to_be_retrieved as $field_name) {
      if (isset($field_names[$field_name])) {
        $returned_fields[] = array_values($field_names[$field_name]);
      }
    }
    if ($returned_fields) {
      $highlight_fields = array_unique(array_merge(...$returned_fields));
      $returned_fields = array_unique(array_merge($highlight_fields, $required_fields));
    }
    else {
      $returned_fields = [
        '*',
        reset($field_names['search_api_relevance']),
      ];
    }
  }
  else {
    $returned_fields = $required_fields;
  }
  $solarium_query
    ->setFields(array_unique($returned_fields));
  if ($highlight) {
    try {
      $highlight_config = $query
        ->getIndex()
        ->getProcessor('highlight')
        ->getConfiguration();
      if ($highlight_config['highlight'] !== 'never') {
        $this
          ->setHighlighting($solarium_query, $query, $highlight_fields);
      }
    } catch (SearchApiException $exception) {

      // Highlighting processor is not enabled for this index. Just use the
      // the index configuration.
      $this
        ->setHighlighting($solarium_query, $query, $highlight_fields);
    }
  }
}