Skip to content

Commit

Permalink
[FEATURE] Support multiple values in suggestField
Browse files Browse the repository at this point in the history
It is now possible to set multiple fields in suggestField, similar to queryFields, using commas.
  • Loading branch information
saitho committed Oct 23, 2024
1 parent 0fd63e1 commit b6c2be7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Classes/Domain/Search/Query/SuggestQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 13 additions & 2 deletions Classes/Domain/Search/Suggest/SuggestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Documentation/Configuration/Reference/TxSolrSuggest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 38 additions & 0 deletions Tests/Integration/Controller/SuggestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit b6c2be7

Please sign in to comment.