DEV Community

Mohammad Shoeb
Mohammad Shoeb

Posted on

Stop Using Dumb Search Bars: Build Smart, AI-Powered Search with Azure + .NET

Users are typing smart questions, but your search bar is still stuck on keyword matching.
Let’s change that.

🎯 What You'll Build
A .NET Web API that performs semantic + vector hybrid search using Azure Cognitive Search
Integration with Azure OpenAI to implement Retrieval-Augmented Generation (RAG)
Response enrichment that offers intelligent, context-aware answers

🛠️ Prerequisites

  • Azure subscription with Cognitive Search (Standard+) and Azure OpenAI resources
  • .NET 7 or 8 SDK installed
  • IDE: Visual Studio or VS Code

🗂️ Architecture Overview

[User Query] → [.NET API] → [Azure Search (Hybrid)] → [Relevant Docs] → [Azure OpenAI RAG] → [Answer]
Enter fullscreen mode Exit fullscreen mode

⚙️ Step 1: Configure Azure Cognitive Search

Create a Standard or higher-tier Search service in Azure Portal (required for semantic & vector features).

dotnet add package Azure.Search.Documents
Enter fullscreen mode Exit fullscreen mode

Note: Azure SKUs (e.g., Basic won’t work for vector/semantic).

Azure CLI command:

az search service create --name my-search --sku standard --resource-group my-rg
Enter fullscreen mode Exit fullscreen mode

📦 Step 2: Index Your Documents with Embeddings

var client = new SearchClient(new Uri(endpoint), indexName, new AzureKeyCredential(apiKey));
await client.UploadDocumentsAsync(new[]
{
    new
    {
        id = "doc1",
        content = "Azure OpenAI enables powerful GPT models...",
        contentVector = /* byte[] embedding from Azure OpenAI's embedding model */
    }
});
Enter fullscreen mode Exit fullscreen mode

ℹ️ Use Azure OpenAI’s text-embedding-ada-002 or text-embedding-3-small to generate the vector.

🔍 Step 3: Perform Hybrid Semantic + Vector Search

var options = new SearchOptions
{
    Vector = embeddingBytes,
    VectorFields = { "contentVector" },
    QueryType = SearchQueryType.Semantic,
    SemanticConfigurationName = "default",
    Size = 5
};
Response<SearchResults<SearchDocument>> response = await client.SearchAsync("azure ai", options);
Enter fullscreen mode Exit fullscreen mode

💬 Step 4: Add Azure OpenAI for RAG

var openAi = new AzureOpenAIClient(new Uri(openAiEndpoint), new AzureKeyCredential(openAiKey));
var chat = openAi.GetChatCompletionsClient("gpt-35-turbo");

var resultDoc = response.Value.GetResults().First();
string resultContent = resultDoc.Document["content"].ToString();

var completion = await chat.GetChatCompletionsAsync(new ChatCompletionsOptions
{
    Messages = {
        new ChatMessage(ChatRole.System, "You are an AI assistant."),
        new ChatMessage(ChatRole.User, $"Answer based on: {resultContent}\n\nQuery: azure ai")
    },
    Temperature = 0.7f,
    MaxTokens = 256
});
Console.WriteLine(completion.Value.Choices[0].Message.Content);
Enter fullscreen mode Exit fullscreen mode

📈 Real-World Use Cases

  • Microsoft Learn: uses semantic + vector search for documentation lookup
  • Enterprise RAG: internal knowledge base Q&A, compliance, automation
  • Internal copilots: powering smart assistants across domains

🧠 GPT Prompt Engineering Tip

new ChatMessage(ChatRole.User, $"Answer like a search assistant. Use this context: {resultContent}")
Enter fullscreen mode Exit fullscreen mode

✅ Summary & Takeaways
In just 30 minutes, you've gone from basic keyword search to GPT-enhanced hybrid retrieval.

Connected vector embeddings, semantic ranking, and GPT
Production-ready with encryption, scaling, and observability

🔗 References
Azure Cognitive Search – Official Docs
Hybrid Search in Azure
Azure OpenAI Embeddings
Azure OpenAI .NET SDK

⭐ Final CTA
💬 Found this helpful? Share it with your team, leave a clap, and follow for more .NET + Azure AI deep dives.

Top comments (0)