Users expect search to understand intent, not just keywords. Semantic search, hybrid retrieval, and LLM-powered reranking are transforming what's possible. Here's how to build search that actually works.
Limitations of Traditional Search
Keyword search misses synonyms ('car' vs 'automobile'), ignores intent ('best laptop for programming'), and can't understand context. Users have learned to craft keywords, but they shouldn't have to.
Semantic Search: Embeddings in Action
Semantic search converts queries and documents to vectors, then finds similar vectors. This captures meaning, not just words.
async function semanticSearch(query: string, k: number) {
const queryEmbedding = await embed(query);
const results = await vectorDB.search({
vector: queryEmbedding,
limit: k
});
return results;
}Hybrid Search: BM25 + Vectors
Neither keyword nor semantic search is perfect alone. BM25 excels at exact matches and rare terms. Semantic search handles synonyms and intent. Combining them gets the best of both.
async function hybridSearch(query: string, k: number, alpha = 0.5) {
const bm25Results = await keywordSearch(query, k * 2);
const semanticResults = await semanticSearch(query, k * 2);
// Reciprocal Rank Fusion
const scores = new Map();
bm25Results.forEach((doc, i) => {
scores.set(doc.id, (scores.get(doc.id) || 0) + (1 - alpha) / (i + 60));
});
semanticResults.forEach((doc, i) => {
scores.set(doc.id, (scores.get(doc.id) || 0) + alpha / (i + 60));
});
return [...scores.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, k);
}LLM Reranking: The Quality Multiplier
Retrieve many candidates cheaply, then use an LLM to rerank the top results. This dramatically improves relevance.
async function rerankWithLLM(query: string, candidates: Doc[]) {
const prompt = \`Given the query: "\${query}"
Rank these documents by relevance (most relevant first):
\${candidates.map((d, i) => \`\${i + 1}. \${d.title}: \${d.snippet}\`).join('\n')}
Return only the numbers in order.\`;
const ranking = await llm.generate(prompt);
return reorder(candidates, ranking);
}Building a Complete Pipeline
1. Query understanding (expand, correct, extract intent). 2. Retrieval (hybrid search). 3. Reranking (LLM or cross-encoder). 4. Presentation (snippets, highlights, explanations).
AI-powered search is not just about better algorithms—it's about understanding what users actually want and delivering it. Start with hybrid search, add reranking for quality, and iterate based on user feedback.
