diff --git a/src/Plugin/search_api/backend/SearchApiSolrBackend.php b/src/Plugin/search_api/backend/SearchApiSolrBackend.php index b880a36..cf36588 100644 --- a/src/Plugin/search_api/backend/SearchApiSolrBackend.php +++ b/src/Plugin/search_api/backend/SearchApiSolrBackend.php @@ -459,7 +459,7 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter 'search_api_mlt', 'search_api_random_sort', 'search_api_data_type_location', - // 'search_api_grouping', + 'search_api_grouping', // 'search_api_spellcheck', // 'search_api_data_type_geohash', ]; @@ -1393,20 +1393,30 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter // If field collapsing has been enabled for this query, we need to process // the results differently. $grouping = $query->getOption('search_api_grouping'); - $docs = array(); - if (!empty($grouping['use_grouping']) && $is_grouping) { - // $docs = array(); - // $result_set['result count'] = 0; - // foreach ($grouping['fields'] as $field) { - // if (!empty($response->grouped->{$fields[$field]})) { - // $result_set['result count'] += $response->grouped->{$fields[$field]}->ngroups; - // foreach ($response->grouped->{$fields[$field]}->groups as $group) { - // foreach ($group->doclist->docs as $doc) { - // $docs[] = $doc; - // } - // } - // } - // }. + if (!empty($grouping['use_grouping'])) { + $docs = []; + $resultCount = 0; + if ($result_set->hasExtraData('search_api_solr_response')) { + $response = $result_set->getExtraData('search_api_solr_response'); + foreach ($grouping['fields'] as $field) { + $solr_field_name = $field_names[$field]; + if (!empty($response['grouped'][$solr_field_name])) { + $resultCount += count($response['grouped'][$solr_field_name]); + foreach ($response['grouped'][$solr_field_name]['groups'] as $group) { + foreach ($group['doclist']['docs'] as $doc) { + $docs[] = $doc; + } + } + } + } + // Set a default number then get the groups number if possible. + $result_set->setResultCount($resultCount); + if (count($grouping['fields']) == 1) { + $field = reset($grouping['fields']); + $solr_field_name = $field_names[$field]; + if (isset($response['grouped'][$solr_field_name]['ngroups'])) { + $result_set->setResultCount($response['grouped'][$solr_field_name]['ngroups']); + } + } + } } else { $result_set->setResultCount($result->getNumFound()); @@ -1414,9 +1424,13 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter } // Add each search result to the results array. - /** @var \Solarium\QueryType\Select\Result\Document $doc */ foreach ($docs as $doc) { - $doc_fields = $doc->getFields(); + if(is_array($doc)) { + $doc_fields = $doc; + }else { + /** @var \Solarium\QueryType\Select\Result\Document $doc */ + $doc_fields = $doc->getFields(); + } $item_id = $doc_fields[$id_field]; // For items coming from a different site, we need to adapt the item ID. if (!$this->configuration['site_hash'] && $doc_fields['hash'] != $site_hash) { @@ -2740,6 +2754,7 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter * @todo This code is outdated and needs to be reviewd and refactored. */ protected function setGrouping(Query $solarium_query, QueryInterface $query, $grouping_options = array(), $index_fields = array(), $field_names = array()) { + $solarium_query->getGrouping(); $group_params['group'] = 'true'; // We always want the number of groups returned so that we get pagers done // right. @@ -2751,8 +2766,9 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter $group_params['group.facet'] = 'true'; } foreach ($grouping_options['fields'] as $collapse_field) { - $type = $index_fields[$collapse_field]['type']; - // Only single-valued fields are supported. + /** @var $field Field $type */ + $field = $index_fields[$collapse_field]; + $type = $field->getType(); 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.', array('@field' => $index_fields[$collapse_field]['name'])); @@ -2766,8 +2782,8 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter 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 (isset($field_names[$group_sort_field])) { + $f = $field_names[$group_sort_field]; if (substr($f, 0, 3) == 'ss_') { $f = 'sort_' . substr($f, 3); } diff --git a/tests/src/Kernel/SearchApiSolrTest.php b/tests/src/Kernel/SearchApiSolrTest.php index 91fe760..3e07308 100644 --- a/tests/src/Kernel/SearchApiSolrTest.php +++ b/tests/src/Kernel/SearchApiSolrTest.php @@ -801,6 +801,35 @@ class SearchApiSolrTest extends BackendTestBase { } } + /** + * Tests search result groping + */ + public function testSearchResultGrouping() { + //Only run the tests if we have a Solr core available + if($this->solrAvailable) { + $this->insertExampleContent(); + $this->indexItems($this->indexId); + + $query = $this->buildSearch(NULL, [], [], FALSE); + $query->setOption('search_api_grouping', [ + 'use_grouping' => TRUE, + 'fields' => [ + 'type', + ], + ]); + $results = $query->execute(); + + $this->assertEquals(2, $results->getResultCount(), 'Get the results count grouping by type.'); + $data = $results->getExtraData('search_api_solr_response'); + $this->assertEquals(5, $data['grouped']['ss_type']['matches'], 'Get the total documents after groping.'); + $this->assertEquals(2, $data['grouped']['ss_type']['ngroups'], 'Get the number of groups after groping.'); + $this->assertResults([1, 4], $results, 'Grouping by type'); + } + else { + $this->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/d8'); + } + } + /** * Tests the autocomplete support. */