protected function SearchApiSolrBackend::extractResults in Search API Solr 8.2
Same name and namespace in other branches
- 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::extractResults()
- 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::extractResults()
- 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.
Throws
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 1716 
Class
- SearchApiSolrBackend
- Apache Solr backend for search api.
Namespace
Drupal\search_api_solr\Plugin\search_api\backendCode
protected function extractResults(QueryInterface $query, ResultInterface $result) {
  $index = $query
    ->getIndex();
  $fields = $index
    ->getFields(TRUE);
  $site_hash = Utility::getSiteHash();
  // We can find the item ID and the score in the special 'search_api_*'
  // properties. Mappings are provided for these properties in
  // SearchApiSolrBackend::getSolrFieldNames().
  $field_names = $this
    ->getSolrFieldNames($index);
  $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');
  $docs = [];
  if (!empty($grouping['use_grouping']) && $is_grouping) {
    // $docs = array();
    //      $result_set['result count'] = 0;
    //      foreach ($grouping['fields'] as $field) {
    //        if (!empty($response->grouped->{$fields[$field]})) {
    //          $result_set['result count'] += $response->grouped->{$fields[$field]}->ngroups;
    //          foreach ($response->grouped->{$fields[$field]}->groups as $group) {
    //            foreach ($group->doclist->docs as $doc) {
    //              $docs[] = $doc;
    //            }
    //          }
    //        }
    //      }.
  }
  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) {
    $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 (isset($doc_fields['hash']) && !$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);
    // If the schema doesn't contain a language field or it is empty we
    // satisfy Seach API by setting the site's default language.
    $result_item
      ->setLanguage(isset($doc_fields[$language_field]) ? $doc_fields[$language_field] : LanguageInterface::LANGCODE_SITE_DEFAULT);
    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::getSolrFieldNames().
    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($this
      ->getIndexId($index), $result_item
      ->getId());
    $this
      ->getHighlighting($result
      ->getData(), $solr_id, $result_item, $field_names);
    $result_set
      ->addResultItem($result_item);
  }
  return $result_set;
}