diff --git a/src/Suggestion.php b/src/Suggestion.php index 16aca2f..783996f 100644 --- a/src/Suggestion.php +++ b/src/Suggestion.php @@ -4,6 +4,7 @@ class Suggestion extends AbstractIndex { + /** * Add a suggestion string to an auto-complete suggestion dictionary. * This is disconnected from the index definitions, @@ -59,10 +60,11 @@ public function length(): int * @param string $prefix * @param bool $fuzzy * @param bool $withPayloads + * @param bool $withScores * @param int $max * @return array */ - public function get(string $prefix, bool $fuzzy = false, bool $withPayloads = false, int $max = -1): array + public function get(string $prefix, bool $fuzzy = false, bool $withPayloads = false, int $max = -1, bool $withScores = false): array { $args = [ $this->indexName, @@ -74,6 +76,9 @@ public function get(string $prefix, bool $fuzzy = false, bool $withPayloads = fa if ($withPayloads) { $args[] = 'WITHPAYLOADS'; } + if ($withScores) { + $args[] = 'WITHSCORES'; + } if ($max >= 0) { $args[] = 'MAX'; $args[] = $max; diff --git a/tests/RediSearch/SuggestionTest.php b/tests/RediSearch/SuggestionTest.php index ed874c1..844b3d2 100644 --- a/tests/RediSearch/SuggestionTest.php +++ b/tests/RediSearch/SuggestionTest.php @@ -82,4 +82,20 @@ public function testShouldGetSuggestion() $this->assertEquals($expectedFirstResult, $result[0]); $this->assertEquals($expectedSecondResult, $result[1]); } + + public function testShouldGetSuggestionWithScore() + { + $expectedSuggestion = 'bar'; + $expectedScore = '2147483648'; + $this->subject->add('bar', 1.23); + $this->subject->add('baz', 24.99); + $this->subject->add('qux', 14.0); + $expectedSizeOfResults = 2; + + $result = $this->subject->get('bar', false, false, 1, true); + + $this->assertCount($expectedSizeOfResults, $result); + $this->assertEquals($expectedSuggestion, $result[0]); + $this->assertEquals($expectedScore, $result[1]); + } }