Build prefix suggestions from approved vocabulary, bind them to access scope and prevent private search histories from surfacing.
Retrieval autocomplete: useful suggestions without query leaks
Suggestion sources have different risks
An autocomplete menu may suggest known document titles, curated subject labels or popular past queries. Titles and labels can be approved at publication; raw query logs can contain names, account IDs and confidential incidents. Never turn query logs into public suggestions merely because the terms are frequent. Start with an approved catalog and a clear deletion path. If popularity is used, apply privacy review and minimum aggregation thresholds before a phrase enters the suggestion index. Safe query parsing still handles the submitted search after the user accepts or edits a suggestion.
Filter at candidate generation
A suggestion can leak a restricted document before the user submits a search. Build tenant-scoped suggestion collections or apply an authorization filter inside the suggestion query and recheck access before display. A site-specific prefix should never enumerate another site's vocabulary. Permission revisions and document deletion must invalidate suggestion caches. If a backend's completion feature cannot enforce the needed access boundary, do not index private titles in it. Tenant isolation covers the surrounding index and routing choices.
Match intent without damaging identifiers
For ordinary words, prefix and infix matching help when the user types the middle of a title. For serials and codes, preserve punctuation and case-normalization rules that fit the identifier scheme; a generic tokenizer may split P-47/AC into unrelated fragments. Rank suggestions by exact prefix, approved category and currentness before popularity. A suggestion is a proposed query, not an answer. Display its source type so a document title is not confused with a verified fact. Lexical identifier safety provides the corresponding search behavior.
Bound work at typing speed
Autocomplete runs for partial inputs, so it can issue many requests per user. Require a meaningful minimum prefix, debounce client requests, cancel stale responses and cap returned items. The server must still rate-limit and enforce length limits because clients can bypass the browser. A trie, edge n-gram index or specialized completion structure can avoid scanning the full vocabulary. Compare its memory and rebuild cost with observed traffic; a small approved catalog may work with a simple sorted prefix scan.
Evaluate visibility and usefulness separately
Test suggestions for common prefixes, typos, IDs, multilingual scripts and document removals. Verify that a restricted title never appears in another tenant's menu, network response or analytics log. Measure acceptance and downstream search success by query slice, but do not treat acceptance as relevance proof: prominent suggestions can influence what users choose. Position bias applies to menus too. A suggestion that raises clicks while suppressing rare but valid queries can make retrieval worse.
Worked contract
from dataclasses import dataclass
@dataclass(frozen=True)
class ApprovedSuggestion:
text: str
tenant: str
current: bool
def suggest(prefix: str, authenticated_tenant: str,
catalog: list[ApprovedSuggestion], limit: int = 5) -> list[str]:
normalized = prefix.strip().casefold()
if len(normalized) < 3 or not 1 <= limit <= 8:
return []
matches = (item.text for item in catalog
if item.tenant == authenticated_tenant and item.current
and item.text.casefold().startswith(normalized))
return sorted(set(matches), key=lambda value: (len(value), value))[:limit]
catalog = [
ApprovedSuggestion("Pump P-47 seal guide", "north", True),
ApprovedSuggestion("Pump P-48 pressure limits", "north", True),
ApprovedSuggestion("Pump P-47 incident", "south", True),
ApprovedSuggestion("Pump P-42 retired", "north", False),
]
assert suggest("Pump P-4", "north", catalog) == [
"Pump P-47 seal guide", "Pump P-48 pressure limits"]Performance and operating cost
This demonstration scans A approved suggestions in O(A) time and uses O(M) memory for M matched strings before sorting them in O(M log M) time. A prefix index can reduce lookup work, but consumes storage and must track updates and revocations. The same current authorization check remains necessary after lookup. Client-side debouncing reduces normal request volume, not server-side abuse; rate limits and minimum prefix length must be enforced by the service.
Common Mistakes
- Building public suggestions directly from raw search logs.
- Filtering restricted suggestions only after returning them to the browser.
- Assuming browser autocomplete settings protect server-side query history.
- Letting a generic tokenizer strip significant punctuation from product IDs.
Validation drill
Create 74 approved titles, 12 removed titles and two tenant scopes. Test 20 prefixes, including punctuation-heavy IDs and a restricted unique prefix; inspect both response payloads and logs for leaks.
