Building Search with Claude Code

Full-text search, filters, ranking. The mechanics Claude handles well — and the decisions that require your input.

Search is one of those features that's easy to add badly and hard to add well. "LIKE '%query%'" technically works; it also locks the table and returns results in arbitrary order. Claude Code builds real search when you give it the right requirements.

Choosing an approach

Before implementing, specify what you need:

I need search for [what users are searching]. Help me choose:
- PostgreSQL full-text search: good for < 10M rows, no extra infrastructure
- Elasticsearch/OpenSearch: better for complex queries, large datasets, relevance tuning
- Typesense/Meilisearch: easier to operate, good relevance out of the box

Scale: [rows to index]
Query types: [simple keyword / phrase / fuzzy / faceted]
Update frequency: [how often data changes]

Which fits?

PostgreSQL full-text search

Implement full-text search using PostgreSQL. Index these fields:
- [field1]: weight A (most important)
- [field2]: weight B
- [field3]: weight C (least important)

Requirements:
- Store tsvector in a generated column (not computed at query time)
- Create GIN index on the tsvector column
- Search function returns results ranked by relevance
- Support phrase search and partial word matching
- Highlight matched terms in results

Show me the migration and the search function.

The "store tsvector in a generated column" is the key performance decision. Computing it at query time on every row is slow. Pre-computing it and indexing it makes search fast.

Filters alongside search

Add filters to the search:
- [field1]: exact match filter
- [field2]: range filter (min/max)
- [field3]: multi-select filter (any of these values)

Filters should combine with AND logic.
Query should use indexes for both text search and filters.
Show me how to create compound indexes if needed.

Autocomplete

Add autocomplete for the search box. Requirements:
- Return suggestions as the user types (from 2+ characters)
- Index prefix matches: "re" should match "react", "redis", "refactor"
- Max 10 suggestions, ranked by [popularity/recency/alphabetical]
- Fast: < 100ms response time
- Don't hit main search index (separate lightweight index)

Search analytics

Log search queries for analytics:
- Query text (sanitized — no PII)
- Result count returned
- Whether user clicked a result (if you have click tracking)
- Zero-result queries (these show you what's missing from your content)

Weekly report: top queries, zero-result queries, trending searches.

Zero-result queries are the most actionable analytics. They tell you exactly what users want that you don't have.

Testing search

Write tests for the search function. Test:
- Exact match: query matches document content exactly
- Partial match: query is a substring of content
- Multi-word: all words present but not adjacent
- No results: query matches nothing
- Ranking: more relevant results appear before less relevant
- Filter: results match filter criteria
- Filter + search: combination works correctly

Search implementation prompts are in the Agent Prompt Playbook. $29.