Skip to content

Commit

Permalink
Support getting scores with suggestions (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanhann authored Mar 26, 2021
1 parent 6c21279 commit e4656bf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Suggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/RediSearch/SuggestionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit e4656bf

Please sign in to comment.