diff --git a/examples/Commands/Search/ft_tagvals.php b/examples/Commands/Search/ft_tagvals.php new file mode 100644 index 00000000..aea77617 --- /dev/null +++ b/examples/Commands/Search/ft_tagvals.php @@ -0,0 +1,36 @@ +addTagField('tag_field'); +$client->ftcreate('index_tagvals', $schema, (new CreateArguments())->prefix(['prefix:'])); + +// 2. Add indexed tags +$client->hset('prefix:1', 'tag_field', 'Hello, World'); +$client->hset('prefix:2', 'tag_field', 'Hey, World'); + +// 3. Unique tags value query +$response = $client->fttagvals('index_tagvals', 'tag_field'); + +echo 'Response:' . "\n"; +print_r($response); diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index cdfb96b0..a0c06c72 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -104,6 +104,7 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftspellcheck(string $index, string $query, ?SearchArguments $arguments = null) * @method $this ftsyndump(string $index) * @method $this ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms) + * @method $this fttagvals(string $index, string $fieldName) * @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 a4bfe9c8..3622bac4 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -113,6 +113,7 @@ use Predis\Response\Status; * @method array ftspellcheck(string $index, string $query, ?SearchArguments $arguments = null) * @method array ftsyndump(string $index) * @method Status ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms) + * @method array fttagvals(string $index, string $fieldName) * @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/Redis/Search/FTTAGVALS.php b/src/Command/Redis/Search/FTTAGVALS.php new file mode 100644 index 00000000..b323949f --- /dev/null +++ b/src/Command/Redis/Search/FTTAGVALS.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 testReturnIndexedTagFieldDistinctValues(): void + { + $redis = $this->getClient(); + $expectedResponse = ['hello', 'hey', 'world']; + + $this->assertEquals( + 'OK', + $redis->ftcreate( + 'index', + (new Schema())->addTagField('tag_field'), + (new CreateArguments())->prefix(['prefix:']) + ) + ); + + $this->assertSame( + 1, + $redis->hset('prefix:1', 'tag_field', 'Hello, World') + ); + + $this->assertSame( + 1, + $redis->hset('prefix:2', 'tag_field', 'Hey, World') + ); + + $this->assertSame($expectedResponse, $redis->fttagvals('index', 'tag_field')); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.0.0 + */ + public function testThrowsExceptionOnNonExistingIndex(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('Unknown Index name'); + + $redis->fttagvals('index', 'fieldName'); + } +}