You are here

protected function SearchApiSolrService::getFacetParams in Search API Solr 7

Helper method for creating the facet field parameters.

2 calls to SearchApiSolrService::getFacetParams()
SearchApiSolrService::search in includes/service.inc
Executes a search on the server represented by this object.
SearchApiSolrService::searchMultiple in includes/service.inc
Implements SearchApiMultiServiceInterface::searchMultiple().

File

includes/service.inc, line 1960

Class

SearchApiSolrService
Search service class using Solr server.

Code

protected function getFacetParams(array $facets, array $fields, array &$fq = array()) {
  if (!$facets) {
    return array();
  }
  $facet_params['facet'] = 'true';
  $facet_params['facet.sort'] = 'count';
  $facet_params['facet.limit'] = 10;
  $facet_params['facet.mincount'] = 1;
  $facet_params['facet.missing'] = 'false';
  foreach ($facets as $info) {
    if (empty($fields[$info['field']])) {
      continue;
    }

    // String fields have their own corresponding facet fields.
    $field = $fields[$info['field']];

    // Check for the "or" operator.
    if (isset($info['operator']) && $info['operator'] === 'or') {

      // This tag should automatically be placed on any filters created via
      // this filter by the Facet API integration. We here use it to exclude
      // those filters from the logic creating OR facet filters.
      $tag = 'facet:' . $info['field'];

      // Check whether Solr facet queries were set for this facet and use
      // those.
      if (!empty($info['solr_facet_query'])) {
        foreach ($info['solr_facet_query'] as $expression) {
          $facet_params['facet.query'][] = "{!ex={$tag}}{$field}:{$expression}";
        }
      }
      else {
        $facet_params['facet.field'][] = "{!ex={$tag}}{$field}";
      }
    }
    elseif (!empty($info['solr_facet_query'])) {

      // No tagging and excluding like above is necessary as facets are built
      // with all filters applied.
      foreach ($info['solr_facet_query'] as $expression) {
        $facet_params['facet.query'][] = "{$field}:{$expression}";
      }
    }
    else {

      // Add the facet field.
      $facet_params['facet.field'][] = $field;
    }

    // Set limit, unless it's the default.
    if ($info['limit'] != 10) {
      $facet_params["f.{$field}.facet.limit"] = $info['limit'] ? $info['limit'] : -1;
    }

    // Set mincount, unless it's the default.
    if ($info['min_count'] != 1) {
      $facet_params["f.{$field}.facet.mincount"] = $info['min_count'];
    }

    // Set missing, if specified.
    if ($info['missing']) {
      $facet_params["f.{$field}.facet.missing"] = 'true';
    }
  }
  return $facet_params;
}