You are here

protected function SearchApiElasticsearchConnector::addSearchFacets in Elasticsearch Connector 7.2

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::addSearchFacets()
  2. 7 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::addSearchFacets()

Helper function build facets in search.

1 call to SearchApiElasticsearchConnector::addSearchFacets()
SearchApiElasticsearchConnector::search in modules/elasticsearch_connector_search_api/service.inc
Overrides search().

File

modules/elasticsearch_connector_search_api/service.inc, line 1748
Provides a Elasticsearch-based service class for the Search API using Elasticsearch Connector module.

Class

SearchApiElasticsearchConnector
Search service class.

Code

protected function addSearchFacets(array &$params, SearchApiQueryInterface $query) {

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

    // Loop trough facets.
    $aggs = \nodespark\DESConnector\Elasticsearch\Aggregations\Aggregations::getInstance();
    foreach ($facets as $facet_id => $facet_info) {
      $field_id = $facet_info['field'];
      $facet = array(
        $field_id => array(),
      );

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

      // TODO: handle different types (GeoDistance and so on). See the supportedFeatures todo.
      if ($field_type === 'date') {
        $interval = $this
          ->getDateFacetInterval($facet_id);
        $facet[$field_id] = new \nodespark\DESConnector\Elasticsearch\Aggregations\Bucket\DateHistogram($field_id, $field_id, $interval);
        $aggs
          ->setAggregation($facet[$field_id]);
      }
      else {
        $facet[$field_id] = new \nodespark\DESConnector\Elasticsearch\Aggregations\Bucket\Terms($field_id, $field_id);
        $facet[$field_id]
          ->setSize($this
          ->getFacetLimit($facet_info));
        $aggs
          ->setAggregation($facet[$field_id]);
      }
    }
    $aggs
      ->applyAggregationsToParams($params);
  }
}