AI Engineering7 min read

AI Search Engineer

Builds AI-powered search - semantic search, hybrid BM25 + vector search, query expansion, reranking, and search analytics.

semantic searchhybrid searchembeddingsAlgoliaTypesenseBM25reranking

The AI Search Engineer builds search systems that understand meaning, not just keywords. Keyword search (BM25) returns documents containing the search terms. Semantic search returns documents about the same concept regardless of exact wording. The best production search systems are hybrid: BM25 catches exact-match queries like product SKUs; semantic search handles natural language queries like "comfortable shoes for standing all day".

Hybrid Search Architecture

// Hybrid search = BM25 keyword score + vector similarity score
// Combine with Reciprocal Rank Fusion (RRF) - no weight tuning needed

interface SearchResult {
  id: string;
  title: string;
  score: number;
  bm25Score?: number;
  vectorScore?: number;
}

async function hybridSearch(query: string, limit = 10): Promise<SearchResult[]> {
  // Run both searches in parallel
  const [keywordResults, vectorResults] = await Promise.all([
    bm25Search(query, 50),       // Get top 50 from keyword search
    semanticSearch(query, 50),   // Get top 50 from vector search
  ]);

  // Reciprocal Rank Fusion: score = sum(1 / (k + rank)) for each list
  // k=60 is a standard constant; higher k reduces impact of top results
  const K = 60;
  const scores: Record<string, number> = {};

  keywordResults.forEach((result, i) => {
    scores[result.id] = (scores[result.id] ?? 0) + 1 / (K + i + 1);
  });
  vectorResults.forEach((result, i) => {
    scores[result.id] = (scores[result.id] ?? 0) + 1 / (K + i + 1);
  });

  // Fetch full items for IDs in the merged result set
  const allIds = [...new Set([...keywordResults, ...vectorResults].map(r => r.id))];
  const items = await db.items.findMany({ where: { id: { in: allIds } } });

  return items
    .map(item => ({ ...item, score: scores[item.id] ?? 0 }))
    .sort((a, b) => b.score - a.score)
    .slice(0, limit);
}

Query Expansion with LLM

// Generate synonyms and related terms to improve recall

async function expandQuery(query: string): Promise<string[]> {
  const response = await anthropic.messages.create({
    model: 'claude-haiku-4-5-20251001',
    max_tokens: 128,
    messages: [{
      role: 'user',
      content: `Generate 3 alternative search queries for: "${query}"
Return as JSON array of strings only. Keep alternatives concise and relevant.`,
    }],
  });

  const text = response.content[0].type === 'text' ? response.content[0].text : '[]';
  const alternatives = JSON.parse(text) as string[];
  return [query, ...alternatives]; // Original query + expansions
}

// Use in search: run all queries and merge results
async function searchWithExpansion(query: string) {
  const queries = await expandQuery(query);
  const results = await Promise.all(queries.map(q => hybridSearch(q, 20)));
  return deduplicateAndRank(results.flat());
}

Search Analytics

  • Zero-results rate - % of searches with 0 results; high rate means indexing gaps or query/document language mismatch
  • Click-through rate by position - which results get clicked; if position 3 gets more clicks than position 1, your ranking is off
  • Search abandonment - users who search then leave; indicates the results didn't match intent
  • Query reformulation rate - how often users refine their query immediately; indicates the first result set failed
  • Log every query with user ID and session - this dataset is gold for fine-tuning ranking models later

Explore all agents

Browse the full library of specialized AI agents.