Searching

Fuzzy search

Fuzzy search allows TNTSearch to find matching documents even when the search query contains typos or misspellings. It uses Levenshtein distance to find terms in the index that are similar to what the user typed.


Fuzzy search is disabled by default. Enable it before performing a search:

$tnt = new TNTSearch;
$tnt->loadConfig($config);
$tnt->selectIndex('articles.index');

$tnt->fuzziness(true);

$results = $tnt->search('juleit');  // matches "juliet"
$results = $tnt->search('quen');    // matches "queen"

Configuration options

You can fine-tune fuzzy search behavior with these settings:

Fuzzy distance

The maximum Levenshtein distance allowed between the search term and index terms. Default is 2.

$tnt->setFuzzyDistance(2);

A distance of 1 allows single character insertions, deletions, or substitutions. A distance of 2 allows two such changes.

Prefix length

The minimum number of characters that must match exactly at the beginning of the word. Default is 2. This improves performance by reducing the number of candidates to check.

$tnt->setFuzzyPrefixLength(2);

Max expansions

The maximum number of similar terms to consider. Default is 50. Higher values give more thorough results but take longer.

$tnt->setFuzzyMaxExpansions(50);

No limit mode

By default, if a search term has an exact match, only the exact match is returned. Enable "no limit" mode to also include fuzzy matches alongside exact matches:

$tnt->fuzziness(true);

// Only returns documents with "199x"
$results = $tnt->search('199x');

// Returns documents with "199x", "199y", and other similar terms
$tnt->fuzzyNoLimit(true);
$results = $tnt->search('199x');

Full example

$tnt = new TNTSearch;
$tnt->loadConfig($config);
$tnt->selectIndex('articles.index');

$tnt->fuzziness(true);
$tnt->setFuzzyDistance(2);
$tnt->setFuzzyPrefixLength(2);
$tnt->setFuzzyMaxExpansions(50);

$results = $tnt->search('misspleled');
// Still finds documents containing "misspelled"

Reading current settings

You can retrieve the current fuzzy search settings:

$tnt->getFuzziness();         // bool
$tnt->getFuzzyDistance();     // int
$tnt->getFuzzyPrefixLength(); // int
$tnt->getFuzzyMaxExpansions(); // int
$tnt->getFuzzyNoLimit();      // bool

Performance tip

Fuzzy search is more expensive than exact matching because it must compare the search term against many terms in the index. Use fuzzy_prefix_length and fuzzy_max_expansions to control the trade-off between recall and speed.

Previous
Boolean search