Storage engines
Redis engine
TNTSearch supports Redis as an alternative storage engine to SQLite. The Redis engine stores the inverted index in Redis data structures, making it suitable for high-throughput applications and distributed environments where multiple servers need access to the same index.
Requirements
The Redis engine requires the predis/predis package, which is included as a dependency of TNTSearch:
composer require predis/predis
You also need a running Redis server.
Configuration
To use Redis, set the engine to RedisEngine and provide Redis connection details:
use TeamTNT\TNTSearch\TNTSearch;
$tnt = new TNTSearch;
$tnt->loadConfig([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'user',
'password' => 'pass',
'storage' => '/path/to/indexes/',
'engine' => 'TeamTNT\TNTSearch\Engines\RedisEngine',
'redis_host' => '127.0.0.1',
'redis_port' => '6379',
]);
Full configuration options
| Option | Required | Default | Description |
|---|---|---|---|
engine | Yes | - | Must be TeamTNT\TNTSearch\Engines\RedisEngine |
redis_host | Yes | - | Redis server hostname or IP |
redis_port | Yes | - | Redis server port |
redis_password | No | null | Redis authentication password |
redis_scheme | No | tcp | Connection scheme (tcp or tls) |
redis_ssl_options | No | null | SSL/TLS options for secure connections |
redis_options | No | null | Additional Predis client options |
Secure connection example
$tnt->loadConfig([
'driver' => 'mysql',
'engine' => 'TeamTNT\TNTSearch\Engines\RedisEngine',
'redis_host' => 'redis.example.com',
'redis_port' => '6380',
'redis_password' => 'your-password',
'redis_scheme' => 'tls',
'redis_ssl_options' => [
'verify_peer' => true,
],
// ... other config
]);
Creating and searching an index
The API is identical to the SQLite engine. You create and search indexes the same way:
// Create index
$indexer = $tnt->createIndex('articles.index');
$indexer->query('SELECT id, title, body FROM articles;');
$indexer->run();
// Search
$tnt->selectIndex('articles.index');
$results = $tnt->search('full text search');
All search features work with Redis: boolean search, fuzzy search, as-you-type, and stemming.
Redis data structures
TNTSearch stores the index using these Redis key patterns:
| Key pattern | Type | Description |
|---|---|---|
{indexName}:wordlist:{term} | Hash | Stores num_hits and num_docs for each term |
{indexName}:doclist:{term}:{docId} | Hash | Stores num_hits for a term in a specific document |
{indexName}:info | Hash | Index metadata (stemmer, tokenizer, total_documents) |
{indexName}:filemap:{id} | String | File path mapping (for filesystem driver) |
Flushing an index
To completely remove an index from Redis:
// Creating a new index with the same name automatically flushes the old one
$indexer = $tnt->createIndex('articles.index');
The Redis engine's flushIndex() method scans and deletes all keys matching the index name prefix.
When to use Redis
The Redis engine is ideal when:
- Multiple application servers need to share the same search index
- You need fast, in-memory index access
- You're already running Redis in your infrastructure
- You want the index to survive application deployments without file management
Memory considerations
Redis stores everything in memory. Large indexes will consume significant RAM. Monitor your Redis memory usage and consider using SQLite for very large datasets that don't fit comfortably in memory.
