You are here

protected function SearchApiSolrAnySchemaBackend::postQuery in Search API Solr 8.2

Allow custom changes before search results are returned for subclasses.

Works exactly like hook_search_api_solr_search_results_alter().

Parameters

\Drupal\search_api\Query\ResultSetInterface $results: The results array that will be returned for the search.

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

object $response: The response object returned by Solr.

Overrides SearchApiSolrBackend::postQuery

File

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

Class

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

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function postQuery(ResultSetInterface $results, QueryInterface $query, $response) {
  parent::postQuery($results, $query, $response);

  // Do not alter the results if the index does not use the solr_document
  // datasource.
  $datasources = $query
    ->getIndex()
    ->getDatasources();
  if (!isset($datasources['solr_document'])) {
    return;
  }

  /** @var \Drupal\search_api_solr\SolrDocumentFactoryInterface $solr_document_factory */
  $solr_document_factory = \Drupal::getContainer()
    ->get('solr_document.factory');

  /** @var \Drupal\search_api\Item\Item $item */
  foreach ($results
    ->getResultItems() as $item) {

    // Create the typed data object for the Item immediately after the query
    // has been run. Doing this now can prevent the Search API from having to
    // query for individual documents later.
    $item
      ->setOriginalObject($solr_document_factory
      ->create($item));

    // Prepend each item's itemId with the datasource ID. A lot of the Search
    // API assumes that the item IDs are formatted as
    // 'datasouce_id/entity_id'. Of course, the ID numbers of external Solr
    // documents will not have this pattern and the datasource must be added.
    // Reflect into the class to set the itemId.
    $reflection = new \ReflectionClass($item);
    $id_property = $reflection
      ->getProperty('itemId');
    $id_property
      ->setAccessible(TRUE);
    $id_property
      ->setValue($item, 'solr_document/' . $item
      ->getId());
  }
}