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

OptionRequiredDefaultDescription
engineYes-Must be TeamTNT\TNTSearch\Engines\RedisEngine
redis_hostYes-Redis server hostname or IP
redis_portYes-Redis server port
redis_passwordNonullRedis authentication password
redis_schemeNotcpConnection scheme (tcp or tls)
redis_ssl_optionsNonullSSL/TLS options for secure connections
redis_optionsNonullAdditional 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 patternTypeDescription
{indexName}:wordlist:{term}HashStores num_hits and num_docs for each term
{indexName}:doclist:{term}:{docId}HashStores num_hits for a term in a specific document
{indexName}:infoHashIndex metadata (stemmer, tokenizer, total_documents)
{indexName}:filemap:{id}StringFile 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.

Previous
SQLite engine