From 47d0da845ab5d9e9c26be05452c15705e755e18b Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 23 Feb 2023 14:22:53 +0200 Subject: [PATCH 1/4] Extended RediSearch support by implementing FT.DICTADD command (#1155) * 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 --------- Co-authored-by: shacharPash Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/Search/FTDICTADD.php | 28 ++++++++ .../Command/Redis/Search/FTDICTADD_Test.php | 70 +++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 src/Command/Redis/Search/FTDICTADD.php create mode 100644 tests/Predis/Command/Redis/Search/FTDICTADD_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index d0c154c1..02a1184f 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -91,6 +91,7 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftaliasdel(string $alias) * @method $this ftaliasupdate(string $alias, string $index) * @method $this ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) + * @method $this ftdictadd(string $dict, ...$term) * @method $this ftinfo(string $index) * @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method $this get($key) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 93dc47b3..1da2f51b 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -100,6 +100,7 @@ use Predis\Response\Status; * @method Status ftaliasdel(string $alias) * @method Status ftaliasupdate(string $alias, string $index) * @method Status ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) + * @method int ftdictadd(string $dict, ...$term) * @method array ftinfo(string $index) * @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method string|null get(string $key) diff --git a/src/Command/Redis/Search/FTDICTADD.php b/src/Command/Redis/Search/FTDICTADD.php new file mode 100644 index 00000000..c0bc3da7 --- /dev/null +++ b/src/Command/Redis/Search/FTDICTADD.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.4.0 + */ + public function testAddTermsIntoGivenDictionary(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->ftdictadd('dict', 'foo', 'bar'); + + $this->assertSame(2, $actualResponse); + } +} From 16844693029d19867dd78df6300a29b2b1a6bafa Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 23 Feb 2023 14:33:41 +0200 Subject: [PATCH 2/4] Extended RediSearch support by implementing FT.DICTDEL support (#1156) * 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.DICTDEL command --------- Co-authored-by: shacharPash Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/Search/FTDICTDEL.php | 28 ++++++ .../Command/Redis/Search/FTDICTDEL_Test.php | 98 +++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 src/Command/Redis/Search/FTDICTDEL.php create mode 100644 tests/Predis/Command/Redis/Search/FTDICTDEL_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 02a1184f..1d49b2f7 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -92,6 +92,7 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftaliasupdate(string $alias, string $index) * @method $this ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) * @method $this ftdictadd(string $dict, ...$term) + * @method $this ftdictdel(string $dict, ...$term) * @method $this ftinfo(string $index) * @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method $this get($key) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 1da2f51b..2c4e29bf 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -101,6 +101,7 @@ use Predis\Response\Status; * @method Status ftaliasupdate(string $alias, string $index) * @method Status ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) * @method int ftdictadd(string $dict, ...$term) + * @method int ftdictdel(string $dict, ...$term) * @method array ftinfo(string $index) * @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method string|null get(string $key) diff --git a/src/Command/Redis/Search/FTDICTDEL.php b/src/Command/Redis/Search/FTDICTDEL.php new file mode 100644 index 00000000..19e0633b --- /dev/null +++ b/src/Command/Redis/Search/FTDICTDEL.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 + * @dataProvider dictionariesProvider + * @param array $addArguments + * @param array $deleteArguments + * @param int $expectedResponse + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testRemovesTermsFromGivenDictionary( + array $addArguments, + array $deleteArguments, + int $expectedResponse + ): void { + $redis = $this->getClient(); + + $redis->ftdictadd(...$addArguments); + + $this->assertSame($expectedResponse, $redis->ftdictdel(...$deleteArguments)); + } + + public function dictionariesProvider(): array + { + return [ + 'removes existing term' => [ + ['dict', 'foo', 'bar'], + ['dict', 'foo'], + 1, + ], + 'removes non-existing term' => [ + ['dict', 'foo', 'bar'], + ['dict', 'baz'], + 0, + ], + 'removes from non-existing dict' => [ + ['dict', 'foo', 'bar'], + ['dict123', 'baz'], + 0, + ], + ]; + } +} From 736370c0bc86076f7f4bd4e28cb4cd7854bd7afc Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:00:37 +0200 Subject: [PATCH 3/4] Extended RediSearch support by implementing FT.DICTDUMP command (#1157) * 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.DICTDUMP command * Codestyle fixes --------- Co-authored-by: shacharPash Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/Search/FTDICTDUMP.php | 28 ++++++ .../Command/Redis/Search/FTDICTDUMP_Test.php | 86 +++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 src/Command/Redis/Search/FTDICTDUMP.php create mode 100644 tests/Predis/Command/Redis/Search/FTDICTDUMP_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 1d49b2f7..0f1f4074 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -93,6 +93,7 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) * @method $this ftdictadd(string $dict, ...$term) * @method $this ftdictdel(string $dict, ...$term) + * @method $this ftdictdump(string $dict) * @method $this ftinfo(string $index) * @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method $this get($key) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 2c4e29bf..6d4b0ef0 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -102,6 +102,7 @@ use Predis\Response\Status; * @method Status ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) * @method int ftdictadd(string $dict, ...$term) * @method int ftdictdel(string $dict, ...$term) + * @method array ftdictdump(string $dict) * @method array ftinfo(string $index) * @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method string|null get(string $key) diff --git a/src/Command/Redis/Search/FTDICTDUMP.php b/src/Command/Redis/Search/FTDICTDUMP.php new file mode 100644 index 00000000..b8282b56 --- /dev/null +++ b/src/Command/Redis/Search/FTDICTDUMP.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.4.0 + */ + public function testDumpTermsFromGivenDictionary(): void + { + $redis = $this->getClient(); + + $redis->ftdictadd('dict', 'foo', 'bar'); + + $this->assertSame(['bar', 'foo'], $redis->ftdictdump('dict')); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testThrowsExceptionOnNonExistingDictionary(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('could not open dict key'); + + $redis->ftdictdump('dict'); + } +} From 4d71c7011a703440f5e12303733dcf4509cef8e9 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:45:02 +0200 Subject: [PATCH 4/4] Extended RediSearch support by implementing FT.DROPINDEX command (#1158) * 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.DROPINDEX command * Changed arguments handling --------- Co-authored-by: shacharPash Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + src/Command/Argument/Search/DropArguments.php | 43 ++++++++ src/Command/Redis/Search/FTDROPINDEX.php | 38 +++++++ .../Argument/Search/DropArgumentsTest.php | 38 +++++++ .../Command/Redis/Search/FTDROPINDEX_Test.php | 102 ++++++++++++++++++ 6 files changed, 225 insertions(+) create mode 100644 src/Command/Argument/Search/DropArguments.php create mode 100644 src/Command/Redis/Search/FTDROPINDEX.php create mode 100644 tests/Predis/Command/Argument/Search/DropArgumentsTest.php create mode 100644 tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 0f1f4074..b5a7a9a5 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -15,6 +15,7 @@ namespace Predis; use Predis\Command\Argument\Geospatial\ByInterface; use Predis\Command\Argument\Geospatial\FromInterface; use Predis\Command\Argument\Search\CreateArguments; +use Predis\Command\Argument\Search\DropArguments; use Predis\Command\Argument\Search\Schema; use Predis\Command\Argument\Search\SearchArguments; use Predis\Command\Argument\Server\LimitOffsetCount; @@ -94,6 +95,7 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftdictadd(string $dict, ...$term) * @method $this ftdictdel(string $dict, ...$term) * @method $this ftdictdump(string $dict) + * @method $this ftdropindex(string $index, ?DropArguments $arguments = null) * @method $this ftinfo(string $index) * @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method $this get($key) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 6d4b0ef0..eef472e7 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -15,6 +15,7 @@ namespace Predis; use Predis\Command\Argument\Geospatial\ByInterface; use Predis\Command\Argument\Geospatial\FromInterface; use Predis\Command\Argument\Search\CreateArguments; +use Predis\Command\Argument\Search\DropArguments; use Predis\Command\Argument\Search\Schema; use Predis\Command\Argument\Search\SearchArguments; use Predis\Command\Argument\Server\LimitOffsetCount; @@ -103,6 +104,7 @@ use Predis\Response\Status; * @method int ftdictadd(string $dict, ...$term) * @method int ftdictdel(string $dict, ...$term) * @method array ftdictdump(string $dict) + * @method Status ftdropindex(string $index, ?DropArguments $arguments = null) * @method array ftinfo(string $index) * @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method string|null get(string $key) diff --git a/src/Command/Argument/Search/DropArguments.php b/src/Command/Argument/Search/DropArguments.php new file mode 100644 index 00000000..0c631320 --- /dev/null +++ b/src/Command/Argument/Search/DropArguments.php @@ -0,0 +1,43 @@ +arguments[] = 'DD'; + + return $this; + } + + /** + * @return array + */ + public function toArray(): array + { + return $this->arguments; + } +} diff --git a/src/Command/Redis/Search/FTDROPINDEX.php b/src/Command/Redis/Search/FTDROPINDEX.php new file mode 100644 index 00000000..0da330fa --- /dev/null +++ b/src/Command/Redis/Search/FTDROPINDEX.php @@ -0,0 +1,38 @@ +toArray(); + } + + parent::setArguments(array_merge( + [$index], + $commandArguments + )); + } +} diff --git a/tests/Predis/Command/Argument/Search/DropArgumentsTest.php b/tests/Predis/Command/Argument/Search/DropArgumentsTest.php new file mode 100644 index 00000000..c843013a --- /dev/null +++ b/tests/Predis/Command/Argument/Search/DropArgumentsTest.php @@ -0,0 +1,38 @@ +arguments = new DropArguments(); + } + + /** + * @return void + */ + public function testCreatesArgumentsWithLanguageModifier(): void + { + $this->arguments->dd(); + + $this->assertSame(['DD'], $this->arguments->toArray()); + } +} diff --git a/tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php b/tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php new file mode 100644 index 00000000..a0e2032c --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php @@ -0,0 +1,102 @@ +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 >= 2.0.0 + */ + public function testDropRemovesGivenIndex(): void + { + $redis = $this->getClient(); + + $schema = new Schema(); + $schema->addTextField('text_field'); + + $this->assertEquals('OK', $redis->ftcreate('index', $schema)); + $this->assertEquals('OK', $redis->ftdropindex('index')); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 2.0.0 + */ + public function testThrowsExceptionOnNonExistingIndex(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('Unknown Index name'); + + $redis->ftdropindex('index'); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + ['index'], + ['index'], + ], + 'with DD modifier' => [ + ['index', (new DropArguments())->dd()], + ['index', 'DD'], + ], + ]; + } +}