You are here

protected function SearchApiElasticsearchAbstractService::parseSearchAggregation in Search API Elasticsearch 7

Helper function that parse facets.

1 call to SearchApiElasticsearchAbstractService::parseSearchAggregation()
SearchApiElasticsearchElastica::parseSearchResponse in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Helper function which parse facets in search().

File

includes/SearchApiElasticsearchAbstractService.inc, line 690
Provides a Elasticsearch-based service class for the Search API.

Class

SearchApiElasticsearchAbstractService
Elasticsearch service abstract class.

Code

protected function parseSearchAggregation($response, SearchApiQueryInterface $query) {
  $result = array();
  $index_fields = $this
    ->getIndexFields($query);
  $aggregations = $query
    ->getOption('search_api_facets');
  if (!empty($aggregations) && $response
    ->hasAggregations()) {
    foreach ($response
      ->getAggregations() as $aggregation_id => $aggregation_data) {
      if (isset($aggregations[$aggregation_id])) {
        $aggregation_info = $aggregations[$aggregation_id];
        $aggregation_min_count = $aggregation_info['min_count'];
        $field_id = $aggregation_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 ($aggregation_data['buckets'] as $entry) {
            if ($entry['count'] >= $aggregation_min_count) {

              // Divide time by 1000 as we want seconds from epoch
              // not milliseconds.
              $result[$aggregation_id][] = array(
                'count' => $entry['count'],
                'filter' => '"' . $entry['time'] / 1000 . '"',
              );
            }
          }
        }
        else {
          foreach ($aggregation_data['buckets'] as $term) {
            if ($term['doc_count'] >= $aggregation_min_count) {
              $result[$aggregation_id][] = array(
                'count' => $term['doc_count'],
                'filter' => '"' . $term['key'] . '"',
              );
            }
          }
        }
      }
    }
  }
  return $result;
}