You are here

protected function SearchApiElasticsearchQuery::addAggregations in Search API Elasticsearch 7.2

Add aggregations to an Elasticsearch query.

1 call to SearchApiElasticsearchQuery::addAggregations()
SearchApiElasticsearchQuery::build in includes/SearchApiElasticsearchQuery.inc
Build Elastica query for Elasticsearch.

File

includes/SearchApiElasticsearchQuery.inc, line 189

Class

SearchApiElasticsearchQuery
@file

Code

protected function addAggregations() {
  $aggregations = $this->search_api_query
    ->getOption('search_api_facets');
  if (!empty($aggregations)) {
    $searcher = key(facetapi_get_active_searchers());

    /** @var SearchApiFacetapiAdapter $adapter */
    $adapter = isset($searcher) ? facetapi_adapter_load($searcher) : NULL;
    $enabled_facets = $adapter
      ->getEnabledFacets();
    foreach ($aggregations as $aggregation_id => $aggregation_info) {
      $aggregation = null;
      $field_id = $aggregation_info['field'];
      if (!isset($this->index_fields[$field_id])) {
        continue;
      }
      $field_type = search_api_extract_inner_type($this->index_fields[$field_id]['type']);

      /** @var FacetapiFacet $facet */
      $facet = $adapter
        ->getFacet($enabled_facets[$aggregation_id]);
      $facet_settings = $facet
        ->getSettings();
      if ($field_type === 'date') {
        $gap_weight = array(
          'YEAR' => 2,
          'MONTH' => 1,
          'DAY' => 0,
        );

        // Get the date granularity.
        $date_gap = $facet_settings->settings['date_granularity'];

        // Get the current date gap from the active date filters.
        $active_items = $adapter
          ->getActiveItems(array(
          'name' => $aggregation_id,
        ));
        if (!empty($active_items)) {
          foreach ($active_items as $active_item) {
            $value = $active_item['value'];
            if (strpos($value, ' TO ') > 0) {
              list($date_min, $date_max) = explode(' TO ', str_replace(array(
                '[',
                ']',
              ), '', $value), 2);
              $gap = facetapi_get_timestamp_gap($date_min, $date_max);
              if (isset($gap_weight[$gap])) {
                $gaps[] = $gap_weight[$gap];
              }
            }
          }
          if (!empty($gaps)) {

            // Minimum gap.
            $date_gap = array_search(min($gaps), $gap_weight);
          }
        }
        switch ($date_gap) {

          // Already a selected YEAR, we want the months.
          case 'YEAR':
            $date_interval = 'month';
            break;

          // Already a selected MONTH, we want the days.
          case 'MONTH':
            $date_interval = 'day';
            break;

          // Already a selected DAY, we want the hours and so on.
          case 'DAY':
            $date_interval = 'hour';
            break;

          // By default we return result counts by year.
          default:
            $date_interval = 'year';
        }
        $aggregation = new \Elastica\Aggregation\DateHistogram($aggregation_id, $field_id, $date_interval);
      }
      elseif ($field_type === 'string') {
        if (strpos($aggregation_id, 'latlon') !== false) {
        }
        else {
          $aggregation = new Terms($aggregation_id);
        }
      }
      if (!empty($aggregation)) {
        $this
          ->setAggregationOptions($aggregation, $field_type, $aggregation_info);
        $this->query
          ->addAggregation($aggregation);
      }
    }
  }
}