Skip to content

Commit 0846801

Browse files
Merge pull request #184 from daniellienert/task/refactor-suggestion-result
!!! TASK: Adjust the result of getSuggestions
2 parents 5cf9036 + 056393a commit 0846801

3 files changed

Lines changed: 54 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public function aggregation($name, array $aggregationDefinition, $parentPath = '
424424
*
425425
* nodes = ${Search....termSuggestions("aTerm")}
426426
*
427-
* Access all suggestions data with {nodes.suggestions} in your fluid template
427+
* Access all suggestions data with ${Search....getSuggestions()}
428428
*
429429
* @param string $text
430430
* @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: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,24 +208,50 @@ public function fieldBasedAggregations()
208208
$this->assertEquals($expectedChickenBucket, $result[$aggregationTitle]['buckets'][0]);
209209
}
210210

211+
public function termSuggestionDataProvider()
212+
{
213+
return [
214+
'singleWord' => [
215+
'term' => 'chickn',
216+
'expectedBestSuggestions' => [
217+
'chicken'
218+
]
219+
],
220+
'multiWord' => [
221+
'term' => 'chickn eggs',
222+
'expectedBestSuggestions' => [
223+
'chicken',
224+
'egg'
225+
]
226+
],
227+
];
228+
}
229+
211230
/**
231+
* @dataProvider termSuggestionDataProvider
212232
* @test
233+
*
234+
* @param string $term
235+
* @param array $expectedBestSuggestions
213236
*/
214-
public function termSuggestion()
237+
public function termSuggestion($term, $expectedBestSuggestions)
215238
{
216-
$titleSuggestionKey = "chickn";
217-
218239
$result = $this->getQueryBuilder()
219240
->log($this->getLogMessagePrefix(__METHOD__))
220-
->termSuggestions($titleSuggestionKey, "title")
241+
->termSuggestions($term, 'title')
221242
->execute()
222243
->getSuggestions();
223244

224-
$this->assertArrayHasKey("options", $result);
225-
226-
$this->assertCount(1, $result['options']);
245+
$this->assertArrayHasKey('suggestions', $result);
246+
$this->assertTrue(is_array($result['suggestions']), 'Suggestions must be an array.');
247+
$this->assertCount(count($expectedBestSuggestions), $result['suggestions']);
227248

228-
$this->assertEquals('chicken', $result['options'][0]['text']);
249+
foreach ($expectedBestSuggestions as $key => $expectedBestSuggestion) {
250+
$suggestion = $result['suggestions'][$key];
251+
$this->assertArrayHasKey('options', $suggestion);
252+
$this->assertCount(1, $suggestion['options']);
253+
$this->assertEquals($expectedBestSuggestion, $suggestion['options'][0]['text']);
254+
}
229255
}
230256

231257
/**

0 commit comments

Comments
 (0)