Implementing Autocomplete Search for Web Applications
Autocomplete is a search field that shows suggestions as you type. Technically, it's an intersection of several tasks: fast fuzzy search through an index, debounce to avoid server overload, correct keyboard handling (ARIA combobox), canceling stale requests. Each is solved separately; together they form a complete component.
Search Architecture
Client-side — data loads at once, search in browser. Suitable for up to ~50,000 records. Libraries: Fuse.js (fuzzy), MiniSearch (fulltext). No latency after loading.
Server-side — each keystroke queries the server. Needs debounce and request cancellation. Works for large data. Elasticsearch suggest API, Typesense, PostgreSQL trgm.
Hybrid — cache popular queries in memory, rare ones go to server.
Basic Hook with Debounce and Cancellation
import { useState, useEffect, useRef, useCallback } from 'react';
type SearchResult = {
id: string;
title: string;
category?: string;
url: string;
};
function useAutocomplete(
fetchFn: (query: string, signal: AbortSignal) => Promise<SearchResult[]>,
delay = 250
) {
const [query, setQuery] = useState('');
const [results, setResults] = useState<SearchResult[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const abortRef = useRef<AbortController | null>(null);
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const search = useCallback((value: string) => {
setQuery(value);
if (timerRef.current) clearTimeout(timerRef.current);
if (abortRef.current) abortRef.current.abort();
if (value.trim().length < 2) {
setResults([]);
return;
}
timerRef.current = setTimeout(async () => {
const controller = new AbortController();
abortRef.current = controller;
setLoading(true);
setError(null);
try {
const data = await fetchFn(value, controller.signal);
if (!controller.signal.aborted) {
setResults(data);
}
} catch (err) {
if (err instanceof Error && err.name !== 'AbortError') {
setError(err);
}
} finally {
if (!controller.signal.aborted) {
setLoading(false);
}
}
}, delay);
}, [fetchFn, delay]);
useEffect(() => () => {
if (timerRef.current) clearTimeout(timerRef.current);
if (abortRef.current) abortRef.current.abort();
}, []);
return { query, results, loading, error, search };
}
Component with ARIA Combobox
Correct implementation per ARIA combobox pattern:
import { useId, useRef, useState } from 'react';
interface AutocompleteProps {
placeholder?: string;
onSelect: (result: SearchResult) => void;
fetchResults: (query: string, signal: AbortSignal) => Promise<SearchResult[]>;
}
export function Autocomplete({ placeholder, onSelect, fetchResults }: AutocompleteProps) {
const id = useId();
const listId = `${id}-listbox`;
const inputRef = useRef<HTMLInputElement>(null);
const listRef = useRef<HTMLUListElement>(null);
const { query, results, loading, search } = useAutocomplete(fetchResults);
const [activeIndex, setActiveIndex] = useState(-1);
const [open, setOpen] = useState(false);
const isOpen = open && (results.length > 0 || loading);
const handleKeyDown = (e: React.KeyboardEvent) => {
switch (e.key) {
case 'ArrowDown':
e.preventDefault();
setActiveIndex((i) => Math.min(i + 1, results.length - 1));
break;
case 'ArrowUp':
e.preventDefault();
setActiveIndex((i) => Math.max(i - 1, -1));
break;
case 'Enter':
if (activeIndex >= 0 && results[activeIndex]) {
onSelect(results[activeIndex]);
setOpen(false);
setActiveIndex(-1);
}
break;
case 'Escape':
setOpen(false);
setActiveIndex(-1);
inputRef.current?.focus();
break;
}
};
return (
<div className="autocomplete" role="combobox" aria-expanded={isOpen} aria-haspopup="listbox">
<input
ref={inputRef}
type="search"
placeholder={placeholder}
value={query}
aria-autocomplete="list"
aria-controls={listId}
aria-activedescendant={activeIndex >= 0 ? `${id}-option-${activeIndex}` : undefined}
onChange={(e) => {
search(e.target.value);
setOpen(true);
setActiveIndex(-1);
}}
onFocus={() => query.length >= 2 && setOpen(true)}
onBlur={() => setTimeout(() => setOpen(false), 150)}
onKeyDown={handleKeyDown}
/>
{isOpen && (
<ul
ref={listRef}
id={listId}
role="listbox"
className="autocomplete__dropdown"
>
{loading && (
<li role="option" aria-selected="false" className="autocomplete__loading">
Searching...
</li>
)}
{results.map((result, index) => (
<li
key={result.id}
id={`${id}-option-${index}`}
role="option"
aria-selected={index === activeIndex}
className={`autocomplete__option ${index === activeIndex ? 'autocomplete__option--active' : ''}`}
onMouseDown={() => {
onSelect(result);
setOpen(false);
}}
onMouseEnter={() => setActiveIndex(index)}
>
<HighlightMatch text={result.title} query={query} />
{result.category && (
<span className="autocomplete__category">{result.category}</span>
)}
</li>
))}
</ul>
)}
</div>
);
}
Match Highlighting
function HighlightMatch({ text, query }: { text: string; query: string }) {
if (!query.trim()) return <span>{text}</span>;
const escaped = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const parts = text.split(new RegExp(`(${escaped})`, 'gi'));
return (
<span>
{parts.map((part, i) =>
part.toLowerCase() === query.toLowerCase()
? <mark key={i}>{part}</mark>
: <span key={i}>{part}</span>
)}
</span>
);
}
Server-side: Elasticsearch Suggest
// POST /api/suggest
async function suggestHandler(req: Request) {
const { q } = await req.json();
if (!q || q.length < 2) return Response.json({ hits: [] });
const response = await esClient.search({
index: 'products',
body: {
suggest: {
title_suggest: {
prefix: q,
completion: {
field: 'title.suggest', // completion field type
size: 10,
fuzzy: { fuzziness: 'AUTO' },
},
},
},
// Additionally — fulltext search for more relevant results
query: {
multi_match: {
query: q,
fields: ['title^3', 'description', 'tags^2'],
type: 'bool_prefix',
},
},
_source: ['id', 'title', 'category', 'url', 'image'],
size: 10,
},
});
return Response.json({
hits: response.hits.hits.map((h) => h._source),
});
}
Index with completion field:
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"suggest": {
"type": "completion",
"analyzer": "standard"
},
"keyword": { "type": "keyword" }
}
}
}
}
}
PostgreSQL with pg_trgm
For projects without Elasticsearch:
-- Extension for trigram search
CREATE EXTENSION IF NOT EXISTS pg_trgm;
-- Index
CREATE INDEX products_title_trgm_idx ON products
USING gin (title gin_trgm_ops);
-- Query with suggestions
SELECT id, title, category, similarity(title, $1) AS score
FROM products
WHERE title % $1 -- trigram match
OR title ILIKE $1 || '%' -- starts-with (faster for exact matches)
ORDER BY
title ILIKE $1 || '%' DESC, -- starts-with priority
score DESC
LIMIT 10;
Client-side Caching
const cache = new Map<string, { data: SearchResult[]; ts: number }>();
const TTL = 30_000; // 30 seconds
async function fetchWithCache(query: string, signal: AbortSignal) {
const cached = cache.get(query);
if (cached && Date.now() - cached.ts < TTL) {
return cached.data;
}
const res = await fetch(`/api/suggest?q=${encodeURIComponent(query)}`, { signal });
const data = await res.json();
cache.set(query, { data: data.hits, ts: Date.now() });
if (cache.size > 100) {
// Remove oldest entry
cache.delete(cache.keys().next().value);
}
return data.hits;
}
Timeline
- Simple autocomplete (fetch + debounce, no ARIA, no highlighting): 4–8 hours
- Full component (ARIA combobox, keyboard navigation, highlighting, client cache): 2–3 days
- With server Elasticsearch index (completion mapping, index setup, fuzzy): another 1–2 days







