macfja / redisearch
PHP Client for RediSearch
Installs: 80 474
Dependents: 2
Suggesters: 0
Security: 0
Stars: 67
Watchers: 5
Forks: 9
Open Issues: 12
Requires
- php: ^7.2 || ^8.0
- ext-intl: *
- composer/semver: ^3.2
- respect/validation: ^2.0
Requires (Dev)
- ext-mbstring: *
- 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: ^1.2.0
- phpunit/phpunit: ^8.5 || ^9.3
- predis/predis: ^1.1 || ^2.0
- ptrofimov/tinyredisclient: ^1.1
- redisent/redisent: dev-master
- roave/security-advisories: dev-latest
- rskuipers/php-assumptions: ^0.8.0
- sebastian/phpcpd: ^4.1 || ^6.0
- ukko/phpredis-phpdoc: dev-master
- vimeo/psalm: ^4.7
Suggests
- 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
README
MacFJA/redisearch is a PHP Client for RediSearch.
The implemented API is for RediSearch 2.x
Installation
composer require macfja/redisearch
Usage
Get a Redis client
This lib can use several connector for Redis:
- Predis - Pure PHP implementation
- Phpredis - PHP extension
- Phpiredis - PHP extension depending on hiredis
- Amp\Redis - Pure PHP Async implementation
- cheprasov/php-redis-client - Pure PHP implementation
- Credis - Pure PHP implementation
- Rediska - Pure PHP implementation
- Redisent - Pure PHP implementation
- TinyRedis - Pure PHP implementation
You can pick the connector depending on your need.
$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:
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade(); $clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);
Create a new index
$client = /* ... */; $builder = new \MacFJA\RediSearch\IndexBuilder(); // Field can be created in advance $address = (new \MacFJA\RediSearch\Redis\Command\CreateCommand\GeoFieldOption()) ->setField('address'); $builder ->setIndex('person') ->addField($address) // Or field can be created "inline" ->addTextField('lastname', false, null, null, true) ->addTextField('firstname') ->addNumericField('age') ->create($client);
The builder can also be used with withXxx
and withAddedXxx
instead of setXxx
and addXxx
.
This will give you a new instance of the builder with the configured data.
Add a document
$client = /* ... */; $index = new \MacFJA\RediSearch\Index('person', $client); $index->addDocumentFromArray([ 'firstname' => 'Joe', 'lastname' => 'Doe', 'age' => 30, 'address' => '-74.044502,40.689247' ]);
Search
$client = /* ... */; $search = new \MacFJA\RediSearch\Redis\Command\Search(); $search ->setIndex('person') ->setQuery('Doe') ->setHighlight(['lastname']) ->setWithScores(); $results = $client->execute($search);
Create a search query
use MacFJA\RediSearch\Query\Builder\GeoFacet; use MacFJA\RediSearch\Query\Builder\Negation; use MacFJA\RediSearch\Query\Builder\NumericFacet; use MacFJA\RediSearch\Query\Builder\Optional; use MacFJA\RediSearch\Query\Builder\Word; use MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption; $queryBuilder = new \MacFJA\RediSearch\Query\Builder(); $query = $queryBuilder ->addElement(NumericFacet::greaterThan('age', 17)) ->addString('Doe') ->addElement( new Negation( new GeoFacet(['address'], -74.044502, 40.589247, 40, GeoFilterOption::UNIT_KILOMETERS) ) ) ->addElement(new Optional(new Word('John'))) ->render(); // The value of $query is: // @age:[(17 +inf] Doe -@address:[-74.044502 40.589247 40.000000 km] ~John
Pipeline
use MacFJA\RediSearch\Redis\Command\Aggregate; 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; $client = /* ... */; $query = '@age:[(17 +inf] %john%'; $search = new Search(); $search->setIndex('people') ->setQuery($query); $stats = new Aggregate(); $stats->setIndex('people') ->setQuery($query) ->addGroupBy(new GroupByOption([], [ ReduceOption::average('age', 'avg'), ReduceOption::maximum('age', 'oldest') ])); $aggregate = new Aggregate(); $aggregate->setIndex('people') ->setQuery($query) ->addGroupBy(new GroupByOption(['lastname'], [ReduceOption::count('count')])); $suggestion = new SugGet(); $suggestion->setDictionary('names') ->setPrefix('john') ->setFuzzy(); $result = $client->pipeline($search, $stats, $aggregate, $suggestion); // $result[0] is the search result // $result[1] is the first aggregation result // $result[2] is the second aggregation result // $result[3] is the suggestion result
Use Predis (v1.x) shorthand syntax
$client = new \Predis\Client(/* ... */); \MacFJA\RediSearch\Redis\Initializer::registerCommandsPredis($client->getProfile()); $client->ftsearch('people', '@age:[(17 +inf] %john%'); // But you will have raw Redis output.
Use Rediska shorthand syntax
$client = new \Rediska(/* ... */); \MacFJA\RediSearch\Redis\Initializer::registerCommandsRediska(); $client->ftsearch('people', '@age:[(17 +inf] %john%'); // But you will have raw Redis output.
Similar projects
- ethanhann/redisearch-php - Abandoned
- front/redisearch - Partial fork of
ethanhann/redisearch-php
- ashokgit/redisearch-php - Fork of
ethanhann/redisearch-php
Contributing
You can contribute to the library. To do so, you have Github issues to:
- ask your question
- request any change (typo, bad code, new feature etc.)
- and much more...
You also have PR to:
- suggest a correction
- suggest a new feature
- and much more...
See CONTRIBUTING for more information.
License
The MIT License (MIT). Please see License File for more information.