Extended RediSearch support by implementing FT.SYNUPDATE command (#1164)

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

* Added command description

---------

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 14:38:23 +02:00
committed by GitHub
parent b56a85c6a6
commit a0d571d7db
10 changed files with 256 additions and 22 deletions
+31
View File
@@ -0,0 +1,31 @@
<?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\Schema;
require __DIR__ . '/../../shared.php';
// Example of FT.SYNUPDATE command usage:
// 1. Create index
$client = new Client();
$schema = new Schema();
$schema->addTextField('text_field');
$client->ftcreate('index_synupdate', $schema);
// 2. Add synonyms into synonym group
$response = $client->ftsynupdate('index_synupdate', 'synonym1', null, 'term1', 'term2');
echo 'Response:' . "\n";
print_r($response);
+2
View File
@@ -19,6 +19,7 @@ use Predis\Command\Argument\Search\DropArguments;
use Predis\Command\Argument\Search\ProfileArguments;
use Predis\Command\Argument\Search\Schema;
use Predis\Command\Argument\Search\SearchArguments;
use Predis\Command\Argument\Search\SynUpdateArguments;
use Predis\Command\Argument\Server\LimitOffsetCount;
use Predis\Command\Argument\Server\To;
use Predis\Command\CommandInterface;
@@ -101,6 +102,7 @@ use Predis\Command\Redis\Container\FUNCTIONS;
* @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 ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms)
* @method $this get($key)
* @method $this getbit($key, $offset)
* @method $this getex(string $key, $modifier = '', $value = false)
+2
View File
@@ -19,6 +19,7 @@ use Predis\Command\Argument\Search\DropArguments;
use Predis\Command\Argument\Search\ProfileArguments;
use Predis\Command\Argument\Search\Schema;
use Predis\Command\Argument\Search\SearchArguments;
use Predis\Command\Argument\Search\SynUpdateArguments;
use Predis\Command\Argument\Server\LimitOffsetCount;
use Predis\Command\Argument\Server\To;
use Predis\Command\CommandInterface;
@@ -110,6 +111,7 @@ use Predis\Response\Status;
* @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 Status ftsynupdate(string $index, string $synonymGroupId, ?SynUpdateArguments $arguments = null, string ...$terms)
* @method string|null get(string $key)
* @method int getbit(string $key, $offset)
* @method int|null getex(string $key, $modifier = '', $value = false)
@@ -51,6 +51,18 @@ class CommonArguments implements ArrayableArgument
return $this;
}
/**
* If set, does not scan and index.
*
* @return $this
*/
public function skipInitialScan(): self
{
$this->arguments[] = 'SKIPINITIALSCAN';
return $this;
}
/**
* {@inheritDoc}
*/
@@ -214,16 +214,4 @@ class CreateArguments extends CommonArguments
return $this;
}
/**
* If set, does not scan and index.
*
* @return $this
*/
public function skipInitialScan(): self
{
$this->arguments[] = 'SKIPINITIALSCAN';
return $this;
}
}
@@ -0,0 +1,17 @@
<?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\Argument\Search;
class SynUpdateArguments extends CommonArguments
{
}
+46
View File
@@ -0,0 +1,46 @@
<?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.synupdate/
*
* Update a synonym group
*/
class FTSYNUPDATE extends RedisCommand
{
public function getId()
{
return 'FT.SYNUPDATE';
}
public function setArguments(array $arguments)
{
[$index, $synonymGroupId] = $arguments;
$commandArguments = [];
if (!empty($arguments[2])) {
$commandArguments = $arguments[2]->toArray();
}
$terms = array_slice($arguments, 3);
parent::setArguments(array_merge(
[$index, $synonymGroupId],
$commandArguments,
$terms
));
}
}
@@ -45,4 +45,14 @@ class CommonArgumentsTest extends TestCase
$this->assertSame(['DIALECT', 'dialect'], $this->arguments->toArray());
}
/**
* @return void
*/
public function testCreatesArgumentsWithStopInitialScanModifier(): void
{
$this->arguments->skipInitialScan();
$this->assertSame(['SKIPINITIALSCAN'], $this->arguments->toArray());
}
}
@@ -178,16 +178,6 @@ class CreateArgumentsTest extends TestCase
$this->assertSame(['STOPWORDS', 2, 'word1', 'word2'], $this->arguments->toArray());
}
/**
* @return void
*/
public function testCreatesArgumentsWithStopInitialScanModifier(): void
{
$this->arguments->skipInitialScan();
$this->assertSame(['SKIPINITIALSCAN'], $this->arguments->toArray());
}
/**
* @return void
*/
@@ -0,0 +1,136 @@
<?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\Schema;
use Predis\Command\Argument\Search\SynUpdateArguments;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class FTSYNUPDATE_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return FTSYNUPDATE::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'FTSYNUPDATE';
}
/**
* @group disconnected
* @dataProvider argumentsProvider
*/
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
{
$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.2.0
*/
public function testCreatesSynonymGroupWithinGivenIndex(): void
{
$redis = $this->getClient();
$this->assertEquals(
'OK',
$redis->ftcreate('index', (new Schema())->addTextField('text'))
);
$this->assertEquals(
'OK',
$redis->ftsynupdate('index', 'synonym1', null, 'term1', 'term2')
);
}
/**
* @group connected
* @return void
* @requiresRediSearchVersion >= 1.2.0
*/
public function testUpdatesAlreadyExistingSynonymGroupWithinGivenIndex(): void
{
$redis = $this->getClient();
$this->assertEquals(
'OK',
$redis->ftcreate('index', (new Schema())->addTextField('text'))
);
$this->assertEquals(
'OK',
$redis->ftsynupdate('index', 'synonym1', null, 'term1', 'term2')
);
$this->assertEquals(
'OK',
$redis->ftsynupdate('index', 'synonym1', null, 'term3', 'term4')
);
}
/**
* @group connected
* @return void
* @requiresRediSearchVersion >= 1.2.0
*/
public function testThrowsExceptionOnNonExistingIndex(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('Unknown index name');
$redis->ftsynupdate(
'index',
'synonym1',
null,
'term1'
);
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['index', 'synonymGroupId', null, 'term1', 'term2'],
['index', 'synonymGroupId', 'term1', 'term2'],
],
'with SKIPINITIALSCAN modifier' => [
['index', 'synonymGroupId', (new SynUpdateArguments())->skipInitialScan(), 'term1', 'term2'],
['index', 'synonymGroupId', 'SKIPINITIALSCAN', 'term1', 'term2'],
],
];
}
}