You are here

protected function SearchApiSolrBackend::setGrouping in Search API Solr 8.2

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 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.

@todo This code is outdated and needs to be reviewd and refactored.

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 3356

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 = []) {
  $group_params['group'] = 'true';

  // We always want the number of groups returned so that we get pagers done
  // right.
  $group_params['group.ngroups'] = 'true';
  if (!empty($grouping_options['truncate'])) {
    $group_params['group.truncate'] = 'true';
  }
  if (!empty($grouping_options['group_facet'])) {
    $group_params['group.facet'] = 'true';
  }
  foreach ($grouping_options['fields'] as $collapse_field) {
    $type = $index_fields[$collapse_field]['type'];

    // Only single-valued fields are supported.
    if ($this->dataTypeHelper
      ->isTextType($type)) {
      $warnings[] = $this
        ->t('Grouping is not supported for field @field. Only single-valued fields not indexed as "Fulltext" are supported.', [
        '@field' => $index_fields[$collapse_field]['name'],
      ]);
      continue;
    }
    $group_params['group.field'][] = $field_names[$collapse_field];
  }
  if (empty($group_params['group.field'])) {
    unset($group_params);
  }
  else {
    if (!empty($grouping_options['group_sort'])) {
      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);
          }
          $order = strtolower($order);
          $group_params['group.sort'][] = $f . ' ' . $order;
        }
      }
      if (!empty($group_params['group.sort'])) {
        $group_params['group.sort'] = implode(', ', $group_params['group.sort']);
      }
    }
    if (!empty($grouping_options['group_limit']) && $grouping_options['group_limit'] != 1) {
      $group_params['group.limit'] = $grouping_options['group_limit'];
    }
  }
  foreach ($group_params as $param_id => $param_value) {
    $solarium_query
      ->addParam($param_id, $param_value);
  }
}