You are here

protected function SearchApiSolrBackend::extractResults in Search API Solr 8

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

Extract results from a Solr response.

Parameters

\Drupal\search_api\Query\QueryInterface $query: The Search API query object.

\Solarium\Core\Query\Result\ResultInterface $result: A Solarium select response object.

Return value

\Drupal\search_api\Query\ResultSetInterface A result set object.

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function extractResults(QueryInterface $query, ResultInterface $result) {
  $index = $query
    ->getIndex();
  $backend_config = $index
    ->getServerInstance()
    ->getBackendConfig();
  $field_names = $this
    ->getSolrFieldNames($index);
  $fields = $index
    ->getFields(TRUE);
  $site_hash = SearchApiSolrUtility::getSiteHash();

  // We can find the item ID and the score in the special 'search_api_*'
  // properties. Mappings are provided for these properties in
  // SearchApiSolrBackend::getFieldNames().
  $id_field = $field_names['search_api_id'];
  $score_field = $field_names['search_api_relevance'];
  $language_field = $field_names['search_api_language'];

  // Set up the results array.
  $result_set = $query
    ->getResults();
  $result_set
    ->setExtraData('search_api_solr_response', $result
    ->getData());

  // In some rare cases (e.g., MLT query with nonexistent ID) the response
  // will be NULL.
  $is_grouping = $result instanceof Result && $result
    ->getGrouping();
  if (!$result
    ->getResponse() && !$is_grouping) {
    $result_set
      ->setResultCount(0);
    return $result_set;
  }

  // If field collapsing has been enabled for this query, we need to process
  // the results differently.
  $grouping = $query
    ->getOption('search_api_grouping');
  if (!empty($grouping['use_grouping'])) {
    $docs = [];
    $resultCount = 0;
    if ($result_set
      ->hasExtraData('search_api_solr_response')) {
      $response = $result_set
        ->getExtraData('search_api_solr_response');
      foreach ($grouping['fields'] as $field) {
        $solr_field_name = $field_names[$field];
        if (!empty($response['grouped'][$solr_field_name])) {
          $resultCount = count($response['grouped'][$solr_field_name]);
          foreach ($response['grouped'][$solr_field_name]['groups'] as $group) {
            foreach ($group['doclist']['docs'] as $doc) {
              $docs[] = $doc;
            }
          }
        }
      }

      // Set a default number then get the groups number if possible.
      $result_set
        ->setResultCount($resultCount);
      if (count($grouping['fields']) == 1) {
        $field = reset($grouping['fields']);

        // @todo handle languages
        $solr_field_name = $field_names[$field];
        if (isset($response['grouped'][$solr_field_name]['ngroups'])) {
          $result_set
            ->setResultCount($response['grouped'][$solr_field_name]['ngroups']);
        }
      }
    }
  }
  else {
    $result_set
      ->setResultCount($result
      ->getNumFound());
    $docs = $result
      ->getDocuments();
  }

  // Add each search result to the results array.

  /** @var \Solarium\QueryType\Select\Result\Document $doc */
  foreach ($docs as $doc) {
    if (is_array($doc)) {
      $doc_fields = $doc;
    }
    else {

      /** @var \Solarium\QueryType\Select\Result\Document $doc */
      $doc_fields = $doc
        ->getFields();
    }
    if (empty($doc_fields[$id_field])) {
      throw new SearchApiSolrException(sprintf('The result does not contain the essential ID field "%s".', $id_field));
    }
    $item_id = $doc_fields[$id_field];

    // For items coming from a different site, we need to adapt the item ID.
    if (!$this->configuration['site_hash'] && $doc_fields['hash'] != $site_hash) {
      $item_id = $doc_fields['hash'] . '--' . $item_id;
    }
    $result_item = $this->fieldsHelper
      ->createItem($index, $item_id);
    $result_item
      ->setExtraData('search_api_solr_document', $doc);
    $result_item
      ->setLanguage($doc_fields[$language_field]);
    if (isset($doc_fields[$score_field])) {
      $result_item
        ->setScore($doc_fields[$score_field]);
      unset($doc_fields[$score_field]);
    }

    // The language field should not be removed. We keep it in the values as
    // well for backward compatibility and for easy access.
    unset($doc_fields[$id_field]);

    // Extract properties from the Solr document, translating from Solr to
    // Search API property names. This reverses the mapping in
    // SearchApiSolrBackend::getFieldNames().
    foreach ($field_names as $search_api_property => $solr_property) {
      if (isset($doc_fields[$solr_property]) && isset($fields[$search_api_property])) {
        $doc_field = is_array($doc_fields[$solr_property]) ? $doc_fields[$solr_property] : [
          $doc_fields[$solr_property],
        ];
        $field = clone $fields[$search_api_property];
        foreach ($doc_field as &$value) {
          switch ($field
            ->getType()) {
            case 'date':

              // Field type convertions
              // Date fields need some special treatment to become valid date values
              // (i.e., timestamps) again.
              if (preg_match('/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$/', $value)) {
                $value = strtotime($value);
              }
              break;
            case 'text':
              $value = new TextValue($value);
          }
        }
        $field
          ->setValues($doc_field);
        $result_item
          ->setField($search_api_property, $field);
      }
    }
    $solr_id = $this
      ->createId($index
      ->id(), $result_item
      ->getId());
    if ($excerpt = $this
      ->getExcerpt($result
      ->getData(), $solr_id, $result_item, $field_names)) {
      $result_item
        ->setExcerpt($excerpt);
    }
    $result_set
      ->addResultItem($result_item);
  }

  // Check for spellcheck suggestions.

  /*if (module_exists('search_api_spellcheck') && $query->getOption('search_api_spellcheck')) {
       $result_set->setExtraData('search_api_spellcheck', new SearchApiSpellcheckSolr($result));
    }*/
  return $result_set;
}