You are here

protected function SearchApiSolrBackend::setFacets in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setFacets()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setFacets()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setFacets()

Helper method for creating the facet field parameters.

Parameters

\Drupal\search_api\Query\QueryInterface $query:

\Solarium\QueryType\Select\Query\Query $solarium_query:

array $field_names:

1 call to SearchApiSolrBackend::setFacets()
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 1959

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setFacets(QueryInterface $query, Query $solarium_query, array $field_names) {
  $facets = $query
    ->getOption('search_api_facets', []);
  if (empty($facets)) {
    return;
  }
  $facet_set = $solarium_query
    ->getFacetSet();
  $facet_set
    ->setSort('count');
  $facet_set
    ->setLimit(10);
  $facet_set
    ->setMinCount(1);
  $facet_set
    ->setMissing(FALSE);
  foreach ($facets as $info) {
    if (empty($field_names[$info['field']])) {
      continue;
    }
    $field = $field_names[$info['field']];

    // Create the Solarium facet field object.
    $facet_field = $facet_set
      ->createFacetField($field)
      ->setField($field);

    // For "OR" facets, add the expected tag for exclusion.
    if (isset($info['operator']) && strtolower($info['operator']) === 'or') {

      // @see https://cwiki.apache.org/confluence/display/solr/Faceting#Faceting-LocalParametersforFaceting
      $facet_field
        ->getLocalParameters()
        ->clearExcludes()
        ->addExcludes([
        'facet:' . $info['field'],
      ]);
    }

    // Set limit, unless it's the default.
    if ($info['limit'] != 10) {
      $limit = $info['limit'] ? $info['limit'] : -1;
      $facet_field
        ->setLimit($limit);
    }

    // Set mincount, unless it's the default.
    if ($info['min_count'] != 1) {
      $facet_field
        ->setMinCount($info['min_count']);
    }

    // Set missing, if specified.
    if ($info['missing']) {
      $facet_field
        ->setMissing(TRUE);
    }
    else {
      $facet_field
        ->setMissing(FALSE);
    }
  }
}