How it works
Autocomplete answers an incomplete input, not a completed search. Set the contract first: prefix-only or substring matches, result count, normalization (folding case and accents so “Café” matches “cafe”), supported languages, regional differences, personalization, and empty-input behavior. A useful first version returns five popular completed searches that begin with the typed prefix. Spell correction is a separate feature with its own index and ranking work.
The client should debounce, meaning it waits for a short pause in typing before sending a request. If every key fired a request, typing “camera” would send six. With an example 100 ms debounce, a person who pauses only after “cam” and “camera” sends two. The client should also ignore late responses for older inputs, because responses can arrive out of order. Cancellation helps but can race with an already completed request, so tag each request with a sequence number and show only the newest.
Define a high-percentile latency target from keystroke to visible suggestions, not just the server average. As an example budget, if a list should appear within 200 ms of the request, an 80 ms mobile round trip and 20 ms of rendering leave the server 100 ms at p99. Very short prefixes can be disproportionately expensive because they cover large candidate sets. You may not need any of this: a country picker with 200 fixed entries can ship its list with the page and filter in the browser.
A user types “ca” and then “cat,” but the “ca” response arrives last. Which suggestions should remain visible?
Only results for “cat.” Tag requests by input or sequence and discard stale responses even if cancellation was attempted.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Specify matching, ranking, context, and latency before choosing an index.
- Debouncing reduces traffic; stale-response handling preserves correctness.