From b56a85c6a688b0a06e9bf1d2aaf7c488f0b86d95 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:19:23 +0200 Subject: [PATCH] Extended RediSearch support by implementing FT.SPELLCHECK command (#1162) * add support for CF.ADDNX * fix key name * fix wrong command * Pulling changes * Added support for FT.CREATE command * Fixed tests to choose correct DB * Added test coverage * Revert changes for missing commands * Added data types enums, added methods default assignments * Fixed vector field, removed default assignments, fixed tests * Added constants enum for Sortable argument, renamed arguments object * Codestyle fixes * Rename test class * Added support for FT.DICTADD command * Added support for FT.SPELLCHECK command * Added test group, fixed description --------- Co-authored-by: shacharPash Co-authored-by: Vladyslav Vildanov --- examples/Commands/Search/ft_spellcheck.php | 39 +++++ src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + .../Argument/Search/SpellcheckArguments.php | 59 +++++++ src/Command/Redis/Search/FTSPELLCHECK.php | 38 +++++ .../Search/SpellcheckArgumentsTest.php | 77 +++++++++ .../Command/Redis/Search/FTPROFILE_Test.php | 4 + .../Redis/Search/FTSPELLCHECK_Test.php | 156 ++++++++++++++++++ 8 files changed, 375 insertions(+) create mode 100644 examples/Commands/Search/ft_spellcheck.php create mode 100644 src/Command/Argument/Search/SpellcheckArguments.php create mode 100644 src/Command/Redis/Search/FTSPELLCHECK.php create mode 100644 tests/Predis/Command/Argument/Search/SpellcheckArgumentsTest.php create mode 100644 tests/Predis/Command/Redis/Search/FTSPELLCHECK_Test.php diff --git a/examples/Commands/Search/ft_spellcheck.php b/examples/Commands/Search/ft_spellcheck.php new file mode 100644 index 00000000..188bbc1a --- /dev/null +++ b/examples/Commands/Search/ft_spellcheck.php @@ -0,0 +1,39 @@ +addTextField('text_field'); +$client->ftcreate('index_spellcheck', $schema); + +// 2. Add dictionary with terms +$client->ftdictadd('dict', 'hello', 'help'); + +// 3. Perform spelling correction query +$response = $client->ftspellcheck( + 'index_spellcheck', + 'held', + (new SpellcheckArguments())->distance(2)->terms('dict') +); + +echo 'Response:' . "\n"; +print_r($response); diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 240aa8a1..1059c78f 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -100,6 +100,7 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftinfo(string $index) * @method $this ftprofile(string $index, ProfileArguments $arguments) * @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null) + * @method $this ftspellcheck(string $index, string $query, ?SearchArguments $arguments = null) * @method $this get($key) * @method $this getbit($key, $offset) * @method $this getex(string $key, $modifier = '', $value = false) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index cf143253..033d2bbb 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -109,6 +109,7 @@ use Predis\Response\Status; * @method array ftinfo(string $index) * @method array ftprofile(string $index, ProfileArguments $arguments) * @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null) + * @method array ftspellcheck(string $index, string $query, ?SearchArguments $arguments = null) * @method string|null get(string $key) * @method int getbit(string $key, $offset) * @method int|null getex(string $key, $modifier = '', $value = false) diff --git a/src/Command/Argument/Search/SpellcheckArguments.php b/src/Command/Argument/Search/SpellcheckArguments.php new file mode 100644 index 00000000..7a6c2489 --- /dev/null +++ b/src/Command/Argument/Search/SpellcheckArguments.php @@ -0,0 +1,59 @@ + 'INCLUDE', + 'exclude' => 'EXCLUDE', + ]; + + /** + * Is maximum Levenshtein distance for spelling suggestions (default: 1, max: 4). + * + * @return $this + */ + public function distance(int $distance): self + { + $this->arguments[] = 'DISTANCE'; + $this->arguments[] = $distance; + + return $this; + } + + /** + * Specifies an inclusion (INCLUDE) or exclusion (EXCLUDE) of a custom dictionary named {dict}. + * + * @param string $dictionary + * @param string $modifier + * @param string ...$terms + * @return $this + */ + public function terms(string $dictionary, string $modifier = 'INCLUDE', string ...$terms): self + { + if (!in_array(strtoupper($modifier), $this->termsEnum)) { + $enumValues = implode(', ', array_values($this->termsEnum)); + throw new InvalidArgumentException("Wrong modifier value given. Currently supports: {$enumValues}"); + } + + array_push($this->arguments, 'TERMS', $this->termsEnum[strtolower($modifier)], $dictionary, ...$terms); + + return $this; + } +} diff --git a/src/Command/Redis/Search/FTSPELLCHECK.php b/src/Command/Redis/Search/FTSPELLCHECK.php new file mode 100644 index 00000000..ac0232bb --- /dev/null +++ b/src/Command/Redis/Search/FTSPELLCHECK.php @@ -0,0 +1,38 @@ +toArray(); + } + + parent::setArguments(array_merge( + [$index, $query], + $commandArguments + )); + } +} diff --git a/tests/Predis/Command/Argument/Search/SpellcheckArgumentsTest.php b/tests/Predis/Command/Argument/Search/SpellcheckArgumentsTest.php new file mode 100644 index 00000000..32a126cf --- /dev/null +++ b/tests/Predis/Command/Argument/Search/SpellcheckArgumentsTest.php @@ -0,0 +1,77 @@ +arguments = new SpellcheckArguments(); + } + + /** + * @return void + */ + public function testCreatesArgumentsWithDistanceModifier(): void + { + $this->arguments->distance(2); + + $this->assertSame(['DISTANCE', 2], $this->arguments->toArray()); + } + + /** + * @dataProvider termsProvider + * @param array $arguments + * @param array $expectedResponse + * @return void + */ + public function testCreatesArgumentsWithTermsModifier(array $arguments, array $expectedResponse): void + { + $this->arguments->terms(...$arguments); + + $this->assertSame($expectedResponse, $this->arguments->toArray()); + } + + /** + * @return void + */ + public function testThrowsExceptionOnInvalidTermsModifierValue(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Wrong modifier value given. Currently supports: INCLUDE, EXCLUDE'); + + $this->arguments->terms('dict', 'wrong'); + } + + public function termsProvider(): array + { + return [ + 'with INCLUDE modifier' => [ + ['dict', 'INCLUDE', 'term1', 'term2'], + ['TERMS', 'INCLUDE', 'dict', 'term1', 'term2'], + ], + 'with EXCLUDE modifier' => [ + ['dict', 'EXCLUDE', 'term1', 'term2'], + ['TERMS', 'EXCLUDE', 'dict', 'term1', 'term2'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/Search/FTPROFILE_Test.php b/tests/Predis/Command/Redis/Search/FTPROFILE_Test.php index a41797fc..2b4e16f6 100644 --- a/tests/Predis/Command/Redis/Search/FTPROFILE_Test.php +++ b/tests/Predis/Command/Redis/Search/FTPROFILE_Test.php @@ -17,6 +17,10 @@ use Predis\Command\Argument\Search\Schema; use Predis\Command\Redis\PredisCommandTestCase; use Predis\Response\ServerException; +/** + * @group commands + * @group realm-stack + */ class FTPROFILE_Test extends PredisCommandTestCase { /** diff --git a/tests/Predis/Command/Redis/Search/FTSPELLCHECK_Test.php b/tests/Predis/Command/Redis/Search/FTSPELLCHECK_Test.php new file mode 100644 index 00000000..c6a4d9fc --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FTSPELLCHECK_Test.php @@ -0,0 +1,156 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testSpellcheckReturnsPossibleSuggestionsToGivenMisspelledTerm(): void + { + $redis = $this->getClient(); + $expectedResponse = [['TERM', 'held', [['0', 'hello'], ['0', 'help']]]]; + + $this->assertEquals('OK', $redis->ftcreate( + 'index', + (new Schema())->addTextField('text_field')) + ); + + $this->assertEquals(2, $redis->ftdictadd('dict', 'hello', 'help')); + + $actualResponse = $redis->ftspellcheck( + 'index', + 'held', + (new SpellcheckArguments())->distance(2)->terms('dict') + ); + + $this->assertSame($expectedResponse, $actualResponse); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testThrowsExceptionOnIncorrectTermsModifierGiven(): void + { + $redis = $this->getClient(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Wrong modifier value given. Currently supports: INCLUDE, EXCLUDE'); + + $redis->ftspellcheck( + 'index', + 'held', + (new SpellcheckArguments())->distance(2)->terms('dict', 'wrong') + ); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testThrowsExceptionOnNonExistingIndex(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('Unknown Index name'); + + $redis->ftspellcheck( + 'index', + 'held', + (new SpellcheckArguments())->distance(2)->terms('dict') + ); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + ['index', 'query'], + ['index', 'query'], + ], + 'with DISTANCE modifier' => [ + ['index', 'query', (new SpellcheckArguments())->distance(2)], + ['index', 'query', 'DISTANCE', 2], + ], + 'with TERMS modifier - INCLUDE' => [ + ['index', 'query', (new SpellcheckArguments())->terms('dict', 'INCLUDE', 'term')], + ['index', 'query', 'TERMS', 'INCLUDE', 'dict', 'term'], + ], + 'with TERMS modifier - EXCLUDE' => [ + ['index', 'query', (new SpellcheckArguments())->terms('dict', 'EXCLUDE', 'term')], + ['index', 'query', 'TERMS', 'EXCLUDE', 'dict', 'term'], + ], + 'with DIALECT modifier' => [ + ['index', 'query', (new SpellcheckArguments())->dialect('dialect')], + ['index', 'query', 'DIALECT', 'dialect'], + ], + 'with all arguments' => [ + ['index', 'query', (new SpellcheckArguments())->distance(2)->terms('dict', 'INCLUDE', 'term')->dialect('dialect')], + ['index', 'query', 'DISTANCE', 2, 'TERMS', 'INCLUDE', 'dict', 'term', 'DIALECT', 'dialect'], + ], + ]; + } +}