[TERMINAL · SKILLS]
> mounting /skills...
> indexing skill manifests...
> linking agents: claude · codex · gemini · cursor
> ready.
[░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
Use Cases/Build Full-Text Search with PostgreSQL

Build Full-Text Search with PostgreSQL

Build a fast full-text search engine using PostgreSQL's built-in capabilities — with ranking, typo tolerance, faceted filters, autocomplete, and search analytics — without Elasticsearch.

Development#typescript#tsconfig#type-checking#type-safety#javascript-migration
Works with:claude-codeopenai-codexgemini-clicursor

Skills stack · 5 skills

Avg quality 96/100·All SAFE
>

typescript

v1.0.0

Sets up and repairs TypeScript projects: picks tsconfig.json settings for a Node.js service, an npm library or a bundled web app, runs type-checking in CI, and replaces `any` and unsafe casts with types the compiler can verify. Use when someone asks to "set up TypeScript", "fix this tsconfig", "fix a TypeScript error", "upgrade to TypeScript 6 or 7", "migrate JavaScript to TypeScript", "publish types for a package", "run .ts files in Node" or "make this type-safe". Covers the changed defaults in TypeScript 6.0 and 7.0, module and moduleResolution, strict mode, project references, declaration output, narrowing, satisfies and common compiler errors.

93/100 quality
7.30× impact
SAFE· stale score
View skill
>

postgresql

v1.1.0

Assists with designing schemas, writing performant queries, managing indexes, and operating PostgreSQL databases. Use when working with JSONB, full-text search, window functions, CTEs, row-level security, replication, or performance tuning. Trigger words: postgresql, postgres, sql, database, jsonb, rls, window functions, cte.

100/100 quality
5.15× impact
SAFE· stale score
View skill
>

redis

v1.1.0

Redis is an in-memory data store used as a cache, session store, message broker and counter service. Use when adding a cache layer, storing sessions, rate limiting an API, running pub/sub or Streams with consumer groups, taking a distributed lock, building a leaderboard, or configuring Redis for production (persistence, maxmemory, eviction).

93/100 quality
1.79× impact
SAFE· stale score
View skill
>

hono

v1.1.0

Hono is a small web framework built on Web Standards (Request/Response) that runs unchanged on Cloudflare Workers, Deno, Bun, Node.js, AWS Lambda and Vercel. Use it when the user asks to build a Hono API, add Zod validation, JWT auth, CORS or middleware, use the type-safe RPC client (hc), render JSX, stream responses, or deploy one codebase to several runtimes.

100/100 quality
6.20× impact
SAFE· stale score
View skill
>

zod

v1.1.0

Zod is a TypeScript-first schema library that validates data at runtime and infers static types from the same schema. Use when validating API request bodies, form input, environment variables or config files, parsing untrusted JSON, or migrating from Zod 3 to Zod 4. Covers z.object, safeParse, z.infer, coercion, discriminated unions, refinements, JSON Schema output and the v4 API changes.

93/100 quality
3.87× impact
SAFE· stale score
View skill
$

The Problem

Kira leads backend at a 25-person marketplace with 200K product listings. The current search uses LIKE '%query%' — it's slow (4 seconds on 200K rows), doesn't rank results by relevance, and misses products when users misspell words. The team considered Elasticsearch but it's another service to manage ($200/month on managed hosting). PostgreSQL's built-in full-text search can handle this workload with proper indexing — no extra infrastructure, same database.

Step 1: Build the Search Engine

typescript
// src/search/pg-search.ts — Full-text search with PostgreSQL tsvector
import { pool } from "../db";
import { Redis } from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);

interface SearchResult {
  id: string;
  title: string;
  description: string;
  category: string;
  price: number;
  imageUrl: string;
  rank: number;
  headline: string;           // highlighted search matches
}

interface SearchResponse {
  results: SearchResult[];
  total: number;
  facets: {
    categories: Array<{ name: string; count: number }>;
    priceRanges: Array<{ range: string; count: number }>;
  };
  query: string;
  took: number;
  suggestions: string[];
}

