Skip to content

Remove hard dependency on Predis + allow other Redis lib #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .unused.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'enlightn/security-checker', // QA tool
'php-parallel-lint/php-parallel-lint', // QA tool
'sebastian/phpcpd', // QA tool
'ukko/phpredis-phpdoc' // Stubs
],
'excludeDirectories' => [],
'scanFiles' => [],
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: analyze fix-code test coverage mutation-test validation
.PHONY: analyze fix-code test coverage validation integration-test

analyze: | vendor
$(COMPOSER) exec -v parallel-lint -- src
Expand All @@ -22,7 +22,10 @@ coverage: | vendor
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
$(COMPOSER) exec -v phpunit -- --coverage-text --color

validation: fix-code analyze test coverage
integration-test: | vendor
$(COMPOSER) exec -v phpunit -- --group integration

validation: fix-code analyze test coverage integration-test

vendor: composer.json
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
Expand Down
73 changes: 59 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,62 @@ composer require macfja/redisearch

## Usage

### Get a Redis client

This lib can use several connector for Redis:
- [Predis](https://github.com/predis/predis/wiki) - Pure PHP implementation
- [Phpredis](https://github.com/phpredis/phpredis) - PHP extension
- [Phpiredis](https://github.com/nrk/phpiredis) - PHP extension depending on [hiredis](https://github.com/redis/hiredis)
- [Amp\Redis](https://github.com/amphp/redis) - Pure PHP Async implementation
- [cheprasov/php-redis-client](https://github.com/cheprasov/php-redis-client) - Pure PHP implementation
- [Credis](https://github.com/colinmollenhour/credis) - Pure PHP implementation
- [Rediska](https://github.com/Shumkov/Rediska) - Pure PHP implementation
- [Redisent](https://github.com/jdp/redisent) - Pure PHP implementation
- [TinyRedis](https://github.com/ptrofimov/tinyredisclient) - Pure PHP implementation

You can pick the connector depending of your need.

```php
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();

// With Predis
$client = $clientFacade->getClient(new \Predis\Client(/* ... */));

// With Phpredis extension
$client = $clientFacade->getClient(new \Redis([/* ... */]));

// With Phpiredis extension
$client = $clientFacade->getClient(phpiredis_connect($host));

// With Amp\Redis
$client = $clientFacade->getClient(new \Amp\Redis\Redis(new RemoteExecutor(Config::fromUri(/* ... */))));

// With Cheprasov
$client = $clientFacade->getClient(new \RedisClient\Client\Version\RedisClient6x0([/* ... */]));

// With Rediska
$client = $clientFacade->getClient(new \Rediska(['servers' => [[/* ... */]]]));

// With Redisent
$client = $clientFacade->getClient(new \redisent\Redis(/* ... */));

// With TinyRedisClient
$client = $clientFacade->getClient(new \TinyRedisClient(/* ... */));

// With Credis
$client = $clientFacade->getClient(new \Credis_Client(/* ... */));
```

You can add your own implementation, all you need is to implement the interface `\MacFJA\RediSearch\Redis\Client` and add it to the client facace with:
```php
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
$clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);
```

### Create a new index

```php
$client = new \Predis\Client(/* ... */);
$client = /* ... */;
$builder = new \MacFJA\RediSearch\IndexBuilder();

// Field can be create in advance
Expand All @@ -38,9 +90,9 @@ This will give you a new instance of the builder with the configured data.
### Add a document

```php
$client = new \Predis\Client(/* ... */);
$client = /* ... */;
$index = new \MacFJA\RediSearch\Index('person', $client);
$index->addFromArray([
$index->addDocumentFromArray([
'firstname' => 'Joe',
'lastname' => 'Doe',
'age' => 30,
Expand All @@ -51,15 +103,15 @@ $index->addFromArray([
### Search

```php
$client = new \Predis\Client(/* ... */);
$client = /* ... */;
$search = new \MacFJA\RediSearch\Redis\Command\Search();

$search
->setIndex('person')
->setQuery('Doe')
->setHighlight(['lastname'])
->setWithScores();
$results = $client->executeCommand($search);
$results = $client->execute($search);
```

#### Create a search query
Expand Down Expand Up @@ -96,9 +148,8 @@ use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
use MacFJA\RediSearch\Redis\Command\Search;
use MacFJA\RediSearch\Redis\Command\SugGet;
use Predis\Client;

$client = new Client(/* ... */);
$client = /* ... */;

$query = '@age:[(17 +inf] %john%';
$search = new Search();
Expand All @@ -123,13 +174,7 @@ $suggestion->setDictionary('names')
->setPrefix('john')
->setFuzzy();

$result = $client->pipeline()
->executeCommand($search)
->executeCommand($stats)
->executeCommand($aggregate)
->executeCommand($suggestion)
->execute()
;
$result = $client->pipeline($search, $stats, $aggregate, $suggestion);

// $result[0] is the search result
// $result[1] is the first aggregation result
Expand Down
20 changes: 19 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,41 @@
"php": "^7.2",
"ext-intl": "*",
"composer/semver": "^3.2",
"predis/predis": "^1.1",
"respect/validation": "^2.0"
},
"require-dev": {
"amphp/redis": "^1.0",
"cheprasov/php-redis-client": "^1.10",
"colinmollenhour/credis": "^1.12",
"enlightn/security-checker": "^1.9",
"ergebnis/composer-normalize": "^2.13",
"friendsofphp/php-cs-fixer": "^3.0",
"geometria-lab/rediska": "^0.5.10",
"insolita/unused-scanner": "^2.3",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpmd/phpmd": "^2.10",
"phpstan/phpstan": "^0.12.92",
"phpunit/phpunit": "^8.5",
"predis/predis": "^1.1",
"ptrofimov/tinyredisclient": "^1.1",
"redisent/redisent": "dev-master",
"roave/security-advisories": "dev-latest",
"rskuipers/php-assumptions": "^0.8.0",
"sebastian/phpcpd": "^4.1",
"ukko/phpredis-phpdoc": "dev-master",
"vimeo/psalm": "^4.7"
},
"suggest": {
"ext-phpiredis": "To use Phpiredis extension implementation",
"ext-redis": "To use Phpredis extension implementation",
"amphp/redis": "To use AmpPhp implementation",
"cheprasov/php-redis-client": "To use Cheprasov implementation",
"colinmollenhour/credis": "To use Credis implementation",
"geometria-lab/rediska": "To use Rediska implementation",
"predis/predis": "To use Predis implementation",
"ptrofimov/tinyredisclient": "To use TinyRedisClient implementation",
"redisent/redisent": "To use Redisent implementation"
},
"config": {
"platform": {
"php": "7.2"
Expand Down
Loading