Semantic Search in .NET 8 with Azure AI Search & OpenAI Embeddings

Mahmut Sarıkaya 4 min read 2 Views 0
Semantic Search in .NET 8 with Azure AI Search & OpenAI Embeddings

Why Semantic Search Matters for Modern .NET 8 Apps

Imagine a user typing a vague phrase like "how to fix a null reference" and instantly receiving the most relevant code snippets, documentation pages, and forum posts. Traditional keyword search would struggle because the exact terms may not match, but semantic search bridges that gap by comparing meanings. In 2023, Microsoft reported that vector‑based search reduced average query time by 40% while improving relevance scores by up to 30% for enterprise workloads. For .NET 8 developers building knowledge bases, e‑commerce catalogs, or legal repositories, semantic search is no longer a luxury—it’s a competitive necessity.

Understanding Azure AI Search Vector Capabilities

Azure AI Search introduced native vector search in 2022 and expanded it in the 2024 release with configurable algorithms such as HNSW and IVF‑PQ. An index can store a dense vector field alongside traditional searchable text. When you query with a vector, the service computes approximate nearest‑neighbors (ANN) in sub‑millisecond latency, even for collections exceeding ten million records. The service also supports hybrid queries, letting you filter by metadata while scoring by semantic similarity.

Preparing OpenAI Embeddings in a .NET 8 Service

OpenAI’s ada‑002 model produces 1536‑dimensional embeddings that work out of the box with Azure AI Search. The following async helper creates an embedding from any string. Remember to replace and with your Azure OpenAI resource values.

using Azure.AI.OpenAI; using Azure; using System.Threading.Tasks; public async Task GetEmbeddingAsync(string text) { var client = new OpenAIClient(new Uri("https://.openai.azure.com/"), new AzureKeyCredential("")); var response = await client.GetEmbeddingsAsync("text-embedding-ada-002", new EmbeddingsOptions { Input = new[] { text } }); return response.Value.Data[0].Embedding.ToArray(); }

Cache the resulting vectors in memory or a distributed store if you anticipate high query volume; the API call costs roughly $0.0001 per 1,000 tokens, which is negligible compared to Azure Search RU consumption.

Integrating Embeddings with Azure AI Search Index

First, define an index that includes a contentVector field of type Collection(Single) with the same dimension as the OpenAI model. The index definition below uses the Azure.Search.Documents SDK for .NET 8.

using Azure.Search.Documents.Indexes; using Azure.Search.Documents.Indexes.Models; var index = new SearchIndex("documents") { Fields = new SearchField[] { new SimpleField("id", SearchFieldDataType.String) { IsKey = true }, new SearchableField("content") { AnalyzerName = LexicalAnalyzerName.EnLucene }, new VectorSearchField("contentVector", SearchFieldDataType.Collection(SearchFieldDataType.Single)) { Dimensions = 1536, VectorSearchAlgorithmConfiguration = "my-config" } } }; var adminClient = new SearchIndexClient(new Uri("https://.search.windows.net"), new AzureKeyCredential("")); await adminClient.CreateOrUpdateIndexAsync(index);

After the index is live, upload documents together with their embeddings. The SDK automatically serializes the float array into the required JSON format.

using Azure.Search.Documents; using Azure.Search.Documents.Models; var searchClient = new SearchClient(new Uri("https://.search.windows.net"), "documents", new AzureKeyCredential("")); var text = "How to handle NullReferenceException in C#"; var vector = await GetEmbeddingAsync(text); var batch = IndexDocumentsBatch.Upload(new[] { new { id = "doc1", content = text, contentVector = vector } }); await searchClient.IndexDocumentsAsync(batch);

Executing a Vector Search Query from C#

When a user submits a query, convert it to an embedding, then issue a hybrid search that filters by a tenant ID (if multi‑tenant) and ranks by vector similarity. The SearchOptions object lets you specify the Vector property.

public async Task>> SemanticSearchAsync(string userQuery) { var queryVector = await GetEmbeddingAsync(userQuery); var options = new SearchOptions { Size = 5, Vector = new SearchVector { Value = queryVector, K = 5, Fields = new[] { "contentVector" } } }; var response = await searchClient.SearchAsync(null, options); return response.Value.GetResults(); }

The method returns the top five most semantically related documents. You can combine options.Filter = "tenantId eq '123'" to keep results scoped to a specific customer.

Performance Tips and Cost Management

Even though Azure AI Search handles ANN efficiently, a few practical steps keep latency low and costs predictable:

  • Store vectors in a dedicated field; avoid mixing them with large text fields to reduce payload size.
  • Set Dimensions correctly—mismatched dimensions trigger runtime errors.
  • Use the my-config algorithm configuration with a MaximumVectorCount that matches your expected result set (e.g., 1000 for large catalogs).
  • Monitor RU consumption via Azure Monitor; a typical 10 k‑document index consumes ~150 RU per query when using HNSW.

For development, enable the “semantic search preview” flag only in non‑production environments to avoid accidental over‑provisioning.

Conclusion

By pairing OpenAI’s high‑quality embeddings with Azure AI Search’s native vector engine, .NET 8 developers can deliver lightning‑fast, meaning‑aware search experiences without managing custom ML pipelines. The workflow—generate embeddings, store them in a vector field, and query with the same model—fits naturally into the existing Azure SDK ecosystem, allowing you to focus on domain logic rather than infrastructure. Adopt the steps outlined above, monitor performance, and your application will stay ahead of the semantic search curve.

Sources

  • Microsoft Azure AI Search documentation
  • OpenAI API reference
  • .NET 8 official release notes

Author: Mahmut Sarıkaya — sarikayadev.com

Tags: #Azure AI Search #.NET 8 #semantic search #OpenAI embeddings #vector search
Share:
M

Written by

Mahmut Sarıkaya

Software Developer

Comments

No comments yet. Be the first to share your thoughts!

Leave a Comment

7 + 5 =