RSS Amplifier

The .NET Saturday Newsletter · Jul 4, 2026

Build AI Search in .NET using Postgres and pgvector

0
Sign in to vote or save

Muhammad Waseem · The .NET Saturday Newsletter

Sponsor this newsletter

“Search is easy... until users stop searching with the exact words you stored.”

Imagine you have thousands of blog posts.

A user searches:

“How do I secure my APIs?”

But your blog title is:

“JWT Authentication in ASP.NET Core”

A normal database search returns nothing.

Even though that article is exactly what the user wants.

This is the problem with keyword search and it is why AI-powered search exists.

In this article, we will build an AI-powered search using:

  • ASP.NET Core

  • PostgreSQL

  • pgvector

  • Gemini Embeddings API

Let’s break it down step by step.

Here is what normal search looks like in EF Core:

This works fine when users search with the exact words stored in your database.

But when they search by meaning, “secure my APIs” instead of “JWT”, it fails silently.

You get zero results. Not because the data does not exist, but because the search does not understand meaning.

That is what vector search solves.

Vector Search is a way of searching by meaning instead of exact words.

Instead of asking:

“Does this sentence contain JWT?”

It asks:

“Does this sentence mean something similar to what the user asked?”

Embeddings are numbers that represent the meaning of text.

A sentence like:

“JWT Authentication in ASP.NET Core”

becomes something like:

[0.123, -0.873, ...]

Text with similar meanings produces similar vectors. That is how meaning-based search works.

Almost every AI provider offers an embedding model. Gemini, OpenAI, local LLMs.

With the Gemini free API, you send your text using the models e.g. gemini-embedding-001 :

POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent";

And you will receive a response similar to this :

Store this vector in your database alongside the blog post.

Add an Embedding property to your Blog entity:

Enable the pgvector extension:

Add the vector column:

Install the EF Core package:

dotnet add package Pgvector.EntityFrameworkCore

Configure it in your DbContext:

PostgreSQL now understands vectors.

Whenever a new blog is created, generate and save its embedding:

Blog → Gemini Embedding API → Embedding Vector → Save Blog + Vector

When a user searches “Secure my APIs”:

Generate an embedding for the search query:

Then ask PostgreSQL to return the nearest vectors:

Cosine distance measures how similar two vectors are in meaning.

The closest ones come back first, even if no keywords match.

I put together a complete running Web API with 100 seeded articles.

Clone the repo, add your Gemini API key and PostgreSQL connection string and you are ready to go.

👉 GitHub Issue #76

Vector search gets you the right documents.

But what if you want a direct answer instead of a list of results?

That is where RAG comes in. You take these search results and pass them to an LLM to generate a precise, grounded answer. We will cover that in the future articles.

  1. Enhance your .NET skills by subscribing to my YouTube Channel

  2. Promote yourself to 10,000+ subscribers by sponsoring this newsletter

Read the original on mwaseemzakir.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.