mirror of
https://github.com/predis/predis.git
synced 2026-09-14 20:37:29 +00:00
Extended RediSearch support by implementing FT.SPELLCHECK command (#1162)
* 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.SPELLCHECK command * Added test group, fixed 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:
committed by
GitHub
parent
c51e4adf4e
commit
b56a85c6a6
@@ -0,0 +1,39 @@
|
||||
<?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;
|
||||
use Predis\Command\Argument\Search\SpellcheckArguments;
|
||||
|
||||
require __DIR__ . '/../../shared.php';
|
||||
|
||||
// Example of FT.SPELLCHECK command usage:
|
||||
|
||||
// 1. Create index
|
||||
$client = new Client();
|
||||
|
||||
$schema = new Schema();
|
||||
$schema->addTextField('text_field');
|
||||
$client->ftcreate('index_spellcheck', $schema);
|
||||
|
||||
// 2. Add dictionary with terms
|
||||
$client->ftdictadd('dict', 'hello', 'help');
|
||||
|
||||
// 3. Perform spelling correction query
|
||||
$response = $client->ftspellcheck(
|
||||
'index_spellcheck',
|
||||
'held',
|
||||
(new SpellcheckArguments())->distance(2)->terms('dict')
|
||||
);
|
||||
|
||||
echo 'Response:' . "\n";
|
||||
print_r($response);
|
||||
@@ -100,6 +100,7 @@ use Predis\Command\Redis\Container\FUNCTIONS;
|
||||
* @method $this ftinfo(string $index)
|
||||
* @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 get($key)
|
||||
* @method $this getbit($key, $offset)
|
||||
* @method $this getex(string $key, $modifier = '', $value = false)
|
||||
|
||||
@@ -109,6 +109,7 @@ use Predis\Response\Status;
|
||||
* @method array ftinfo(string $index)
|
||||
* @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 string|null get(string $key)
|
||||
* @method int getbit(string $key, $offset)
|
||||
* @method int|null getex(string $key, $modifier = '', $value = false)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class SpellcheckArguments extends CommonArguments
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $termsEnum = [
|
||||
'include' => 'INCLUDE',
|
||||
'exclude' => 'EXCLUDE',
|
||||
];
|
||||
|
||||
/**
|
||||
* Is maximum Levenshtein distance for spelling suggestions (default: 1, max: 4).
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function distance(int $distance): self
|
||||
{
|
||||
$this->arguments[] = 'DISTANCE';
|
||||
$this->arguments[] = $distance;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies an inclusion (INCLUDE) or exclusion (EXCLUDE) of a custom dictionary named {dict}.
|
||||
*
|
||||
* @param string $dictionary
|
||||
* @param string $modifier
|
||||
* @param string ...$terms
|
||||
* @return $this
|
||||
*/
|
||||
public function terms(string $dictionary, string $modifier = 'INCLUDE', string ...$terms): self
|
||||
{
|
||||
if (!in_array(strtoupper($modifier), $this->termsEnum)) {
|
||||
$enumValues = implode(', ', array_values($this->termsEnum));
|
||||
throw new InvalidArgumentException("Wrong modifier value given. Currently supports: {$enumValues}");
|
||||
}
|
||||
|
||||
array_push($this->arguments, 'TERMS', $this->termsEnum[strtolower($modifier)], $dictionary, ...$terms);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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;
|
||||
|
||||
class FTSPELLCHECK extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'FT.SPELLCHECK';
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
[$index, $query] = $arguments;
|
||||
$commandArguments = [];
|
||||
|
||||
if (!empty($arguments[2])) {
|
||||
$commandArguments = $arguments[2]->toArray();
|
||||
}
|
||||
|
||||
parent::setArguments(array_merge(
|
||||
[$index, $query],
|
||||
$commandArguments
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SpellcheckArgumentsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var SpellcheckArguments
|
||||
*/
|
||||
private $arguments;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->arguments = new SpellcheckArguments();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithDistanceModifier(): void
|
||||
{
|
||||
$this->arguments->distance(2);
|
||||
|
||||
$this->assertSame(['DISTANCE', 2], $this->arguments->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider termsProvider
|
||||
* @param array $arguments
|
||||
* @param array $expectedResponse
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithTermsModifier(array $arguments, array $expectedResponse): void
|
||||
{
|
||||
$this->arguments->terms(...$arguments);
|
||||
|
||||
$this->assertSame($expectedResponse, $this->arguments->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testThrowsExceptionOnInvalidTermsModifierValue(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Wrong modifier value given. Currently supports: INCLUDE, EXCLUDE');
|
||||
|
||||
$this->arguments->terms('dict', 'wrong');
|
||||
}
|
||||
|
||||
public function termsProvider(): array
|
||||
{
|
||||
return [
|
||||
'with INCLUDE modifier' => [
|
||||
['dict', 'INCLUDE', 'term1', 'term2'],
|
||||
['TERMS', 'INCLUDE', 'dict', 'term1', 'term2'],
|
||||
],
|
||||
'with EXCLUDE modifier' => [
|
||||
['dict', 'EXCLUDE', 'term1', 'term2'],
|
||||
['TERMS', 'EXCLUDE', 'dict', 'term1', 'term2'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,10 @@ use Predis\Command\Argument\Search\Schema;
|
||||
use Predis\Command\Redis\PredisCommandTestCase;
|
||||
use Predis\Response\ServerException;
|
||||
|
||||
/**
|
||||
* @group commands
|
||||
* @group realm-stack
|
||||
*/
|
||||
class FTPROFILE_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
<?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 InvalidArgumentException;
|
||||
use Predis\Command\Argument\Search\Schema;
|
||||
use Predis\Command\Argument\Search\SpellcheckArguments;
|
||||
use Predis\Command\Redis\PredisCommandTestCase;
|
||||
use Predis\Response\ServerException;
|
||||
|
||||
/**
|
||||
* @group commands
|
||||
* @group realm-stack
|
||||
*/
|
||||
class FTSPELLCHECK_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return FTSPELLCHECK::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'FTSPELLCHECK';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.4.0
|
||||
*/
|
||||
public function testSpellcheckReturnsPossibleSuggestionsToGivenMisspelledTerm(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
$expectedResponse = [['TERM', 'held', [['0', 'hello'], ['0', 'help']]]];
|
||||
|
||||
$this->assertEquals('OK', $redis->ftcreate(
|
||||
'index',
|
||||
(new Schema())->addTextField('text_field'))
|
||||
);
|
||||
|
||||
$this->assertEquals(2, $redis->ftdictadd('dict', 'hello', 'help'));
|
||||
|
||||
$actualResponse = $redis->ftspellcheck(
|
||||
'index',
|
||||
'held',
|
||||
(new SpellcheckArguments())->distance(2)->terms('dict')
|
||||
);
|
||||
|
||||
$this->assertSame($expectedResponse, $actualResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRediSearchVersion >= 1.4.0
|
||||
*/
|
||||
public function testThrowsExceptionOnIncorrectTermsModifierGiven(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Wrong modifier value given. Currently supports: INCLUDE, EXCLUDE');
|
||||
|
||||
$redis->ftspellcheck(
|
||||
'index',
|
||||
'held',
|
||||
(new SpellcheckArguments())->distance(2)->terms('dict', 'wrong')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRediSearchVersion >= 1.4.0
|
||||
*/
|
||||
public function testThrowsExceptionOnNonExistingIndex(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage('Unknown Index name');
|
||||
|
||||
$redis->ftspellcheck(
|
||||
'index',
|
||||
'held',
|
||||
(new SpellcheckArguments())->distance(2)->terms('dict')
|
||||
);
|
||||
}
|
||||
|
||||
public function argumentsProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
['index', 'query'],
|
||||
['index', 'query'],
|
||||
],
|
||||
'with DISTANCE modifier' => [
|
||||
['index', 'query', (new SpellcheckArguments())->distance(2)],
|
||||
['index', 'query', 'DISTANCE', 2],
|
||||
],
|
||||
'with TERMS modifier - INCLUDE' => [
|
||||
['index', 'query', (new SpellcheckArguments())->terms('dict', 'INCLUDE', 'term')],
|
||||
['index', 'query', 'TERMS', 'INCLUDE', 'dict', 'term'],
|
||||
],
|
||||
'with TERMS modifier - EXCLUDE' => [
|
||||
['index', 'query', (new SpellcheckArguments())->terms('dict', 'EXCLUDE', 'term')],
|
||||
['index', 'query', 'TERMS', 'EXCLUDE', 'dict', 'term'],
|
||||
],
|
||||
'with DIALECT modifier' => [
|
||||
['index', 'query', (new SpellcheckArguments())->dialect('dialect')],
|
||||
['index', 'query', 'DIALECT', 'dialect'],
|
||||
],
|
||||
'with all arguments' => [
|
||||
['index', 'query', (new SpellcheckArguments())->distance(2)->terms('dict', 'INCLUDE', 'term')->dialect('dialect')],
|
||||
['index', 'query', 'DISTANCE', 2, 'TERMS', 'INCLUDE', 'dict', 'term', 'DIALECT', 'dialect'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user