You are here

public function SearchApiElasticsearchBackend::parseSearchResponse in Elasticsearch Connector 8

Helper function which parse facets in search().

1 call to SearchApiElasticsearchBackend::parseSearchResponse()
SearchApiElasticsearchBackend::search in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Overrides search().

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 1379
Contains the SearchApiElasticsearchBackend object.

Class

SearchApiElasticsearchBackend
Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function parseSearchResponse(array $response, QueryInterface $query) {
  $index = $query
    ->getIndex();

  // Set up the results array.
  $results = SearchApiUtility::createSearchResultSet($query);
  $results
    ->setExtraData('elasticsearch_response', $response);
  $results
    ->setResultCount($response['hits']['total']);

  // Add each search result to the results array.
  if (!empty($response['hits']['hits'])) {
    foreach ($response['hits']['hits'] as $result) {
      $result_item = SearchApiUtility::createItem($index, $result['_id']);
      $result_item
        ->setScore($result['_score']);

      // Set each item in _source as a field in Search API.
      foreach ($result['_source'] as $elasticsearch_property_id => $elasticsearch_property) {

        // Make everything a multifield.
        if (!is_array($elasticsearch_property)) {
          $elasticsearch_property = array(
            $elasticsearch_property,
          );
        }
        $field = SearchApiUtility::createField($index, $elasticsearch_property_id);
        $field
          ->setValues($elasticsearch_property);
        $result_item
          ->setField($elasticsearch_property_id, $field);
      }

      // @todo: Add excerpt handling
      $results
        ->addResultItem($result_item);
    }
  }
  return $results;
}