You are here

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

Helper function that parse facets.

Parameters

array $response:

QueryInterface $query:

Return value

array

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 747
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 parseSearchFacets(array $response, QueryInterface $query) {
  $result = [];
  $index_fields = $this
    ->getIndexFields($query);
  $facets = $query
    ->getOption('search_api_facets');
  if (!empty($facets) && isset($response['facets'])) {
    foreach ($response['facets'] as $facet_id => $facet_data) {
      if (isset($facets[$facet_id])) {
        $facet_info = $facets[$facet_id];
        $facet_min_count = $facet_info['min_count'];
        $field_id = $facet_info['field'];
        $field_type = search_api_extract_inner_type($index_fields[$field_id]['type']);

        // TODO: handle different types (GeoDistance and so on).
        if ($field_type === 'date') {
          foreach ($facet_data['entries'] as $entry) {
            if ($entry['count'] >= $facet_min_count) {

              // Divide time by 1000 as we want seconds from epoch
              // not milliseconds.
              $result[$facet_id][] = [
                'count' => $entry['count'],
                'filter' => '"' . $entry['time'] / 1000 . '"',
              ];
            }
          }
        }
        else {
          foreach ($facet_data['terms'] as $term) {
            if ($term['count'] >= $facet_min_count) {
              $result[$facet_id][] = [
                'count' => $term['count'],
                'filter' => '"' . $term['term'] . '"',
              ];
            }
          }
        }
      }
    }
  }
  return $result;
}