Postgres, pgvector and Ollama: .NET vector search
Build semantic search in .NET with Postgres 17, pgvector and Ollama, using Qwen 3 Embedding 0.6B to generate 1,024-dimensional vectors.
Player not loading? Watch on YouTube
This tutorial builds a .NET API that searches blog articles by meaning. Postgres stores the article URLs, titles and embeddings, while Ollama runs Qwen 3 Embedding 0.6B locally. The demo uses vectors with 1,024 dimensions and a sitemap containing roughly 180 articles.
.NET Aspire orchestrates Ollama and a Postgres 17 container using the pgvector/pgvector image. The setup includes persistent container lifetimes and data volumes; the speaker recommends enabling GPU support if suitable hardware is available. The API connects through Npgsql and configures vector mapping with UseVector and a Dapper type handler.
The database initialization enables the pgvector extension, reloads the connection's type cache and creates a vector column whose dimensions match the embedding model. An HNSW index supports approximate nearest-neighbor search. Article ingestion fetches each page's content, generates its embedding and inserts the result into Postgres. The speaker notes that this work usually belongs in a background process rather than a typical API endpoint.
The search endpoint embeds the query, orders stored vectors by cosine distance and limits the results. Query and article embeddings must come from identical or compatible models. Example requests return articles about distributed messaging, CQRS and software architecture. The speaker reports roughly 100 milliseconds for the demonstrated CQRS request; this is a demo result, not a general performance guarantee.