Book a 30-min callBook a call

System design: search autocomplete (typeahead)

How to design search autocomplete: a trie with top-k per node, prefix indexes, suggestions from query logs, edge caching, typo tolerance and the client.

A search box with a short prefix above a list of four suggestions, linked by an arrow to a small tree of circles with one path highlighted.
Written by
BYD Tech
Published
Topic
Deep dives
Reading time
13 min read

Questions people ask

What data structure is best for autocomplete?

For query suggestions, a trie or finite-state transducer that stores the top k completions at every prefix gives lookups proportional to the prefix length. If you already run a search engine, an edge n-gram field or a completion suggester avoids building your own.

How long should I debounce autocomplete requests?

There is no single right value. Start with a short delay, around 100 to 200 milliseconds, measure how many requests per search you send and how the list feels to fast typists, and tune from there. Always cancel the previous request as well.

How many suggestions should autocomplete show?

Few enough to scan at a glance, commonly between five and ten. Elasticsearch’s completion suggester returns five by default. On small screens, show fewer.

How do I keep offensive or private queries out of suggestions?

Filter when building the index and again when serving: a blocklist with spelling variants, a classifier, pattern rules for emails and phone numbers, a minimum number of distinct users per query, and a removal path that takes effect without a rebuild.

Should autocomplete be personalized?

Often a little: recent searches and the user’s locale help. Keep the global list shared and cacheable, merge a small personal list on top, and drop the personal part when it is slow rather than delaying the whole response.

Building something like this?