← All Articles

How to add AI semantic search to an existing website with pgvector

You can add AI semantic search to your existing website by generating embeddings for your content, storing them in a pgvector column, and querying them with cosine similarity. This guide walks you through model selection, vector creation, storage, and UI wiring without rebuilding your site.

Illustration of a website search box linked to a database with vector arrows

You can add AI semantic search to an existing website by generating an embedding for each searchable page, storing those vectors in a PostgreSQL table with the pgvector extension, and adding a search endpoint that ranks pages by cosine distance to the embedded query. It needs a new table, an indexing job, and one API route, so your existing pages and front end stay as they are.

Key takeaways

  • Pick one embedding model and use it for both your content and your queries; OpenAI's text-embedding-3-small (1536 dimensions) is a common starting point, and open-source sentence-transformers models work if you want to self-host.
  • Store vectors in a vector column provided by the pgvector extension, so similarity search runs directly in SQL next to your existing data.
  • Split long pages into sections before embedding; smaller chunks return more precise matches than one vector per page.
  • Query with ORDER BY embedding <=> query_embedding LIMIT k, where <=> is pgvector's cosine distance operator, and add an HNSW index once the table grows.
  • Re-embed pages when they change and review real search logs to keep results relevant.

Which embedding model should I choose?

Direct answer: Choose a model that covers your site's languages, fits your latency and privacy needs, and is available to your backend; a hosted model such as OpenAI's text-embedding-3-small is the quickest way to start.

  1. List your requirements: languages, expected query volume, latency, and whether content may leave your infrastructure.
  2. Compare hosted embedding APIs (for example OpenAI, Cohere, or Voyage AI) with open-source models from the sentence-transformers family that you run yourself.
  3. Embed a small sample of pages with each candidate and run 20-30 real queries from your site search logs.
  4. Keep the model whose top results a person on your team judges most relevant, then fix the vector dimension in your schema to match it.

How do I generate vectors for my site content?

Direct answer: Export your pages, split them into sections, and send each section's text to the embedding model to get one vector per section.

  1. Export or crawl your content into records with URL, title, section heading, and text.
  2. Split long pages by heading or into passages of a few hundred words, keeping the page URL on every chunk.
  3. Write a script (Python, Node, or an automation tool) that calls the embedding endpoint in batches.
  4. Check that every returned vector has the dimension your model documents (1536 for text-embedding-3-small).
  5. Store a last_updated timestamp with each chunk so later runs only re-embed what changed.

How do I store vectors in pgvector?

Direct answer: Enable the pgvector extension, create a table with a vector column sized to your model, and upsert each chunk with its URL.

  1. Enable the extension on your database (Supabase or self-hosted PostgreSQL), as described in the Supabase pgvector docs:
create extension if not exists vector;
  1. Create a table for search data:
create table site_search (
  id bigserial primary key,
  url text not null,
  title text,
  content text not null,
  last_updated timestamptz not null default now(),
  embedding vector(1536) -- must match your model's dimension
);
  1. Insert or upsert the chunks with your ORM or a bulk insert.
  2. Add an index for faster similarity queries once you have more than a few thousand rows. HNSW is the usual choice because it needs no training step and keeps good recall as data changes:
create index on site_search using hnsw (embedding vector_cosine_ops);

The operator class (vector_cosine_ops) must match the distance operator you query with.

How can I wire a semantic search UI to pgvector?

Direct answer: Add an API endpoint that embeds the user's query with the same model, runs a nearest-neighbor SQL query, and returns the matching pages for your front end to display.

  1. Front end: add a search box that sends the query to an endpoint such as /api/semantic-search.
  2. Back end:
    • Embed the query string with the model you used for indexing.
    • Run the query, passing the embedding as a parameter:
select url, title, content
from site_search
order by embedding <=> $1
limit 10;
  • On Supabase, wrap this query in a Postgres function and call it with rpc() from your server code, as shown in Supabase's embeddings guide.
  1. Display: group results by URL so a page appears once, and show the matching section as the snippet.
  2. Performance: cache embeddings for repeated queries so popular searches do not call the embedding API every time.

How do I monitor and improve retrieval quality?

Direct answer: Log what people search and click, review weak queries regularly, and re-embed content when it changes or when you switch models.

SignalHow to capture itWhat to try if it is weak
Click-through on resultsLog which result a user opens after a searchReview top results for those queries; adjust chunk size or the text you embed
Searches with no clickLog queries where nobody opens a resultAdd hybrid search (combine keyword full-text search with vector similarity)
Explicit feedbackAsk "Did you find what you were looking for?"Add a reranking step on the top results

Run a scheduled job that re-embeds pages edited since the last run, so the vector table stays in sync with your content.

How Datamatic approaches this

We deliver semantic search as part of our AI integrations and RAG work, in four steps:

  1. Discovery and model fit: we test candidate embedding models on a sample of your real content and real queries before committing to one.
  2. Vector pipeline: we set up pgvector on your existing Postgres or Supabase database, build the chunking and embedding job, and add the right index.
  3. Search API and UI: we add a search endpoint and a search component to your current site, without rebuilding it.
  4. Monitoring: we log searches and clicks so relevance can be reviewed and improved after launch.

We have built AI features into client products before, such as AI-powered candidate matching for the recruitment platform Stigue and the AI lunch assistant in Meal Outpost. The same vector store can later power retrieval-augmented generation (RAG), where an LLM answers questions using the retrieved pages.

Frequently asked questions

Do I need to rebuild my website to use pgvector?

No. pgvector is a PostgreSQL extension, so you add a table and an index to a database you already run or to a new Supabase project. Your site only needs a search box that calls the new endpoint.

Can I use a local embedding model instead of a cloud API?

Yes. Open-source models such as all-mpnet-base-v2 from sentence-transformers run on your own server, which keeps content in your infrastructure. Set the vector column to that model's dimension (768 for all-mpnet-base-v2) and use the same model for queries.

How large can my vector store be before performance degrades?

With an HNSW index, pgvector handles large tables well; speed depends mostly on vector dimension, index settings, and the memory available to your database. Test with your real data volume and monitor query times as content grows.

What if my content changes frequently?

Trigger re-embedding from your CMS webhooks or a scheduled job that picks up rows with a newer last_updated value. Upsert the new vectors so the table always reflects the current pages without a full re-index.

Is cosine distance the only option in pgvector?

No. pgvector supports L2 distance (<->), negative inner product (<#>), and cosine distance (<=>), among others. Cosine distance is the usual choice for text embeddings; if your model returns normalized vectors, inner product gives the same ranking and can be slightly faster.

If you want AI semantic search on your current site, talk to our team or explore our services.

Sources

Building something similar?

We engineer mission-critical web applications, AI integrations, and cloud platforms for ambitious teams.