@@ -12,10 +12,38 @@ composer require macfja/redisearch
1212
1313## Usage
1414
15+ ### Get a Redis client
16+
17+ This lib can use several connector for Redis:
18+ - [ Predis] ( https://github.com/predis/predis/wiki ) - Pure PHP implementation
19+ - [ Phpredis] ( https://github.com/phpredis/phpredis ) - PHP extension
20+ - [ Phpiredis] ( https://github.com/nrk/phpiredis ) - PHP extension depending on [ hiredis] ( https://github.com/redis/hiredis )
21+
22+ You can pick the connector depending of your need.
23+
24+ ``` php
25+ $clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
26+
27+ // With Predis
28+ $client = $clientFacade->getClient(new \Predis\Client(/* ... */));
29+
30+ // With Phpredis extension
31+ $client = $clientFacade->getClient(new Redis([/* ... */]));
32+
33+ // With Phpiredis extension
34+ $client = $clientFacade->getClient(phpiredis_connect($host));
35+ ```
36+
37+ 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:
38+ ``` php
39+ $clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
40+ $clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);
41+ ```
42+
1543### Create a new index
1644
1745``` php
18- $client = new \Predis\Client( /* ... */) ;
46+ $client = /* ... */;
1947$builder = new \MacFJA\RediSearch\IndexBuilder();
2048
2149// Field can be create in advance
@@ -38,9 +66,9 @@ This will give you a new instance of the builder with the configured data.
3866### Add a document
3967
4068``` php
41- $client = new \Predis\Client( /* ... */) ;
69+ $client = /* ... */;
4270$index = new \MacFJA\RediSearch\Index('person', $client);
43- $index->addFromArray ([
71+ $index->addDocumentFromArray ([
4472 'firstname' => 'Joe',
4573 'lastname' => 'Doe',
4674 'age' => 30,
@@ -51,15 +79,15 @@ $index->addFromArray([
5179### Search
5280
5381``` php
54- $client = new \Predis\Client( /* ... */) ;
82+ $client = /* ... */;
5583$search = new \MacFJA\RediSearch\Redis\Command\Search();
5684
5785$search
5886 ->setIndex('person')
5987 ->setQuery('Doe')
6088 ->setHighlight(['lastname'])
6189 ->setWithScores();
62- $results = $client->executeCommand ($search);
90+ $results = $client->execute ($search);
6391```
6492
6593#### Create a search query
@@ -96,9 +124,8 @@ use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
96124use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
97125use MacFJA\RediSearch\Redis\Command\Search;
98126use MacFJA\RediSearch\Redis\Command\SugGet;
99- use Predis\Client;
100127
101- $client = new Client( /* ... */) ;
128+ $client = /* ... */;
102129
103130$query = '@age:[(17 +inf] %john%';
104131$search = new Search();
@@ -123,13 +150,7 @@ $suggestion->setDictionary('names')
123150 ->setPrefix('john')
124151 ->setFuzzy();
125152
126- $result = $client->pipeline()
127- ->executeCommand($search)
128- ->executeCommand($stats)
129- ->executeCommand($aggregate)
130- ->executeCommand($suggestion)
131- ->execute()
132- ;
153+ $result = $client->pipeline($search, $stats, $aggregate, $suggestion);
133154
134155// $result[0] is the search result
135156// $result[1] is the first aggregation result
0 commit comments