You are here

protected function SearchApiElasticsearchQuery::parseAggregations in Search API Elasticsearch 7.2

Parse aggregations for Facet API.

Parameters

ResultSet $result_set:

Return value

array

1 call to SearchApiElasticsearchQuery::parseAggregations()
SearchApiElasticsearchQuery::parseResults in includes/SearchApiElasticsearchQuery.inc
Parse Elasticsearch results for use by Search API.

File

includes/SearchApiElasticsearchQuery.inc, line 147

Class

SearchApiElasticsearchQuery
@file

Code

protected function parseAggregations(ResultSet $result_set) {
  $result = [];
  $aggregations = $this->search_api_query
    ->getOption('search_api_facets');
  if (!empty($aggregations) && $result_set
    ->hasAggregations()) {
    foreach ($result_set
      ->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($this->index_fields[$field_id]['type']);
        if ($field_type === 'date') {
          foreach ($aggregation_data['buckets'] as $entry) {
            if ($entry['doc_count'] >= $aggregation_min_count) {
              $result[$aggregation_id][] = [
                'count' => $entry['doc_count'],
                'filter' => '"' . $entry['key'] / 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;
}