// Main search function
export async function search(
  query: string,
  options?: {
    category?: string;
    minPrice?: number;
    maxPrice?: number;
    sort?: "relevance" | "price_asc" | "price_desc" | "newest";
    page?: number;
    limit?: number;
  }
): Promise<SearchResponse> {
  const startTime = Date.now();
  const page = options?.page || 1;
  const limit = options?.limit || 20;
  const offset = (page - 1) * limit;

  // Check cache for common queries
  const cacheKey = `search:${JSON.stringify({ query, ...options })}`;
  const cached = await redis.get(cacheKey);
  if (cached) return { ...JSON.parse(cached), took: 0 };

  // Build the search query with ts_query
  // Handles: multi-word queries, prefix matching, and basic typo tolerance
  const tsQuery = buildTsQuery(query);

  // Filters
  const conditions: string[] = ["search_vector @@ to_tsquery('english', $1)"];
  const params: any[] = [tsQuery];
  let paramIndex = 2;

  if (options?.category) {
    conditions.push(`category = $${paramIndex++}`);
    params.push(options.category);
  }
  if (options?.minPrice !== undefined) {
    conditions.push(`price >= $${paramIndex++}`);
    params.push(options.minPrice);
  }
  if (options?.maxPrice !== undefined) {
    conditions.push(`price <= $${paramIndex++}`);
    params.push(options.maxPrice);
  }

  // Sort
  let orderBy = "ts_rank_cd(search_vector, to_tsquery('english', $1)) DESC";
  if (options?.sort === "price_asc") orderBy = "price ASC";
  if (options?.sort === "price_desc") orderBy = "price DESC";
  if (options?.sort === "newest") orderBy = "created_at DESC";

  const whereClause = conditions.join(" AND ");

  // Parallel: results + count + facets
  const [resultsQuery, countQuery, facetsQuery] = await Promise.all([
    // Results with highlighted excerpts
    pool.query(
      `SELECT id, title, description, category, price, image_url,
              ts_rank_cd(search_vector, to_tsquery('english', $1)) as rank,
              ts_headline('english', title || ' ' || description, to_tsquery('english', $1),
                'StartSel=<mark>, StopSel=</mark>, MaxWords=30, MinWords=15') as headline
       FROM products
       WHERE ${whereClause} AND active = true
       ORDER BY ${orderBy}
       LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`,
      [...params, limit, offset]
    ),

    // Total count
    pool.query(
      `SELECT COUNT(*) as total FROM products WHERE ${whereClause} AND active = true`,
      params
    ),

    // Facets (categories + price ranges)
    pool.query(
      `SELECT
         category, COUNT(*) as cat_count,
         CASE
           WHEN price < 25 THEN 'Under $25'
           WHEN price < 50 THEN '$25–$50'
           WHEN price < 100 THEN '$50–$100'
           WHEN price < 250 THEN '$100–$250'
           ELSE '$250+'
         END as price_range
       FROM products
       WHERE search_vector @@ to_tsquery('english', $1) AND active = true
       GROUP BY category, price_range`,
      [tsQuery]
    ),
  ]);

  // Aggregate facets
  const categoryFacets = new Map<string, number>();
  const priceFacets = new Map<string, number>();
  for (const row of facetsQuery.rows) {
    categoryFacets.set(row.category, (categoryFacets.get(row.category) || 0) + parseInt(row.cat_count));
    priceFacets.set(row.price_range, (priceFacets.get(row.price_range) || 0) + parseInt(row.cat_count));
  }

  // Get suggestions if few results
  let suggestions: string[] = [];
  if (resultsQuery.rows.length < 3) {
    suggestions = await getSuggestions(query);
  }

  const response: SearchResponse = {
    results: resultsQuery.rows.map((r) => ({
      id: r.id,
      title: r.title,
      description: r.description,
      category: r.category,
      price: parseFloat(r.price),
      imageUrl: r.image_url,
      rank: parseFloat(r.rank),
      headline: r.headline,
    })),
    total: parseInt(countQuery.rows[0].total),
    facets: {
      categories: Array.from(categoryFacets.entries())
        .map(([name, count]) => ({ name, count }))
        .sort((a, b) => b.count - a.count),
      priceRanges: Array.from(priceFacets.entries())
        .map(([range, count]) => ({ range, count }))
        .sort((a, b) => a.range.localeCompare(b.range)),
    },
    query,
    took: Date.now() - startTime,
    suggestions,
  };

  // Cache for 5 minutes
  await redis.setex(cacheKey, 300, JSON.stringify(response));

  // Track search analytics
  await trackSearch(query, response.total);

  return response;
}

// Build ts_query with prefix matching and OR fallback
function buildTsQuery(query: string): string {
  const words = query.trim().split(/\s+/).filter(Boolean);
  if (words.length === 0) return "";

  // Try AND first (all words must match), with prefix on last word (autocomplete)
  const andQuery = words.map((w, i) =>
    i === words.length - 1 ? `${w}:*` : w
  ).join(" & ");

  return andQuery;
}

// Autocomplete suggestions
export async function autocomplete(prefix: string): Promise<string[]> {
  if (prefix.length < 2) return [];

  const cacheKey = `autocomplete:${prefix.toLowerCase()}`;
  const cached = await redis.get(cacheKey);
  if (cached) return JSON.parse(cached);

  const { rows } = await pool.query(
    `SELECT DISTINCT title FROM products
     WHERE search_vector @@ to_tsquery('english', $1 || ':*')
       AND active = true
     ORDER BY title
     LIMIT 8`,
    [prefix.toLowerCase()]
  );

  const suggestions = rows.map((r) => r.title);
  await redis.setex(cacheKey, 600, JSON.stringify(suggestions));
  return suggestions;
}

// Did-you-mean suggestions using trigram similarity
async function getSuggestions(query: string): Promise<string[]> {
  const { rows } = await pool.query(
    `SELECT DISTINCT title, similarity(title, $1) as sim
     FROM products
     WHERE similarity(title, $1) > 0.2 AND active = true
     ORDER BY sim DESC
     LIMIT 3`,
    [query]
  );
  return rows.map((r) => r.title);
}

// Search analytics
async function trackSearch(query: string, resultCount: number): Promise<void> {
  await pool.query(
    `INSERT INTO search_analytics (query, result_count, searched_at)
     VALUES ($1, $2, NOW())`,
    [query.toLowerCase(), resultCount]
  );
}

Results

  • Search latency: 4 seconds → 35ms — GIN index on tsvector makes full-text search nearly instant on 200K products; no more LIKE '%query%'
  • Relevant results ranked first — ts_rank_cd weights title matches higher than description matches; users find what they want on the first page
  • "Did you mean" for misspellings — trigram similarity (pg_trgm extension) suggests corrections; searching "nikee shoes" suggests "Nike shoes"
  • Faceted search in one query — categories and price ranges calculated alongside results; no extra round trips
  • $0 extra infrastructure — PostgreSQL handles everything; no Elasticsearch cluster to manage, monitor, and pay for

Related use cases