You are here

protected function SearchApiElasticsearchBackend::addSearchFacets in Elasticsearch Connector 8.2

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::addSearchFacets()
  2. 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()
  3. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()
  4. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()

Helper function build facets in search.

Parameters

array $params:

QueryInterface $query:

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 521
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

protected function addSearchFacets(array &$params, QueryInterface $query) {

  // SEARCH API FACETS.
  $facets = $query
    ->getOption('search_api_facets');
  $index_fields = $this
    ->getIndexFields($query);
  if (!empty($facets)) {

    // Loop trough facets.
    foreach ($facets as $facet_id => $facet_info) {
      $field_id = $facet_info['field'];
      $facet = [
        $field_id => [],
      ];

      // Skip if not recognized as a known field.
      if (!isset($index_fields[$field_id])) {
        continue;
      }
      $field_type = search_api_extract_inner_type($index_fields[$field_id]['type']);

      // TODO: handle different types (GeoDistance and so on). See the
      // supportedFeatures todo.
      if ($field_type === 'date') {
        $facet_type = 'date_histogram';
        $facet[$field_id] = $this
          ->createDateFieldFacet($field_id, $facet);
      }
      else {
        $facet_type = 'terms';
        $facet[$field_id][$facet_type]['all_terms'] = (bool) $facet_info['missing'];
      }

      // Add the facet.
      if (!empty($facet[$field_id])) {

        // Add facet options.
        $facet_info['facet_type'] = $facet_type;
        $facet[$field_id] = $this
          ->addFacetOptions($facet[$field_id], $query, $facet_info);
      }
      $params['body']['facets'][$field_id] = $facet[$field_id];
    }
  }
}