AI - Azure Cognitive Search


High-level workflow:

  • Create a Cognitive Search service in Azure.

  • Index your database (Orders table with Comments). You can connect directly to SQL DB or push documents programmatically.

  • Query the index from your C# app using the Azure.Search.Documents SDK.

  • Present results (matching orders or summaries).

 

This example assumes you have:

  • A Cognitive Search service

  • An index called orders

  • A field comments inside that index

 

using System;
using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Models;class Program
{
    static void Main(string[] args)
    {
        string serviceEndpoint = "https://<your-search-service>.search.windows.net";
        string indexName = "orders";
        string apiKey = "<your-admin-or-query-key>";       

             var client = new SearchClient(
            new Uri(serviceEndpoint),
            indexName,
            new AzureKeyCredential(apiKey));       

             Console.WriteLine("Enter your question:");
        string userInput = Console.ReadLine();       

             // Step 1: Run a semantic search query
        var options = new SearchOptions()
        {
            QueryType = SearchQueryType.Semantic,
            Size = 10, // limit results
            QueryLanguage = "en-us"
        };       

             // Optional: select specific fields
        options.Select.Add("orderId");
        options.Select.Add("comments");       

             // Step 2: Execute the search
        SearchResults<SearchDocument> response = client.Search<SearchDocument>(userInput, options);                   

             // Step 3: Display results
        Console.WriteLine("Results:");
        foreach (SearchResult<SearchDocument> result in response.GetResults())
        {
            Console.WriteLine($"OrderId: {result.Document["orderId"]}");
            Console.WriteLine($"Comments: {result.Document["comments"]}");
            Console.WriteLine($"Score: {result.Score}\n");
        }
    }
}

 

Example of queries a user could have:

  • “Show me all orders where delivery is delayed due to an invalid address”

  • “List comments mentioning weather problems”

  • “How many orders had issues with customer service?”

 

Cognitive Search retrieves the matching documents, and you can do post-processing (like counting, grouping) in your app.

 

Key Difference vs. OpenAI + SQL

  • OpenAI + SQL: More flexible, can aggregate and summarize directly via SQL translation.

  • Cognitive Search: Strong at semantic/document retrieval, but summarization/aggregation may require another step (e.g., call Azure OpenAI to summarize the retrieved results).

 

We could use a combination of these two approaches! This is also called RAG - Retrieval Augmented Generation.

 

Comparison

Approach Strengths When to Use
OpenAI → SQL Flexible, works directly with structured DB queries (counts, filters, aggregations). If your data is mostly structured (tables, numbers) and you want query flexibility.
Cognitive Search Great for text-heavy fields, semantic retrieval, fuzzy queries. If you mostly need to search/filter text columns (like Comments).
Hybrid Best of both worlds (search + summarization). If you want both semantic retrieval and reasoning.

No files yet, migration hasn't completed yet!