Skip to content

Searching

Text fields can be filtered with the index’s search method.

$result = $bookIndex->search('two cities');
$result->getCount(); // Number of documents.
$result->getDocuments(); // Array of stdObjects.

Documents can also be returned as arrays instead of objects by passing true as the second parameter to the search method.

$result = $bookIndex->search('two cities', true);
$result->getDocuments(); // Array of arrays.

Tag fields can be filtered with the index’s tagFilter method.

Specifying multiple tags creates a union of documents.

$result = $bookIndex
->tagFilter('color', ['blue', 'red'])
->search('two cities');

Use multiple separate tagFilter calls to create an intersection of documents.

$result = $bookIndex
->tagFilter('color', ['blue'])
->tagFilter('color', ['red'])
->search('two cities');

Numeric fields can be filtered with the index’s numericFilter method.

$result = $bookIndex
->numericFilter('price', 4.99, 19.99)
->search('two cities');

Geo fields can be filtered with the index’s geoFilter method.

$result = $bookIndex
->geoFilter('place', -77.0366, 38.897, 100)
->search('two cities');

Search results can be sorted with the index’s sort method.

$result = $bookIndex
->sortBy('price')
->search('two cities');

The number of documents can be retrieved after performing a search.

$result = $bookIndex->search('two cities');
$result->getCount(); // Number of documents.

Alternatively, the number of documents can be queried without returning the documents themselves. This is useful if you want to check the total number of documents without returning any other data from the Redis server.

$numberOfDocuments = $bookIndex->count('two cities');

A supported language can be specified when running a query. Supported languages are represented as constants in the Ehann\RediSearch\Language class.

$result = $bookIndex
->language(Language::ITALIAN)
->search('two cities');

RediSearch v2.4+ supports multiple query dialects that unlock different syntax features. Use dialect() to select a version (1, 2, or 3):

$result = $bookIndex
->dialect(2)
->search('two cities');

Dialect 2 is required for vector/KNN queries and extended query syntax.

Vector similarity search allows you to find documents whose vector fields are nearest to a query vector. This requires a field indexed with addVectorField(), dialect 2, and the params() method to pass the query vector as a named parameter.

// Pack your float32 values into a binary string.
$queryVector = pack('f*', 0.1, 0.2, 0.3, /* ... 128 floats total */);
$result = $bookIndex
->params(['vec' => $queryVector])
->dialect(2)
->search('*=>[KNN 5 @embedding $vec]');

spellCheck() returns suggestions for potentially misspelled terms in a query. The optional second argument sets the maximum edit distance (1–4, default 1).

$suggestions = $bookIndex->spellCheck('helo'); // distance 1
$suggestions = $bookIndex->spellCheck('helo', 2); // distance 2

Synonym groups let you treat different terms as equivalent during search.

// Register 'book', 'novel', and 'tome' as synonyms.
$bookIndex->synUpdate('group1', 'book', 'novel', 'tome');
// Inspect all synonym mappings for the index.
$map = $bookIndex->synDump();

An explanation for a query can be generated with the index’s explain method.

This can be helpful for understanding why a query is returning a set of results.

$result = $bookIndex
->numericFilter('price', 4.99, 19.99)
->sortBy('price')
->explain('two cities');

Logging is optional. It can be enabled by injecting a PSR compliant logger, such as Monolog, into a RedisClient instance.

Install Monolog:

Terminal window
composer require monolog/monolog

Inject a logger instance (with a stream handler in this example):

$logger = new Logger('Ehann\RediSearch');
$logger->pushHandler(new StreamHandler('MyLogFile.log', Logger::DEBUG));
$this->redisClient->setLogger($logger);