You are here

protected function SearchApiElasticsearchBackend::addFacets in Elasticsearch Connector 8.5

Same name and namespace in other branches
  1. 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addFacets()
  2. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addFacets()

Fill the aggregation array of the request.

Parameters

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

1 call to SearchApiElasticsearchBackend::addFacets()
SearchApiElasticsearchBackend::search in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Executes a search on this server.

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 655

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

protected function addFacets(QueryInterface $query) {
  foreach ($query
    ->getOption('search_api_facets') as $key => $facet) {
    $facet += [
      'type' => NULL,
    ];
    $object = NULL;

    // @todo Add more options.
    switch ($facet['type']) {
      case 'stats':
        $object = new Stats($key, $key);
        break;
      default:
        $object = new Terms($key, $key);

        // Limit the number of facets in the result according the to facet
        // setting. A limit of 0 means no limit. Elasticsearch doesn't have a
        // way to set no limit, so we set a large integer in that case.
        $size = $facet['limit'] ? $facet['limit'] : self::FACET_NO_LIMIT_SIZE;
        $object
          ->setSize($size);

        // Set global scope for facets with 'OR' operator.
        if ($facet['operator'] == 'or') {
          $object
            ->setGlobalScope(TRUE);
        }
    }
    if (!empty($object)) {
      $this->client
        ->aggregations()
        ->setAggregation($object);
    }
  }
}