
For decades, relational database search has relied on keyword matching. Whether using SQL's simple LIKE operator or complex Full-Text Indexing, search engines only look for exact character sequences. If a user searches for "automobile," a keyword-based database will completely miss records containing "car" or "vehicle," despite them meaning the exact same thing. This lack of conceptual understanding leads to frustrated users and poor data discoverability.
To solve this, modern web applications are moving toward Semantic Search. By converting text into mathematical coordinate arrays called vector embeddings, systems can search by conceptual meaning rather than exact characters. With .NET 9, Microsoft has introduced native AI capabilities via Microsoft.Extensions.AI, making it easier than ever to implement hybrid search—combining traditional keyword matching with vector similarity search for the ultimate query accuracy.
What is Semantic Hybrid Search?
Hybrid search is the gold standard for modern search engines. It executes two queries simultaneously: Keyword Search (best for exact product codes, SKU numbers, or names) and Vector Search (best for synonyms, natural language queries, and intent). It then merges both result sets using the Reciprocal Rank Fusion (RRF) algorithm to deliver the most relevant records first.
Why Combine Keywords and Vectors?
Pure vector search is powerful, but it has weaknesses. For example, if a user searches for a specific model code like "XT-900," a vector search might return other high-tech equipment, but miss the exact "XT-900" because vectors focus on conceptual meaning rather than exact alphanumeric sequences. Hybrid search combines the strengths of both worlds:
| Search Type | How It Works | Best For |
|---|---|---|
| 1. Keyword Search | Matches exact character tokens (using SQL FTS or Elasticsearch). | Serial numbers, exact names, acronyms, and product codes. |
| 2. Vector Search | Computes cosine similarity between query embeddings and stored records. | Synonyms, descriptive queries, contextual meanings, and multi-lingual search. |
| 3. Hybrid Search (RRF) | Combines and reranks scores from both keyword and vector outputs. | Providing 100% accurate search relevance for all user queries. |
Implementing Hybrid Search in .NET 9
Building a semantic search pipeline involves generating embeddings when records are saved, and querying using a vector database extension like PGVector in PostgreSQL. Below is a C# implementation demonstrating how to generate vector embeddings using Azure OpenAI and query a PostgreSQL vector store natively in .NET 9:
using System.Numerics.Tensors; using Microsoft.Extensions.AI; using Npgsql; public class ProductRecord { public int Id { get; set; } public string Name { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; // Store the 1536-dimensional math vector representing the description public float[]? VectorEmbedding { get; set; } } public class SemanticSearchService { private readonly IEmbeddingGenerator<string, Embedding<float>> _generator; private readonly string _connectionString; public SemanticSearchService(IEmbeddingGenerator<string, Embedding<float>> generator, string connectionString) { _generator = generator; _connectionString = connectionString; } // 1. Generate and Save Vector Embeddings public async Task IndexProductAsync(ProductRecord product) { // Generate embedding vector using Azure OpenAI var embedding = await _generator.GenerateEmbeddingValueAsync(product.Description); product.VectorEmbedding = embedding.ToArray(); using var conn = new NpgsqlConnection(_connectionString); await conn.OpenAsync(); using var cmd = new NpgsqlCommand( "INSERT INTO products (name, description, embedding) VALUES (@name, @desc, @vector)", conn); cmd.Parameters.AddWithValue("name", product.Name); cmd.Parameters.AddWithValue("desc", product.Description); cmd.Parameters.AddWithValue("vector", product.VectorEmbedding); await cmd.ExecuteNonQueryAsync(); } // 2. Query Database Using Vector Cosine Similarity public async Task<List<ProductRecord>> SearchSemanticAsync(string searchQuery, int limit = 5) { // Convert user's search query into an embedding vector var queryEmbedding = await _generator.GenerateEmbeddingValueAsync(searchQuery); var queryVector = queryEmbedding.ToArray(); var results = new List<ProductRecord>(); using var conn = new NpgsqlConnection(_connectionString); await conn.OpenAsync(); // Perform cosine similarity search (using PGVector <=> operator) using var cmd = new NpgsqlCommand( "SELECT id, name, description FROM products ORDER BY embedding <=> @queryVector LIMIT @limit", conn); cmd.Parameters.AddWithValue("queryVector", queryVector); cmd.Parameters.AddWithValue("limit", limit); using var reader = await cmd.ExecuteReaderAsync(); while (await reader.ReadAsync()) { results.Add(new ProductRecord { Id = reader.GetInt32(0), Name = reader.GetString(1), Description = reader.GetString(2) }); } return results; } } Key Steps to Build a Production-Grade Search
To ship this to production, follow these architectural best practices:
- Chunking Large Text: If indexing large documents, split them into paragraphs (chunks) of 200–500 words before generating vectors, so the coordinates focus on specific topics.
- Indexing Vectors: Create HNSW (Hierarchical Navigable Small World) indexes in PostgreSQL or SQL Server. This reduces search time from linear scanning to logarithmic time, supporting sub-millisecond lookups across millions of rows.
- Asynchronous Processing: Generate embeddings in background queues using channels or background workers, preventing database operations from blocking HTTP API response times.
Architect's Tip: Use .NET 9 Microsoft.Extensions.AI
In .NET 9, Microsoft introduced the unified Microsoft.Extensions.AI abstractions. This allows you to swap your embedding models (e.g. from Azure OpenAI to local Ollama/Llama-3 models) with a single line of code change in your Program.cs, completely future-proofing your AI stack.
At Krista Technology, we design modern, intelligent search systems, recommendation engines, and high-performance database architectures. If you want to integrate semantic searching into your product portals or SaaS applications, contact our dedicated .NET AI engineers to craft your custom solutions.
