Skip to content

Commit e4656bf

Browse files
authored
Support getting scores with suggestions (#79)
1 parent 6c21279 commit e4656bf

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Suggestion.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class Suggestion extends AbstractIndex
66
{
7+
78
/**
89
* Add a suggestion string to an auto-complete suggestion dictionary.
910
* This is disconnected from the index definitions,
@@ -59,10 +60,11 @@ public function length(): int
5960
* @param string $prefix
6061
* @param bool $fuzzy
6162
* @param bool $withPayloads
63+
* @param bool $withScores
6264
* @param int $max
6365
* @return array
6466
*/
65-
public function get(string $prefix, bool $fuzzy = false, bool $withPayloads = false, int $max = -1): array
67+
public function get(string $prefix, bool $fuzzy = false, bool $withPayloads = false, int $max = -1, bool $withScores = false): array
6668
{
6769
$args = [
6870
$this->indexName,
@@ -74,6 +76,9 @@ public function get(string $prefix, bool $fuzzy = false, bool $withPayloads = fa
7476
if ($withPayloads) {
7577
$args[] = 'WITHPAYLOADS';
7678
}
79+
if ($withScores) {
80+
$args[] = 'WITHSCORES';
81+
}
7782
if ($max >= 0) {
7883
$args[] = 'MAX';
7984
$args[] = $max;

tests/RediSearch/SuggestionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,20 @@ public function testShouldGetSuggestion()
8282
$this->assertEquals($expectedFirstResult, $result[0]);
8383
$this->assertEquals($expectedSecondResult, $result[1]);
8484
}
85+
86+
public function testShouldGetSuggestionWithScore()
87+
{
88+
$expectedSuggestion = 'bar';
89+
$expectedScore = '2147483648';
90+
$this->subject->add('bar', 1.23);
91+
$this->subject->add('baz', 24.99);
92+
$this->subject->add('qux', 14.0);
93+
$expectedSizeOfResults = 2;
94+
95+
$result = $this->subject->get('bar', false, false, 1, true);
96+
97+
$this->assertCount($expectedSizeOfResults, $result);
98+
$this->assertEquals($expectedSuggestion, $result[0]);
99+
$this->assertEquals($expectedScore, $result[1]);
100+
}
85101
}

0 commit comments

Comments
 (0)