diff --git a/Classes/Domain/Search/Query/SuggestQuery.php b/Classes/Domain/Search/Query/SuggestQuery.php index abfeeedc6d..61b58d948f 100644 --- a/Classes/Domain/Search/Query/SuggestQuery.php +++ b/Classes/Domain/Search/Query/SuggestQuery.php @@ -51,10 +51,11 @@ public function __construct(string $keywords, TypoScriptConfiguration $solrConfi } $this->getEDisMax()->setQueryAlternative('*:*'); - $this->setFields(ReturnFields::fromString(($this->configuration['suggestField'] ?? ''))->getValues()); + $suggestFields = ReturnFields::fromString(($this->configuration['suggestField'] ?? ''))->getValues(); + $this->setFields($suggestFields); $this->addParam('facet', 'on'); $this->addParam('facet.prefix', $this->prefix); - $this->addParam('facet.field', ($this->configuration['suggestField'] ?? null)); + $this->addParam('facet.field', count($suggestFields) ? $suggestFields : null); $this->addParam('facet.limit', ($this->configuration['numberOfSuggestions'] ?? null)); $this->addParam('facet.mincount', 1); $this->addParam('facet.method', 'enum'); diff --git a/Classes/Domain/Search/Suggest/SuggestService.php b/Classes/Domain/Search/Suggest/SuggestService.php index 6839eb80c9..02a375c060 100644 --- a/Classes/Domain/Search/Suggest/SuggestService.php +++ b/Classes/Domain/Search/Suggest/SuggestService.php @@ -159,9 +159,20 @@ protected function getSolrSuggestions(ServerRequestInterface $request, SuggestQu return []; } $results = json_decode($rawResponse); + $suggestConfig = $this->typoScriptConfiguration->getObjectByPath('plugin.tx_solr.suggest.'); - $facetSuggestions = isset($suggestConfig['suggestField']) ? $results->facet_counts->facet_fields->{$suggestConfig['suggestField']} ?? [] : []; - return ParsingUtil::getMapArrayFromFlatArray($facetSuggestions); + $suggestFields = GeneralUtility::trimExplode(',', $suggestConfig['suggestField'], true); + $facetSuggestions = []; + foreach ($suggestFields as $suggestField) { + $suggestions = ParsingUtil::getMapArrayFromFlatArray($results->facet_counts->facet_fields->{$suggestField} ?? []); + foreach ($suggestions as $key => $value) { + if (!array_key_exists($key, $facetSuggestions)) { + $facetSuggestions[$key] = 0; + } + $facetSuggestions[$key] += $value; + } + } + return $facetSuggestions; } /** diff --git a/Documentation/Configuration/Reference/TxSolrSuggest.rst b/Documentation/Configuration/Reference/TxSolrSuggest.rst index e53cec869c..ba97208bf4 100644 --- a/Documentation/Configuration/Reference/TxSolrSuggest.rst +++ b/Documentation/Configuration/Reference/TxSolrSuggest.rst @@ -28,9 +28,10 @@ suggestField :Default: spell Sets the Solr index field used to get suggestions from. A general advice is to use a field without stemming on it. For practical reasons this is currently the spell checker field. +Multiple fields can be defined as a comma separated list. Note: With EXT:solr 11.1.0 ASCII folding and language depending normalization filters were introduced, but due to the special behaviour of the auto suggestions ascii-terms were not treated correctly. So with 11.1.3 the untouched tokens are also kept, as this might lead to duplicate -suggestions, a new field for exact suggestions is introduced, if you want to avoid duplicates and use stricter suggestions, just configure `spellExact` as suggest field. +suggestions, a new field for exact suggestions is introduced, if you want to avoid duplicates and use stricter suggestions, just configure `spellExact` as suggest field. forceHttps ---------- diff --git a/Tests/Integration/Controller/Fixtures/suggest_with_multiple_fields.csv b/Tests/Integration/Controller/Fixtures/suggest_with_multiple_fields.csv new file mode 100644 index 0000000000..ffd72126d0 --- /dev/null +++ b/Tests/Integration/Controller/Fixtures/suggest_with_multiple_fields.csv @@ -0,0 +1,5 @@ +"pages", +,"uid","pid","is_siteroot","doktype","slug","title","seo_title" +,2,1,1,1,"/multiple-fields","Multiple fields","Totally different SEO title" +,3,1,0,1,"/multiple-fields2","Multiple fields 2","Another SEO title" +,4,1,0,1,"/multiple-fields2","Multiple fields 3","Another title" diff --git a/Tests/Integration/Controller/SuggestControllerTest.php b/Tests/Integration/Controller/SuggestControllerTest.php index 9df4c75eb6..cf582a50df 100644 --- a/Tests/Integration/Controller/SuggestControllerTest.php +++ b/Tests/Integration/Controller/SuggestControllerTest.php @@ -110,6 +110,44 @@ public function canSuggestWithUriSpecialChars(): void } } + #[Test] + public function canSuggestWithMultipleFields(): void + { + $this->importCSVDataSet(__DIR__ . '/Fixtures/suggest_with_multiple_fields.csv'); + + $this->addTypoScriptToTemplateRecord( + 1, + /* @lang TYPO3_TypoScript */ + ' + plugin.tx_solr.suggest.suggestField = title,seo_title_stringS + plugin.tx_solr.index.queue.pages.fields { + seo_title_stringS = SOLR_CONTENT + seo_title_stringS.field = seo_title + } + ' + ); + + $this->indexPages([1, 2, 3, 4]); + + $testCases = [ + [ + 'prefix' => 'Multiple', + 'expected' => 'suggestions":{"multiple":3}', + ], + [ + 'prefix' => 'SEO', + 'expected' => 'suggestions":{"SEO":2}', + ], + [ + 'prefix' => 'different', + 'expected' => 'suggestions":{"different":1}', + ], + ]; + foreach ($testCases as $testCase) { + $this->expectSuggested($testCase['prefix'], $testCase['expected']); + } + } + protected function expectSuggested(string $prefix, string $expected) { $result = (string)($this->executeFrontendSubRequestForSuggestQueryString($prefix, 'rand')->getBody());