You are here

protected function SearchApiElasticsearchConnector::parseSearchFacets in Elasticsearch Connector 7.2

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

Helper function that parse facets.

1 call to SearchApiElasticsearchConnector::parseSearchFacets()
SearchApiElasticsearchConnector::parseSearchResponse in modules/elasticsearch_connector_search_api/service.inc
Helper function which parse facets in search().

File

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

Class

SearchApiElasticsearchConnector
Search service class.

Code

protected function parseSearchFacets($response, SearchApiQueryInterface $query) {
  $result = array();
  $index_fields = $this
    ->getIndexFields($query);
  $facets = $query
    ->getOption('search_api_facets');
  if (!empty($facets) && isset($response['aggregations'])) {
    foreach ($response['aggregations'] as $facet_id => $facet_data) {
      if (isset($facets[$facet_id])) {
        $facet_info = $facets[$facet_id];
        $facet_min_count = $facet_info['min_count'];
        $field_id = $facet_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 ($facet_data['buckets'] as $entry) {
            if ($entry['doc_count'] >= $facet_min_count) {

              // Divide time by 1000 as we want seconds from epoch
              // not milliseconds.
              $result[$facet_id][] = array(
                'count' => $entry['doc_count'],
                'filter' => '"' . $entry['key'] / 1000 . '"',
              );
            }
          }
        }
        else {
          foreach ($facet_data['buckets'] as $term) {
            if ($term['doc_count'] >= $facet_min_count) {
              if ($field_type == 'boolean') {
                $term_value = $term['key'] == 'T' ? TRUE : FALSE;
              }
              else {
                $term_value = '"' . $term['key'] . '"';
              }
              $result[$facet_id][] = array(
                'count' => $term['doc_count'],
                'filter' => $term_value,
              );
            }
          }
        }
      }
    }
  }
  return $result;
}