LESSON 03 · Prefix index

Find candidates by prefix

Narrow the candidate set before ranking it.

SYSTEM VIEW

The suggestion path

Diagram view
Edges—
Scanned—
List memory—

Watch a trie find the prefix “ca” cheaply, then see why choosing the best terms below a short prefix is the expensive part.

  1. The Client types “ca”, and the Query API walks the Trie one edge per character, so terms that share a prefix share a path. Reaching “ca” costs O(p), two edges here. A terminal marker tells a submitted query such as “car” from a mere prefix.

    Prefix
    “ca”
    Cost
    O(p), p = 2

    What changed

    • “ca” sent
    • Trie reaches “ca” in 2 edges
  2. With example counts car 900, card 700, cat 600, care 400 and cup 100, the node for “ca” has four descendants, and the top two, car and card, go back to the Client. At this size, ranking is easy.

    Below “ca”
    4 terms
    Top two
    car, card

    What changed

    • 4 terms ranked below “ca”
    • Top two: car, card
  3. In a catalog of 50 million terms, the node for “s” may have millions of descendants. The Trie still reaches it in one edge; scanning and sorting all of them on every keystroke is what costs too much.

    Catalog
    50 million terms
    Below “s”
    millions

    What changed

    • “s” sent
    • 1 edge to reach, millions to sort
  4. Store a bounded top-K list at selected nodes, or materialize a prefix-to-top-K map: the Prefix cache keeps ten ranked candidates per prefix. A read of “s” is now one lookup of ten ready candidates, with nothing to sort.

    Per prefix
    top 10, ranked

    What changed

    • Prefix cache: top 10 per prefix
    • “s” read from a stored list
  5. The trade is memory and build work for fast reads: 20 million prefixes × 10 candidates × 40 bytes is 8 GB in the Prefix cache before overhead. Every list also has to be rebuilt when counts change.

    Prefixes
    20 million
    Per list
    10 × 40 bytes
    Memory
    8 GB + overhead

    What changed

    • Prefix cache holds 8 GB
    • Lists rebuilt when counts change
  6. The Client types “york”, looking for “new york”. The Trie follows “york” from the root, at most four edges, and never reaches “new york” under “n”. Matching a word inside a term means indexing each word or using n-grams, at more memory.

    Not found
    “new york”
    Fix
    index each word, n-grams

    What changed

    • “york” sent
    • “new york” not found
  7. For a small catalog, a B-tree on the lowercased term in Postgres may suffice. The Query API reads the Terms table from “ca” up to “cb” and gets car, card, care and cat. A compressed trie or a sparse map may fit better, depending on vocabulary size, update rate and language.

    Range
    ca ≤ term < cb
    Index
    B-tree on lower(term)

    What changed

    • Terms table in place of the Prefix cache
    • B-tree on the lowercased term
    • 4 rows read from “ca” up to “cb”

Space plays · ← → step · Home resets

THE IDEA

How it works

For a small catalog, a normalized indexed range query may suffice: in Postgres, a B-tree on the lowercased term can answer “every term from ca up to cb.” A trie is a tree with one edge per character, so terms that share a prefix share a path. Following edges reaches a prefix node; a terminal marker distinguishes a complete submitted query from a mere prefix. Prefix lookup costs O(p) for input length p, but that says nothing about the cost of finding the best descendants.

Consider terms with example counts: car (900), card (700), cat (600), care (400), cup (100). The node for “ca” has four descendants, and the top two are car and card. That is easy at this size. In a catalog of 50 million terms, the node for “s” may have millions of descendants, and scanning and sorting them on every keystroke is too expensive. Store a bounded top-K list at selected nodes, or materialize a prefix-to-top-K map. That trades memory and build work for fast reads: 20 million prefixes × 10 candidates × 40 bytes is 8 GB before overhead.

A compressed trie, sparse map, or database index may fit better depending on vocabulary size, update rate, and language. Elasticsearch’s completion suggester, for instance, uses an in-memory finite state transducer, a compact relative of the trie. A trie answers only prefixes: matching “york” inside “new york” requires indexing each word or using n-grams, at more memory. Capping input length bounds p only if enforced; it does not make arbitrary lookup inherently O(1).

DECISION POINT

The prefix “a” has a million descendants. Why can a basic trie still be slow?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Prefix lookup and top-K selection have separate costs.
  • Precomputed top-K lists trade memory and rebuild work for low read latency.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What does following a trie path for p characters cost in the basic model?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.