Search and vectors · Engineering

Fuzzy search with trigrams and Levenshtein

Typo-tolerant matching without a search cluster: trigram candidates, bounded edit distance, and the limits of the approach.

Fuzzy search is the feature that makes a search box feel forgiving. A user types postgress, the application returns PostgreSQL. A product search survives a missing letter.

NonoDB implements fuzzy matching with a trigram candidate index and a bounded Levenshtein distance. It is a focused feature, not a full Lucene fuzzy query language.

Creating a fuzzy index

A fuzzy index covers one field:

{"op": "create_fuzzy_index", "coll": "articles", "field": "title"}

Only string, boolean, and numeric scalar values are searched. Arrays, objects, dates, binary values, and ObjectIds are skipped.

Querying

{
"op": "fuzzy",
"coll": "articles",
"field": "title",
"query": "postgress",
"max_distance": 2,
"filter": {"published": true},
"limit": 10
}

Semantics:

  • the fuzzy index must exist;
  • candidate generation uses shared trigrams;
  • final ranking uses bounded Levenshtein distance;
  • distance is measured against the whole normalized field value or the closest token;
  • max_distance defaults to 2;
  • _distance is added to each result, lower is better;
  • there is no skip parameter;
  • limit defaults to 10 and is capped at 1,000;
  • at most 5,000 candidates are scored per query.

The 5,000-candidate cap is the important operational limit. A broad query against a large field can hit it. The result is still correct for the candidates that were scored, but recall is bounded.

Why trigrams first

Levenshtein distance is expensive to compute for every document. Trigrams turn the field into a set of three-character shingles and use shared shingles to generate candidates. Only those candidates get the full edit-distance calculation.

The final distance is measured against the whole normalized value or the closest token. That means a short field like title behaves differently from a long body: a typo in one word of a long body may be treated as a token-level match rather than a whole-string edit.

Normalization

The index normalizes case and diacritics before building trigrams. A query for zazolc can match zażółć. The exact normalization is the same family used by text search, but fuzzy matching does not stem or remove stop words.

This is why fuzzy search is useful for names, product titles, and short labels. It is less useful for natural-language paragraphs where token boundaries matter more than a single edit distance.

Filters and TTL

filter uses the normal document filter language and is applied before pagination. Expired documents are filtered out according to strict TTL reads, and the path over-fetches when needed.

There is no skip for fuzzy search. If you need pagination, fetch a larger limit and paginate in the application, or combine fuzzy candidates with a separate query. This is a deliberate simplification to keep the scoring path bounded.

What it is not

Fuzzy search does not provide:

  • phonetic matching;
  • synonyms;
  • stemming;
  • per-language analyzers;
  • arbitrary edit distance per term;
  • fuzzy matching inside a BM25 query;
  • a distributed index.

It is one field, one query, bounded candidates. If you need “find any term in this document within two edits, with synonyms and language rules”, use a dedicated search engine.

A common pattern is:

  1. Try exact/BM25 search.
  2. If the result count is low, run fuzzy search on the same query.
  3. Merge and deduplicate by _id.
  4. Show “did you mean” or typo-tolerant results separately.

NonoDB’s search_all API can run lexical and fuzzy modes, and hybrid search can combine lexical and vector results. It does not automatically decide when to fall back to fuzzy; that is application policy.

When it is enough

Fuzzy search is a good fit for:

  • product and catalog names;
  • people and company lookups;
  • support article titles;
  • admin search boxes where a typo should not return zero results.

It is a poor fit for:

  • full-text relevance over long documents;
  • multilingual stemming;
  • large corpora with strict recall requirements;
  • search-as-a-product.

The search API and search guide document the limits, index lifecycle, and validation tests.

All engineering notes →Read the docs