~learn.
0:00 / 1:28
W07 · Search system

Autocomplete breaks the same-analyzer rule, on purpose

transcript

You type n, i. Nike appears before you've typed the k. Every keystroke is a search. So how is a half-typed word that fast?

The trick: stop searching for prefixes at query time. At index time, nike goes through an edge n-gram filter. It stores every prefix from the front of the word. n, ni, nik, nike. Now you type ni. At search time, it's only lowercased. And ni is an exact match to a stored token. A prefix search became a plain lookup.

But here's the catch. The rule was: same analyzer at index time and query time. Autocomplete breaks it. On purpose.

Say you n-grammed the query too. The user types nik. It becomes ni, and nik. And ni matches every word that starts with ni. Not what they typed. Only lowercase it, and nik matches the stored token nik. Exactly.

The price is storage, a few extra tokens per word. In practice, prefixes run from 2 characters to 20, to cut noise. The win: each keystroke is an exact token lookup. That's how you hold a 50 millisecond p99.

Autocomplete doesn't hunt for prefixes. It looks them up. N-gram the index. Leave the query as typed.

Full breakdown in the lecture.