Claude Code prompt to add a search feature
With this prompt Claude Code wires search end to end: a sanitized backend endpoint using your database's text search, plus a debounced frontend input that highlights matches and cancels stale requests. Edge cases that usually surface in production, blank queries and apostrophes in names, are specified up front and covered by tests.
Last updated
Add search over {{resource}} records to this codebase. Backend: a GET /search endpoint accepting q, page, and a filter param for status, returning ranked matches with a total count. Use {{database}} native text search (an ILIKE fallback is fine below a few thousand rows); note in a comment where to swap in a dedicated index later. Sanitize the query string, cap its length at 200 characters, and return an empty result set for blank input rather than an error. Frontend: a debounced input (about 300ms) in the existing header, a results list with the matched term highlighted, an explicit no-results state, and cancellation of stale in-flight requests so a slow early response cannot overwrite a newer one. Tests: relevance ordering on a seeded fixture, the blank-query case, and a special-characters query such as O'Brien. Run everything, then list changed files grouped by backend and frontend.Customize it
Runs in your browser. Nothing you type here is sent anywhere.
Add search over users records to this codebase. Backend: a GET /search endpoint accepting q, page, and a filter param for status, returning ranked matches with a total count. Use PostgreSQL native text search (an ILIKE fallback is fine below a few thousand rows); note in a comment where to swap in a dedicated index later. Sanitize the query string, cap its length at 200 characters, and return an empty result set for blank input rather than an error. Frontend: a debounced input (about 300ms) in the existing header, a results list with the matched term highlighted, an explicit no-results state, and cancellation of stale in-flight requests so a slow early response cannot overwrite a newer one. Tests: relevance ordering on a seeded fixture, the blank-query case, and a special-characters query such as O'Brien. Run everything, then list changed files grouped by backend and frontend.
Same task in other tools
Questions about this prompt
Why database text search instead of Elasticsearch?
For most CRUD apps it ships the feature without new infrastructure, and the prompt marks the exact swap point for a dedicated engine later. If you already run one, name it and Claude Code will target its client instead.
Search returns results in tests but feels slow in the app. What do I do?
Ask Claude Code to run EXPLAIN on the search query and add an index migration for whatever it finds, then confirm the debounce is actually attached to the input rather than the fetch wrapper.
How do I extend this to search several fields with different priorities?
List the columns in ranked order in the backend paragraph, for example "title weighted above description, description above comments". Without that the agent tends to index only the obvious name column.