You are here

protected function SearchApiElasticsearchElastica::addSearchAggregation in Search API Elasticsearch 7

Helper function build Aggregations in search.

2 calls to SearchApiElasticsearchElastica::addSearchAggregation()
SearchApiElasticsearchElastica::search in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Overrides search().
SearchApiElasticsearchElastica::searchMultiple in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Implements SearchApiMultiServiceInterface::searchMultiple().

File

modules/elastica/includes/SearchApiElasticsearchElastica.inc, line 683
Provides Elastica client for Search API Elasticsearch.

Class

SearchApiElasticsearchElastica
Search API Elasticsearch Elastica service class.

Code

protected function addSearchAggregation(Elastica\Query $elastica_query, SearchApiQueryInterface $query) {

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

    // Loop trough Aggregations.
    foreach ($aggs as $agg_id => $agg_info) {
      $agg = NULL;
      $field_id = $agg_info['field'];

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

      // TODO: handle different types (GeoDistance and so on).
      if ($field_type === 'date') {
        $agg = $this
          ->createDateFieldAggregation($agg_id);
      }
      else {
        if ($field_type === 'string') {

          //Check if string is latlong
          if (strpos($agg_id, 'latlon') !== false) {

            //This is a latitude and longitude pair.
          }
          else {
            $agg = new Elastica\Aggregation\Terms($agg_id);
          }
        }
        else {
          $agg = new Elastica\Aggregation\Terms($agg_id);

          // We may want missing Aggregation.

          //$Agg->setAllTerms($agg_missing);
        }
      }

      // Add the Aggregation.
      if (!empty($agg)) {

        // Add Aggregation options.
        $agg = $this
          ->addAggregationOptions($agg, $query, $agg_info, $elastica_query, $field_type);
        $elastica_query
          ->addAggregation($agg);
      }
    }
  }
}