Skip to content

Commit 6a4f832

Browse files
committed
Fix IndexBuilder + Improve README + More tests
1 parent 8ebffb4 commit 6a4f832

File tree

6 files changed

+462
-78
lines changed

6 files changed

+462
-78
lines changed

README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[MacFJA/redisearch](https://packagist.org/packages/macfja/redisearch) is a PHP Client for [RediSearch](https://oss.redislabs.com/redisearch/).
44

5-
The implemented API is for RediSearch 2.0
5+
The implemented API is for RediSearch 2.x
66

77
## Installation
88

@@ -25,7 +25,7 @@ This lib can use several connector for Redis:
2525
- [Redisent](https://github.com/jdp/redisent) - Pure PHP implementation
2626
- [TinyRedis](https://github.com/ptrofimov/tinyredisclient) - Pure PHP implementation
2727

28-
You can pick the connector depending of your need.
28+
You can pick the connector depending on your need.
2929

3030
```php
3131
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
@@ -70,14 +70,14 @@ $clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);
7070
$client = /* ... */;
7171
$builder = new \MacFJA\RediSearch\IndexBuilder();
7272

73-
// Field can be create in advance
73+
// Field can be created in advance
7474
$address = (new \MacFJA\RediSearch\Redis\Command\CreateCommand\GeoFieldOption())
7575
->setField('address');
7676

7777
$builder
7878
->setIndex('person')
7979
->addField($address)
80-
// Or field can be create "inline"
80+
// Or field can be created "inline"
8181
->addTextField('lastname', false, null, null, true)
8282
->addTextField('firstname')
8383
->addNumericField('age')
@@ -186,7 +186,17 @@ $result = $client->pipeline($search, $stats, $aggregate, $suggestion);
186186

187187
```php
188188
$client = new \Predis\Client(/* ... */);
189-
\MacFJA\RediSearch\Redis\Initializer::registerCommands($client->getProfile());
189+
\MacFJA\RediSearch\Redis\Initializer::registerCommandsPredis($client->getProfile());
190+
191+
$client->ftsearch('people', '@age:[(17 +inf] %john%');
192+
// But you will have raw Redis output.
193+
```
194+
195+
### Use Rediska shorthand syntax
196+
197+
```php
198+
$client = new \Rediska(/* ... */);
199+
\MacFJA\RediSearch\Redis\Initializer::registerCommandsRediska();
190200

191201
$client->ftsearch('people', '@age:[(17 +inf] %john%');
192202
// But you will have raw Redis output.

src/IndexBuilder.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
* @method IndexBuilder withStructure(string $type)
4545
* @method IndexBuilder setPrefixes(array $prefixes)
4646
* @method IndexBuilder withPrefixes(array $prefixes)
47-
* @method IndexBuilder addPrefix(string ...$prefixes)
48-
* @method IndexBuilder withAddedPrefix(string ...$prefixes)
47+
* @method IndexBuilder addPrefixes(string ...$prefixes)
48+
* @method IndexBuilder withAddedPrefixes(string ...$prefixes)
4949
* @method IndexBuilder setFilter(string $filter)
5050
* @method IndexBuilder withFilter(string $filter)
5151
* @method IndexBuilder setDefaultLanguage(string $language)
@@ -74,11 +74,12 @@
7474
* @method IndexBuilder withSkipInitialScan(bool $active)
7575
* @method IndexBuilder setStopWords(array $words)
7676
* @method IndexBuilder withStopWords(array $words)
77-
* @method IndexBuilder addStopWord(string ...$words)
78-
* @method IndexBuilder withAddedStopWord(string ...$words)
77+
* @method IndexBuilder addStopWords(string ...$words)
78+
* @method IndexBuilder withAddedStopWords(string ...$words)
7979
* @method IndexBuilder setFields(array $fields)
8080
* @method IndexBuilder withFields(array $fields)
81-
* @method IndexBuilder withAddedField(CreateCommandFieldOption ...$fields)
81+
* @method IndexBuilder addFields(CreateCommandFieldOption ...$fields)
82+
* @method IndexBuilder withAddedFields(CreateCommandFieldOption ...$fields)
8283
* @method IndexBuilder withAddedTextField(string $name, bool $noStem = false, ?float $weight = null, ?string $phonetic = null, bool $sortable = false, bool $noIndex = false)
8384
* @method IndexBuilder withAddedNumericField(string $name, bool $sortable = false, bool $noIndex = false)
8485
* @method IndexBuilder withAddedGeoField(string $name, bool $noIndex = false)
@@ -153,16 +154,9 @@ public function setNoStopWords(): self
153154
return $this;
154155
}
155156

156-
public function withNoStopWords(): self
157+
public function addField(CreateCommandFieldOption $option): self
157158
{
158-
$newInstance = clone $this;
159-
160-
return $newInstance->setNoStopWords();
161-
}
162-
163-
public function addField(CreateCommandFieldOption ...$option): self
164-
{
165-
array_push($this->fields, ...$option);
159+
$this->fields[] = $option;
166160

167161
return $this;
168162
}
@@ -278,7 +272,7 @@ private function withAddedCall(string $name, array $arguments): self
278272
{
279273
$newInstance = clone $this;
280274

281-
return $newInstance->{'add'.$name}($arguments);
275+
return $newInstance->{'add'.$name}(...$arguments);
282276
}
283277

284278
/**
@@ -319,6 +313,9 @@ private function addCall(string $name, array $arguments): self
319313
throw new InvalidArgumentException(sprintf('add%s method need at least one parameter', $name));
320314
}
321315

316+
if (null === $this->{$propertyName}) {
317+
$this->{$propertyName} = [];
318+
}
322319
array_push($this->{$propertyName}, ...$arguments);
323320

324321
return $this;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\Redis\Client\Rediska;
23+
24+
use function count;
25+
use MacFJA\RediSearch\Redis\Client\AbstractClient;
26+
use Rediska_Command_Abstract;
27+
use Rediska_Connection_Exec;
28+
29+
class RediskaRediSearchCommand extends Rediska_Command_Abstract
30+
{
31+
public function create(): Rediska_Connection_Exec
32+
{
33+
$connections = $this->_rediska->getConnections();
34+
35+
if (false === AbstractClient::$disableNotice && count($connections) > 1) {
36+
trigger_error('Warning, Multiple redis connections exists, only the first connection will be used', E_USER_NOTICE);
37+
}
38+
$connection = reset($connections);
39+
40+
$commands = $this->_arguments;
41+
if (!('__redisearch' === $this->_name)) {
42+
array_unshift($commands, $this->_name);
43+
}
44+
45+
return new Rediska_Connection_Exec($connection, $commands);
46+
}
47+
}

src/Redis/Initializer.php

+29-57
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
namespace MacFJA\RediSearch\Redis;
2323

24-
use function count;
25-
use MacFJA\RediSearch\Redis\Client\AbstractClient;
24+
use MacFJA\RediSearch\Redis\Client\Rediska\RediskaRediSearchCommand;
2625
use MacFJA\RediSearch\Redis\Command\Aggregate;
2726
use MacFJA\RediSearch\Redis\Command\AliasAdd;
2827
use MacFJA\RediSearch\Redis\Command\AliasDel;
@@ -51,34 +50,7 @@
5150
use MacFJA\RediSearch\Redis\Command\SynUpdate;
5251
use MacFJA\RediSearch\Redis\Command\TagVals;
5352
use Predis\Profile\RedisProfile;
54-
use Rediska_Command_Abstract;
5553
use Rediska_Commands;
56-
use Rediska_Connection_Exec;
57-
58-
if (class_exists(Rediska_Command_Abstract::class) && class_exists(Rediska_Connection_Exec::class)) {
59-
/**
60-
* @codeCoverageIgnore
61-
*/
62-
class RediskaRediSearch extends Rediska_Command_Abstract
63-
{
64-
public function create(): Rediska_Connection_Exec
65-
{
66-
$connections = $this->_rediska->getConnections();
67-
68-
if (false === AbstractClient::$disableNotice && count($connections) > 1) {
69-
trigger_error('Warning, Multiple redis connections exists, only the first connection will be used', E_USER_NOTICE);
70-
}
71-
$connection = reset($connections);
72-
73-
$commands = $this->_arguments;
74-
if (!('__redisearch' === $this->_name)) {
75-
array_unshift($commands, $this->_name);
76-
}
77-
78-
return new Rediska_Connection_Exec($connection, $commands);
79-
}
80-
}
81-
}
8254

8355
/**
8456
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -124,34 +96,34 @@ public static function registerCommandsPredis(RedisProfile $profile): void
12496
*/
12597
public static function registerCommandsRediska(): void
12698
{
127-
Rediska_Commands::add('ftaggregate', RediskaRediSearch::class);
128-
Rediska_Commands::add('ftaliasadd', RediskaRediSearch::class);
129-
Rediska_Commands::add('ftaliasdel', RediskaRediSearch::class);
130-
Rediska_Commands::add('ftaliasupdate', RediskaRediSearch::class);
131-
Rediska_Commands::add('ftalter', RediskaRediSearch::class);
132-
Rediska_Commands::add('ftconfig', RediskaRediSearch::class);
133-
Rediska_Commands::add('ftconfigset', RediskaRediSearch::class);
134-
Rediska_Commands::add('ftcreate', RediskaRediSearch::class);
135-
Rediska_Commands::add('ftcursordel', RediskaRediSearch::class);
136-
Rediska_Commands::add('ftcursorread', RediskaRediSearch::class);
137-
Rediska_Commands::add('ftdictadd', RediskaRediSearch::class);
138-
Rediska_Commands::add('ftdictdel', RediskaRediSearch::class);
139-
Rediska_Commands::add('ftdictdump', RediskaRediSearch::class);
140-
Rediska_Commands::add('ftdropindex', RediskaRediSearch::class);
141-
Rediska_Commands::add('ftexplain', RediskaRediSearch::class);
142-
Rediska_Commands::add('ftexplaincli', RediskaRediSearch::class);
143-
Rediska_Commands::add('ftlist', RediskaRediSearch::class);
144-
Rediska_Commands::add('ftinfo', RediskaRediSearch::class);
145-
Rediska_Commands::add('ftsearch', RediskaRediSearch::class);
146-
Rediska_Commands::add('ftspellcheck', RediskaRediSearch::class);
147-
Rediska_Commands::add('ftsugadd', RediskaRediSearch::class);
148-
Rediska_Commands::add('ftsugdel', RediskaRediSearch::class);
149-
Rediska_Commands::add('ftsugget', RediskaRediSearch::class);
150-
Rediska_Commands::add('ftsuglen', RediskaRediSearch::class);
151-
Rediska_Commands::add('ftsyndump', RediskaRediSearch::class);
152-
Rediska_Commands::add('ftsynupdate', RediskaRediSearch::class);
153-
Rediska_Commands::add('fttagvals', RediskaRediSearch::class);
154-
Rediska_Commands::add('__redisearch', RediskaRediSearch::class);
99+
Rediska_Commands::add('ftaggregate', RediskaRediSearchCommand::class);
100+
Rediska_Commands::add('ftaliasadd', RediskaRediSearchCommand::class);
101+
Rediska_Commands::add('ftaliasdel', RediskaRediSearchCommand::class);
102+
Rediska_Commands::add('ftaliasupdate', RediskaRediSearchCommand::class);
103+
Rediska_Commands::add('ftalter', RediskaRediSearchCommand::class);
104+
Rediska_Commands::add('ftconfig', RediskaRediSearchCommand::class);
105+
Rediska_Commands::add('ftconfigset', RediskaRediSearchCommand::class);
106+
Rediska_Commands::add('ftcreate', RediskaRediSearchCommand::class);
107+
Rediska_Commands::add('ftcursordel', RediskaRediSearchCommand::class);
108+
Rediska_Commands::add('ftcursorread', RediskaRediSearchCommand::class);
109+
Rediska_Commands::add('ftdictadd', RediskaRediSearchCommand::class);
110+
Rediska_Commands::add('ftdictdel', RediskaRediSearchCommand::class);
111+
Rediska_Commands::add('ftdictdump', RediskaRediSearchCommand::class);
112+
Rediska_Commands::add('ftdropindex', RediskaRediSearchCommand::class);
113+
Rediska_Commands::add('ftexplain', RediskaRediSearchCommand::class);
114+
Rediska_Commands::add('ftexplaincli', RediskaRediSearchCommand::class);
115+
Rediska_Commands::add('ftlist', RediskaRediSearchCommand::class);
116+
Rediska_Commands::add('ftinfo', RediskaRediSearchCommand::class);
117+
Rediska_Commands::add('ftsearch', RediskaRediSearchCommand::class);
118+
Rediska_Commands::add('ftspellcheck', RediskaRediSearchCommand::class);
119+
Rediska_Commands::add('ftsugadd', RediskaRediSearchCommand::class);
120+
Rediska_Commands::add('ftsugdel', RediskaRediSearchCommand::class);
121+
Rediska_Commands::add('ftsugget', RediskaRediSearchCommand::class);
122+
Rediska_Commands::add('ftsuglen', RediskaRediSearchCommand::class);
123+
Rediska_Commands::add('ftsyndump', RediskaRediSearchCommand::class);
124+
Rediska_Commands::add('ftsynupdate', RediskaRediSearchCommand::class);
125+
Rediska_Commands::add('fttagvals', RediskaRediSearchCommand::class);
126+
Rediska_Commands::add('__redisearch', RediskaRediSearchCommand::class);
155127
}
156128

157129
public static function getRediSearchVersion(Client $client): ?string

0 commit comments

Comments
 (0)