Searching

As-you-type search

As-you-type search (also known as prefix search or autocomplete search) returns results based on partial word matches. As the user types each character, TNTSearch matches the beginning of indexed terms, providing instant feedback.


Enabling as-you-type

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

$tnt->asYouType(true);

$results = $tnt->search('k');
// Returns documents containing words starting with "k" (e.g., "king", "kingdom")

How it works

When as-you-type is enabled, the last keyword in your search query is treated as a prefix. TNTSearch looks up all terms in the index that start with that prefix and returns matching documents.

For example, if a user types "sha", TNTSearch would match "shakespeare", "shadow", "shall", etc.

$tnt->asYouType(true);

$results = $tnt->search('Jul');
// Matches "Juliet", "Julius", "July", etc.

$results = $tnt->search('Queen Ma');
// "Queen" is matched exactly, "Ma" is treated as prefix
// Matches documents with "Queen" AND words starting with "Ma" (e.g., "Mab")

Alternative API

You can also use the setter method:

$tnt->setAsYouType(true);

// Check current setting
$isEnabled = $tnt->getAsYouType(); // true

Usage with other features

As-you-type can be combined with fuzzy search for even more forgiving live search:

$tnt->asYouType(true);
$tnt->fuzziness(true);

$results = $tnt->search('shak');
// Matches "shakespeare" via prefix, and also fuzzy matches nearby terms

Tip

For the best search-as-you-type experience, consider pairing this with a debounced input handler on the frontend to avoid excessive requests while the user is still typing.

Previous
Fuzzy search