Highlights

Contents

Part VII
07

Autocomplete and fuzzy search

We respond while the user is still typing. Prefixes and spelling corrections broaden matching and can multiply work. We will build trie intuition, indexed prefixes, edit distance, and ranked suggestions with explicit bounds.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Search basics: why LIKEfails2Tokenization and analysis3The inverted index4Ranking: TF-IDF and BM255The indexing pipeline6Shards and replicas7Autocomplete and fuzzysearch8Operating a searchcluster9Case study: search end toend

#Section 25Prefix search starts at the beginning

The user has typed bir and wants bird. A prefix query matches terms beginning with the supplied characters. It differs from a substring query, which may match characters inside a term. An ordered term dictionary can seek to bir and enumerate terms until that prefix no longer matches.

A trie stores character paths so terms with a common prefix share their initial path. For bird, the path is b, then i, then r, then d. A terminal marker distinguishes a complete term from a path that is only a prefix. Walking bir reaches the subtree whose descendants supply possible completions.

FOLLOW THE PREFIX PATH BEFORE EXPANDING THE REMAINING BRANCHESFOLLOW THE PREFIX PATH BEFORE EXPANDING THE REMAINING BRANCHESrootbirdprefix bir ends hereterminal wordillustrative single-word trie; shared prefixes reuse the same path
Figure 15.25Illustrative single-word trie path. The search for bir reaches the node before the terminal d.

The lookup cost includes walking query characters and enumerating or ranking descendants. A popular one-character prefix can have a huge candidate set, so saying tries make autocomplete constant time hides the output problem. Bound suggestions, require a useful prefix length where appropriate, and store ranking summaries if the workload needs them.

#Section 26Indexed prefixes trade space for query work

Instead of walking all candidate dictionary terms, the index can store selected leading fragments of each token. An edge n-gram is a token fragment taken from the beginning or end, usually the beginning for autocomplete. For bird, leading fragments of lengths 1 through 4 are b, bi, bir, and bird.

A query for bir now looks up a prepared posting list. The cost moves into ingestion and storage because one original token can generate several fragments. The maximum length and minimum length limit that expansion. A multiword query also needs a rule about whether only the final incomplete word is a prefix while earlier words must match normally.

INDEXED PREFIXES STORE MORE FRAGMENTS TO ANSWER SHORTER INPUTSINDEXED PREFIXES STORE MORE FRAGMENTS TO ANSWER SHORTER INPUTSbirdbbirdbibirdbirbirdbirdbird4 indexed prefix fragments for 4 letters
Figure 15.26Computed illustrative prefixes for the word bird. Real analyzer settings choose a bounded fragment range.

Autocomplete is the user-facing suggestion task; prefix search is one possible matching component. Suggestions may be report titles, team names, or popular complete queries. A completion structure can rank a small set of known suggestions without searching every report body. Decide the thing being suggested before choosing an index.

#Section 27Fuzzy search compares edits

The user types brd and expects bird. Edit distance is the minimum number of permitted edits needed to transform one string into another. With insertion, deletion, and substitution, brd needs one insertion of i, while brad needs two substitutions to become bird under the stated convention.

A fuzzy query accepts terms whose edit distance from the input is within a limit. Dynamic programming defines the distance by a grid of prefixes. The cell for prefixes ending at characters ii and jj takes the best of deletion, insertion, and substitution or equality. Each transition builds a longer pair of compared prefixes from shorter ones.

EDIT DISTANCE COUNTS INSERTIONS, DELETIONS AND SUBSTITUTIONSEDIT DISTANCE COUNTS INSERTIONS, DELETIONS AND SUBSTITUTIONSbradbird0123410123211233212343222bird → bird0bird → brd1bird → brad2matrix compares bird with brad; transposition is not a single edit here
Figure 15.27Computed Levenshtein distances with insertion, deletion, and substitution. Transposition is not an allowed single edit in this example.

For a negative candidate, the distance grid still establishes that no permitted sequence of edits fits the limit. Optimized engines can stop exploring dictionary branches whose minimum possible cost already exceeds the budget. That pruning depends on the defined edit operations. Changing transposition behavior changes the accepted language and must be reflected in examples and tests.

The straightforward distance algorithm costs the product of the two word lengths. Engines can use automata and dictionary traversal to avoid comparing every word independently. A high edit limit, short query, or large expansion cap still makes a broad request. Restrict the fuzzy scope and expansion budget before it becomes a public resource-exhaustion endpoint.

#Section 28Rank suggestions without teaching the wrong lesson

A suggestion list that always displays a famous team can suppress an exact but less popular match. Suggestion ranking often combines lexical fit, popularity, recency, and user context. Those signals answer different questions, so inspect them separately when a result seems wrong.

A small cache of popular public prefix results can remove repeated work. Under an assumed hit rate of 0.8 at 1,000 queries per second, 800 requests hit the cache and 200 reach the engine. That hit rate is illustrative. Short prefixes are popular, but personalization and permission dimensions reduce sharing and make cache keys larger.

A SUGGESTION CACHE KEY INCLUDES THE PREFIX AND VISIBILITY CONTEXTA SUGGESTION CACHE KEY INCLUDES THE PREFIX AND VISIBILITY CONTEXTcache keyprefixtenant / visibilityanalyzer versionsuggestion cache1000/s × 0.8 = 800 hits/s; 200 misses/seach envelope represents 100 requests/s in the illustrative workload
Figure 15.28Computed illustrative miss rate under a declared 0.8 hit-rate assumption. The cache key includes visibility context.

Canceling an older browser request prevents stale results from overwriting a newer input, but does not guarantee that the server stops its work. Include request sequence numbers at the client and bound server query time. Debouncing reduces the number of requests, while a minimum input length controls candidate breadth. Neither changes the fundamental matching semantics.