RediSearch-PHP
RediSearch-PHP is a PHP client library for the RediSearch module which adds full-text search to Redis.
Requirements
Section titled “Requirements”- Redis Stack or Redis with the RediSearch module v2.x loaded.
- PHP >=8.2
- PhpRedis, Predis, or php-redis-client.
Install
Section titled “Install”composer require ethanhann/redisearch-phprequire_once 'vendor/autoload.php';Create a Redis Client
Section titled “Create a Redis Client”use Ehann\RedisRaw\PredisAdapter;use Ehann\RedisRaw\PhpRedisAdapter;use Ehann\RedisRaw\RedisClientAdapter;
$redis = (new PredisAdapter())->connect('127.0.0.1', 6379);// or$redis = (new PhpRedisAdapter())->connect('127.0.0.1', 6379);// or$redis = (new RedisClientAdapter())->connect('127.0.0.1', 6379);Create the Schema
Section titled “Create the Schema”use Ehann\RediSearch\Index;
$bookIndex = new Index($redis);
$bookIndex->addTextField('title') ->addTextField('author') ->addNumericField('price') ->addNumericField('stock') ->create();Add a Document
Section titled “Add a Document”$bookIndex->add([ new TextField('title', 'Tale of Two Cities'), new TextField('author', 'Charles Dickens'), new NumericField('price', 9.99), new NumericField('stock', 231),]);Search the Index
Section titled “Search the Index”$result = $bookIndex->search('two cities');
$result->getCount(); // Number of documents.$result->getDocuments(); // Array of matches.
// Documents are returned as objects by default.$firstResult = $result->getDocuments()[0];$firstResult->title;$firstResult->author;