cd /news/ai-search/hybrid-search-in-postgres-when-embed… · home › topics › ai-search › article
[ARTICLE · art-178175] src=danholloran.me ↗ pub= topic=ai-search verified=true sentiment=· neutral

Hybrid Search in Postgres: When Embeddings Miss the Exact Match

Postgres can run keyword and vector search in one query and fuse the results with reciprocal rank fusion (RRF), avoiding a second database or reranking service when embeddings miss exact strings like error codes or SKUs, according to a guide following Supabase's hybrid search documentation. The approach stores a generated tsvector column with a GIN index and a pgvector embedding column with an HNSW index, then merges the two ranked lists in a hybrid_search SQL function using score(doc) = Σ 1/(k + rank_in_list), with k defaulting to 50 in Supabase's reference implementation versus 60 in the original RRF paper. A full outer join ensures documents found by only one search still appear in the final results, and each CTE pulls twice as many candidates as requested, capped at 30.

recommendation

Add a generated tsvector column with a GIN index alongside your pgvector HNSW index and merge both ranked lists with RRF (k=50) via a full outer join to catch exact error codes and SKUs embeddings miss.

more in playbook: Retrieval and RAG
read5 min views1 publishedNov 28, 2026
Hybrid Search in Postgres: When Embeddings Miss the Exact Match
Image: Danholloran (auto-discovered)

You ship a RAG feature, it demos beautifully, and then a user pastes ERR_SSL_PROTOCOL_ERROR into the search box. The top result is a vaguely related page about TLS certificates. The page that literally contains that error string is ranked eleventh. Nothing is broken. Embeddings are doing exactly what they were built to do: matching meaning. An error code, a SKU, a function name, or a person's surname has very little meaning to match on.

The fix most teams reach for is a second database or a reranking service. Before you do that, know that Postgres can run both kinds of search and merge them in a single query.

Two searches, two blind spots #

Keyword search and semantic search fail in opposite directions.

  • Full-text search (Postgrestsvector ) is great at exact tokens. Search "invoice 4471" and it finds the document with "invoice 4471" in it. It is bad at synonyms: "cancel my plan" won't find a doc titled "Ending your subscription."
  • Vector search (pgvector) is great at paraphrase. It finds "Ending your subscription" for "cancel my plan." It is unreliable on rare literal strings, because those tokens barely move an embedding.

Hybrid search runs both and fuses the results. The tricky part is the fusion. A ts_rank_cd score and a cosine distance are on completely different scales, so you can't add them up. Reciprocal rank fusion (RRF) sidesteps that by ignoring the scores and using only each document's position in each list:

score(doc) = Σ  1 / (k + rank_in_list)

A document ranked 1st in keyword search and 4th in vector search scores 1/(k+1) + 1/(k+4). A document found by only one search gets nothing from the other. k is a smoothing constant that keeps a single #1 ranking from swamping everything else. The original RRF paper used 60. Supabase's reference implementation defaults to 50. Either is a sane start.

The schema and the function #

This follows the pattern in Supabase's hybrid search guide, adjusted to use cosine distance. It runs on any Postgres with the vector extension.

create extension if not exists vector;

create table documents (
  id bigint primary key generated always as identity,
  content text not null,
  fts tsvector generated always as (to_tsvector('english', content)) stored,
  embedding vector(512)
);

create index on documents using gin (fts);
create index on documents using hnsw (embedding vector_cosine_ops);

The fts column is generated, so it never drifts out of sync with content. The HNSW index operator class has to match the distance operator you query with: vector_cosine_ops pairs with <=>.

create or replace function hybrid_search(
  query_text text,
  query_embedding vector(512),
  match_count int,
  full_text_weight float = 1,
  semantic_weight float = 1,
  rrf_k int = 50
)
returns setof documents
language sql
as $$
with full_text as (
  select id,
    row_number() over (
      order by ts_rank_cd(fts, websearch_to_tsquery(query_text)) desc
    ) as rank_ix
  from documents
  where fts @@ websearch_to_tsquery(query_text)
  order by rank_ix
  limit least(match_count, 30) * 2
),
semantic as (
  select id,
    row_number() over (order by embedding <=> query_embedding) as rank_ix
  from documents
  order by rank_ix
  limit least(match_count, 30) * 2
)
select documents.*
from full_text
full outer join semantic on full_text.id = semantic.id
join documents on coalesce(full_text.id, semantic.id) = documents.id
order by
  coalesce(1.0 / (rrf_k + full_text.rank_ix), 0.0) * full_text_weight +
  coalesce(1.0 / (rrf_k + semantic.rank_ix), 0.0) * semantic_weight
  desc
limit least(match_count, 30)
$$;

The full outer join is the important bit: a document that only one search found still makes it into the final list. websearch_to_tsquery accepts the kind of input users actually type (quoted phrases, -exclusions) without throwing a syntax error. Each CTE pulls twice as many candidates as you return, so fusion has some room to reorder.

Calling it from Node #

Embed the query with the same model and dimension count you used for the documents, then pass both the raw text and the vector:

import OpenAI from "openai";
import pg from "pg";

const openai = new OpenAI();
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });

export async function search(query: string, limit = 10) {
  const { data } = await openai.embeddings.create({
    model: "text-embedding-3-small",
    input: query,
    dimensions: 512,
  });

  // pgvector accepts the '[0.1,0.2,...]' text format, which JSON.stringify produces
  const { rows } = await pool.query(
    "select id, content from hybrid_search($1, $2::vector, $3)",
    [query, JSON.stringify(data[0].embedding), limit],
  );
  return rows;
}

Where it gets harder #

Hybrid search is not free. You now pay for two index scans per query, and ts_rank_cd can't use the GIN index for ranking, only for filtering. That is fine for a few hundred matches and slow for a few hundred thousand, so keep that where clause selective.

The weights are tuning knobs, not magic. If your users mostly paste identifiers, raise full_text_weight. If they ask questions in plain language, raise semantic_weight. Don't guess: put 30 real queries with known-good answers in a test file and measure which settings put the right document in the top five.

Also be clear about when you don't need this at all. If your search box is for product codes or log lines, plain full-text search is simpler and better. If your corpus is short, conversational answers, vectors alone may be fine. Hybrid search earns its keep when your users type both kinds of query and you can't predict which.

The takeaway #

When retrieval misses an obvious exact match, the problem usually isn't your chunking or your embedding model. The real issue is asking a meaning-based index to do a string-matching job. Add a generated tsvector column, fuse the two rankings with RRF, and let Postgres handle both. You get the exact-match recall of keyword search and the paraphrase tolerance of embeddings without another service to run.

── more in #ai-search 4 stories · sorted by recency
── more on @postgres 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/hybrid-search-in-pos…] indexed:0 read:5min 2026-11-28 · —