You are here

protected function SearchApiSolrBackend::setGrouping 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::setGrouping()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setGrouping()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setGrouping()

Sets grouping for the query.

Parameters

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

\Drupal\search_api\Query\QueryInterface $query:

array $grouping_options:

array $index_fields:

array $field_names:

Throws

\Drupal\search_api_solr\SearchApiSolrException

1 call to SearchApiSolrBackend::setGrouping()
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 2763

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setGrouping(Query $solarium_query, QueryInterface $query, $grouping_options = [], $index_fields = [], $field_names = []) {
  if (!empty($grouping_options['use_grouping'])) {
    $group_fields = [];
    foreach ($grouping_options['fields'] as $collapse_field) {
      $first_name = $field_names[$collapse_field];

      /** @var \Drupal\search_api\Item\Field $field */
      $field = $index_fields[$collapse_field];
      $type = $field
        ->getType();
      if ($this->dataTypeHelper
        ->isTextType($type)) {
        $this
          ->getLogger()
          ->error('Grouping is not supported for field @field. Only single-valued fields not indexed as "Fulltext" are supported.', [
          '@field' => $index_fields[$collapse_field]['name'],
        ]);
      }
      else {
        $group_fields[] = $first_name;
      }
    }
    if (!empty($group_fields)) {

      // Activate grouping on the solarium query.
      $grouping_component = $solarium_query
        ->getGrouping();
      $grouping_component
        ->setFields($group_fields)
        ->setNumberOfGroups(TRUE)
        ->setTruncate(!empty($grouping_options['truncate']))
        ->setFacet(!empty($grouping_options['group_facet']));
      if (!empty($grouping_options['group_limit']) && $grouping_options['group_limit'] != 1) {
        $grouping_component
          ->setLimit($grouping_options['group_limit']);
      }
      if (!empty($grouping_options['group_sort'])) {
        $sorts = [];
        foreach ($grouping_options['group_sort'] as $group_sort_field => $order) {
          if (isset($fields[$group_sort_field])) {
            $f = $fields[$group_sort_field];
            if (substr($f, 0, 3) === 'ss_') {
              $f = 'sort_' . substr($f, 3);
            }
            $sorts[] = $f . ' ' . strtolower($order);
          }
        }
        $grouping_component
          ->setSort(implode(', ', $sorts));
      }
    }
  }
}