Extended RediSearch support by implementing FT.EXPLAIN command (#1159)

* 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

* Added support for search command arguments, implemented FT.SEARCH command

* Fixed vector field, removed default assignments, fixed tests

* Tests fixes

* Added constants enum for Sortable argument, renamed arguments object

* Codestyle fixes

* Rename test class

* Merge conflicts

* Added support for FT.EXPLAIN command

* Added FTEXPLAIN_Test to ignored file for eclint

* Codestyle fixes

* Fixed tests, added separate .editorconfig file

---------

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-03-15 10:13:38 +02:00
committed by GitHub
parent bc42b3bcc5
commit 4fefb9bb76
7 changed files with 231 additions and 0 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.EXPLAIN command usage:
// 1. Create index
$client = new Client();
$schema = new Schema();
$schema->addTextField('text_field');
$client->ftcreate('index_explain', $schema);
// 2. Run query explanations
$response = $client->ftexplain('index_explain', '(foo bar)|(hello world) @date:[100 200]|@date:[500 +inf]');
echo 'Response:' . "\n";
print_r($response);
+1
View File
@@ -106,6 +106,7 @@ use Predis\Command\Container\Search\FTCONFIG;
* @method $this ftdictdel(string $dict, ...$term)
* @method $this ftdictdump(string $dict)
* @method $this ftdropindex(string $index, ?DropArguments $arguments = null)
* @method $this ftexplain(string $index, string $query, ?ExplainArguments $arguments = null)
* @method $this ftinfo(string $index)
* @method $this ftprofile(string $index, ProfileArguments $arguments)
* @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null)
+2
View File
@@ -18,6 +18,7 @@ use Predis\Command\Argument\Search\AggregateArguments;
use Predis\Command\Argument\Search\AlterArguments;
use Predis\Command\Argument\Search\CreateArguments;
use Predis\Command\Argument\Search\DropArguments;
use Predis\Command\Argument\Search\ExplainArguments;
use Predis\Command\Argument\Search\ProfileArguments;
use Predis\Command\Argument\Search\SchemaFields\FieldInterface;
use Predis\Command\Argument\Search\SearchArguments;
@@ -115,6 +116,7 @@ use Predis\Response\Status;
* @method int ftdictdel(string $dict, ...$term)
* @method array ftdictdump(string $dict)
* @method Status ftdropindex(string $index, ?DropArguments $arguments = null)
* @method string ftexplain(string $index, string $query, ?ExplainArguments $arguments = null)
* @method array ftinfo(string $index)
* @method array ftprofile(string $index, ProfileArguments $arguments)
* @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null)
@@ -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 ExplainArguments extends CommonArguments
{
}
+43
View File
@@ -0,0 +1,43 @@
<?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.explain/
*
* Return the execution plan for a complex query.
*/
class FTEXPLAIN extends RedisCommand
{
public function getId()
{
return 'FT.EXPLAIN';
}
public function setArguments(array $arguments)
{
[$index, $query] = $arguments;
$commandArguments = [];
if (!empty($arguments[2])) {
$commandArguments = $arguments[2]->toArray();
}
parent::setArguments(array_merge(
[$index, $query],
$commandArguments
));
}
}
+2
View File
@@ -0,0 +1,2 @@
[Predis/Command/Redis/Search/FTEXPLAIN_Test.php]
indent_size = unset
@@ -0,0 +1,135 @@
<?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\ExplainArguments;
use Predis\Command\Argument\Search\SchemaFields\TextField;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class FTEXPLAIN_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return FTEXPLAIN::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'FTEXPLAIN';
}
/**
* @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.0.0
*/
public function testExplainReturnsExecutionPlanForGivenQuery(): void
{
$redis = $this->getClient();
$expectedResponse = <<<EOT
INTERSECT {
UNION {
INTERSECT {
UNION {
foo
+foo(expanded)
}
UNION {
bar
+bar(expanded)
}
}
INTERSECT {
UNION {
hello
+hello(expanded)
}
UNION {
world
+world(expanded)
}
}
}
UNION {
NUMERIC {100.000000 <= @date <= 200.000000}
NUMERIC {500.000000 <= @date <= inf}
}
}
EOT;
$schema = [new TextField('text_field')];
$this->assertEquals('OK', $redis->ftcreate('index', $schema));
$this->assertEquals(
$expectedResponse,
$redis->ftexplain('index', '(foo bar)|(hello world) @date:[100 200]|@date:[500 +inf]')
);
}
/**
* @group connected
* @return void
* @requiresRediSearchVersion >= 1.0.0
*/
public function testThrowsExceptionOnNonExistingIndex(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('index: no such index');
$redis->ftexplain('index', 'query');
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['index', 'query', null],
['index', 'query'],
],
'with DIALECT' => [
['index', 'query', (new ExplainArguments())->dialect('dialect')],
['index', 'query', 'DIALECT', 'dialect'],
],
];
}
}