Prompt
How do I add autocomplete and instant search to an internal tool?
Latest observation
To add autocomplete and instant search to an internal tool, the usual pattern is:
- Send each keystroke (or a debounced version) to your backend
- Return small, fast search results
- Render suggestions immediately
- Optimize for latency and relevance
Basic approach
1) Use a search endpoint
Create an endpoint like:
GET /api/search?q=acme
It should return only the top matches, e.g.:
{
"results": [
{ "id": 1, "name": "Acme Corp" },
{ "id": 2, "name": "Acme Logistics" }
]
}
For autocomplete, you may want a separate endpoint:
GET /api/autocomplete?q=acm
That can return just suggestions, not full records.
2) Debounce input
Don’t query on every single keypress immediately. Wait ~150–300 ms after the user stops typing.
Example behavior:
- User types
a - then
ac - then
acm - only one request is sent after typing pauses
This reduces load and makes the UI smoother.
3) Show results as the user types
Update the dropdown/list live:
- highlight matching text
- show “no results” when appropriate
- allow keyboard navigation
- support Enter to select
4) Make search fast
For internal tools, search often needs to feel instant. Good options:
- Database indexing: add indexes on searchable columns
- Full-text search: PostgreSQL full-text search, trigram indexes, etc.
- Dedicated search engine: Elasticsearch, OpenSearch, Meilisearch, Typesense, Algolia
If your dataset is small, a well-indexed SQL query may be enough.
If it’s large or needs fuzzy matching, a search engine is better.
Recommended implementation patterns
If you just need a simple internal tool
Use:
- frontend autocomplete component
- backend search API
- database indexes
- debounce + caching
This is often enough.
If you need fuzzy, typo-tolerant, fast search
Use:
- Meilisearch or Typesense for ease of setup
- Elasticsearch/OpenSearch for advanced search needs
These give:
- typo tolerance
- prefix search
- ranking
- filters/facets
Frontend example concept
When the user types:
- wait 200 ms
- call
/api/search?q=... - show dropdown results
- cancel stale requests if the user keeps typing
Important:
- use request cancellation or ignore outdated responses
- cache recent queries if useful
Backend tips
- Return only top 5–10 suggestions for autocomplete
- Keep payloads small
- Add pagination for full search results
- Log failed/slow searches
- Measure latency
UX tips
A good internal search should:
- search while typing
- support arrows/Enter/Esc
- show loading state
- handle empty query gracefully
- preserve recent searches if helpful
Example tech choices
Option A: SQL-only
Good for:
- small/medium datasets
- minimal infrastructure
Use:
- PostgreSQL
ILIKE - indexes
- trigram search for partial matches
Option B: Search service
Good for:
- larger datasets
- fast autocomplete
- typo tolerance
Use:
- Meilisearch / Typesense for simplicity
- Elasticsearch / OpenSearch for scale and advanced needs
Rule of thumb
If your internal tool has:
- fewer than a few hundred thousand records and simple matching → start with SQL + indexes
- lots of records, fuzzy search, or multiple filters → use a search engine
If you want, I can also give you:
- a React autocomplete example
- a Node/Express backend example
- a PostgreSQL search setup
- a recommended architecture for your current stack