From 00a8e5cd612c418ed4bff08a01eb7de1452c2d44 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Wed, 1 Mar 2023 13:19:19 +0200 Subject: [PATCH] Extended RediSearch support by implementing FT.SUGLEN command (#1186) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added support for new arguments for BITPOS, BITCOUNT commands (#1045) * Added support for new arguments for EXPIRE, EXPIREAT commands (#1046) * Extended core support by implementing SORT_RO command (#1044) * Added support for SORT_RO command * Codestyle fixes * Added command description --------- Co-authored-by: Vladyslav Vildanov * fix deprecated call * Added support for container commands (#1049) * Added support for container commands FUNCTION LOAD, FUNCTION DELETE and FCALL * Changed ContainerInterface and AbstractContainer * Re-implement logic of abstract methods --------- Co-authored-by: Vladyslav Vildanov * Added stream commands to KeyPrefixProcessor (#1051) Co-authored-by: Vladyslav Vildanov * Fix return type of ReplicationInterface::getSlaves (#1111) * Codestyle fixes * Changed return annotation * Added support for FT.SUGLEN command --------- Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss Co-authored-by: Stephan --- .codespellrc | 2 +- ...get_del.php => ft_sug_add_get_del_len.php} | 4 +- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/Search/FTSUGLEN.php | 28 ++++++++ .../Command/Redis/Search/FTSUGLEN_Test.php | 71 +++++++++++++++++++ 6 files changed, 105 insertions(+), 2 deletions(-) rename examples/Commands/Search/{ft_sug_add_get_del.php => ft_sug_add_get_del_len.php} (87%) create mode 100644 src/Command/Redis/Search/FTSUGLEN.php create mode 100644 tests/Predis/Command/Redis/Search/FTSUGLEN_Test.php diff --git a/.codespellrc b/.codespellrc index 58df45a2..101bf239 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,5 +1,5 @@ [codespell] -skip=./.git,./src/Command/Redis/Search/FTSUGGET.php,./examples/Commands/Search/ft_sug_add_get_del.php +skip=./.git,./src/Command/Redis/Search/FTSUGGET.php,./examples/Commands/Search/ft_sug_add_get_del_len.php check-hidden= check-filenames= builtin=clear,rare,informal,usage,code,names diff --git a/examples/Commands/Search/ft_sug_add_get_del.php b/examples/Commands/Search/ft_sug_add_get_del_len.php similarity index 87% rename from examples/Commands/Search/ft_sug_add_get_del.php rename to examples/Commands/Search/ft_sug_add_get_del_len.php index d58e1b57..67576986 100644 --- a/examples/Commands/Search/ft_sug_add_get_del.php +++ b/examples/Commands/Search/ft_sug_add_get_del_len.php @@ -16,13 +16,15 @@ use Predis\Command\Argument\Search\SugGetArguments; require __DIR__ . '/../../shared.php'; -// Example of FT.SUGADD, FT.SUGGET, FT.SUGDEL commands usage: +// Example of FT.SUGADD, FT.SUGGET, FT.SUGDEL, FT.SUGLEN commands usage: // 1. Add suggestion to key with payload $client = new Client(); $client->ftsugadd('key', 'hello', 2, (new SugAddArguments())->payload('payload')); +echo 'Suggestions dictionary length: ' . $client->ftsuglen('key') . "\n"; + // 2. Perform fuzzy search by prefix to get previous suggestion with payload $response = $client->ftsugget('key', 'hellp', (new SugGetArguments())->fuzzy()->withPayloads()); diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 68a25580..213b12b5 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -111,6 +111,7 @@ use Predis\Command\Container\Search\FTCONFIG; * @method $this ftsugadd(string $key, string $string, float $score, ?SugAddArguments $arguments = null) * @method $this ftsugdel(string $key, string $string) * @method $this ftsugget(string $key, string $prefix, ?SugGetArguments $arguments = null) + * @method $this ftsuglen(string $key) * @method $this ftsyndump(string $index) * @method $this ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms) * @method $this fttagvals(string $index, string $fieldName) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 7bd6e412..e7fa73a1 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -120,6 +120,7 @@ use Predis\Response\Status; * @method int ftsugadd(string $key, string $string, float $score, ?SugAddArguments $arguments = null) * @method int ftsugdel(string $key, string $string) * @method array ftsugget(string $key, string $prefix, ?SugGetArguments $arguments = null) + * @method int ftsuglen(string $key) * @method array ftsyndump(string $index) * @method Status ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms) * @method array fttagvals(string $index, string $fieldName) diff --git a/src/Command/Redis/Search/FTSUGLEN.php b/src/Command/Redis/Search/FTSUGLEN.php new file mode 100644 index 00000000..1e11d8d3 --- /dev/null +++ b/src/Command/Redis/Search/FTSUGLEN.php @@ -0,0 +1,28 @@ +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.0.0 + */ + public function testReturnsLengthOfGivenSuggestionDictionary(): void + { + $redis = $this->getClient(); + + $redis->ftsugadd('key', 'foo', 1); + $redis->ftsugadd('key', 'bar', 1); + + $this->assertSame(2, $redis->ftsuglen('key')); + } +}