Extended RediSearch support by implementing FT.TAGVALS command (#1166)

* 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.TAGVALS command

* Added example

* Fixed tests

---------

Co-authored-by: shacharPash <shachar.pashchur@redis.com>
Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-02-27 16:21:36 +02:00
committed by GitHub
parent f4db2e7de3
commit 1f4ccbb4fb
5 changed files with 176 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Predis\Client;
use Predis\Command\Argument\Search\CreateArguments;
use Predis\Command\Argument\Search\Schema;
require __DIR__ . '/../../shared.php';
// Example of FT.TAGVALS command usage:
// 1. Create index
$client = new Client();
$schema = new Schema();
$schema->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);
+1
View File
@@ -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)
+1
View File
@@ -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)
+28
View File
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\Search;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/ft.tagvals/
*
* Return a distinct set of values indexed in a Tag field.
*/
class FTTAGVALS extends RedisCommand
{
public function getId()
{
return 'FT.TAGVALS';
}
}
@@ -0,0 +1,110 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\Search;
use Predis\Command\Argument\Search\CreateArguments;
use Predis\Command\Argument\Search\Schema;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
/**
* @group commands
* @group realm-stack
*/
class FTTAGVALS_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return FTTAGVALS::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'FTTAGVALS';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['index', 'fieldName'];
$expectedArguments = ['index', 'fieldName'];
$command = $this->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');
}
}