Skip to content

Commit 4bc6951

Browse files
committed
!!! TASK: Adjust the result of getSuggestions
Before the result of getSuggestions returned the suggestion of a search term if the term only consisted of one word. When the search term conisted of two words it returned the complete array. This is an absolutely unexpected behaviour. It was also only possible to acces the first defined suggest term. This change now reurns the complete suggestion array, so it behaves like the comparable aggregation getter.
1 parent 1b79d2b commit 4bc6951

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public function aggregation($name, array $aggregationDefinition, $parentPath = n
422422
*
423423
* nodes = ${Search....termSuggestions("aTerm")}
424424
*
425-
* Access all suggestions data with {nodes.suggestions} in your fluid template
425+
* Access all suggestions data with ${Search....getSuggestions()}
426426
*
427427
* @param string $text
428428
* @param string $field

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryResult.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,32 @@ public function getAggregations()
208208
}
209209

210210
/**
211+
* Returns an array of type
212+
* [
213+
* <suggestionName> => [
214+
* 'text' => <term>
215+
* 'options' => [
216+
* [
217+
* 'text' => <suggestionText>
218+
* 'score' => <score>
219+
* ],
220+
* [
221+
* ...
222+
* ]
223+
* ]
224+
* ]
225+
* ]
226+
*
211227
* @return array
212228
*/
213229
public function getSuggestions()
214230
{
215231
$this->initialize();
216-
if (count($this->result['suggest']) === 1) {
217-
$suggestArray = current($this->result['suggest']);
218-
219-
if (count($suggestArray) === 1) {
220-
return current($suggestArray);
221-
}
232+
if (array_key_exists('suggest', $this->result)) {
233+
return $this->result['suggest'];
222234
}
223235

224-
return $this->result['suggest'];
236+
return [];
225237
}
226238

227239
/**

Tests/Functional/Eel/ElasticSearchQueryTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,23 @@ public function fieldBasedAggregations()
213213
*/
214214
public function termSuggestion()
215215
{
216-
$titleSuggestionKey = "chickn";
216+
$titleSuggestionKey = 'chickn';
217217

218218
$result = $this->getQueryBuilder()
219219
->log($this->getLogMessagePrefix(__METHOD__))
220-
->termSuggestions($titleSuggestionKey, "title")
220+
->termSuggestions($titleSuggestionKey, 'title')
221221
->execute()
222222
->getSuggestions();
223223

224-
$this->assertArrayHasKey("options", $result);
224+
$this->assertArrayHasKey('suggestions', $result);
225+
$this->assertTrue(is_array($result['suggestions']), 'Suggestions must be an array.');
226+
$firstSuggestion = current($result['suggestions']);
225227

226-
$this->assertCount(1, $result['options']);
228+
$this->assertArrayHasKey('options', $firstSuggestion);
227229

228-
$this->assertEquals('chicken', $result['options'][0]['text']);
230+
$this->assertCount(1, $firstSuggestion['options']);
231+
232+
$this->assertEquals('chicken', $firstSuggestion['options'][0]['text']);
229233
}
230234

231235
/**

0 commit comments

Comments
 (0)