.NET (C#) code samples for Azure Cognitive Search

Learn about the C# code samples that demonstrate the functionality and workflow of an Azure Cognitive Search solution. These samples use the Azure Cognitive Search client library for the Azure SDK for .NET, which you can explore through the following links.

Target Link
Package download www.nuget.org/packages/Azure.Search.Documents/
API reference azure.search.documents
API test cases github.com/Azure/azure-sdk-for-net/tree/master/sdk/search/Azure.Search.Documents/tests
Source code github.com/Azure/azure-sdk-for-net/tree/master/sdk/search/Azure.Search.Documents/src

SDK samples

Code samples from the Azure SDK development team demonstrate API usage. You can find these samples in Azure/azure-sdk-for-net/tree/master/sdk/search/Azure.Search.Documents/samples on GitHub.

Samples Description
"Hello world", synchronously Demonstrates how to create a client, authenticate, and handle errors using synchronous methods.
"Hello world", asynchronously Demonstrates how to create a client, authenticate, and handle errors using asynchronous methods.
Service-level operations Demonstrates how to create indexes, indexers, data sources, skillsets, and synonym maps. This sample also shows you how to get service statistics and how to query an index.
Index operations Demonstrates how to perform an action on existing index, in this case getting a count of documents stored in the index.
FieldBuilderIgnore Demonstrates a technique for working with unsupported data types.
Indexing documents (push model) "Push" model indexing, where you send a JSON payload to an index on a service.
Encryption key sample Demonstrates using a customer-managed encryption key to add an extra layer of protection over sensitive content.

Doc samples

Code samples from the Cognitive Search team demonstrate features and workflows. Many of these samples are referenced in tutorials, quickstarts, and how-to articles. You can find these samples in Azure-Samples/azure-search-dotnet-samples and in Azure-Samples/search-dotnet-getting-started on GitHub.

Samples Article
quickstart Source code for Quickstart: Create a search index . Covers the basic workflow for creating, loading, and querying a search index using sample data.
search-website Source code for Tutorial: Add search to web apps. Demonstrates an end-to-end search app that includes a rich client plus components for hosting the app and handling search requests.
DotNetHowTo Source code for How to use the .NET client library. Steps through the basic workflow, but in more detail and discussion of API usage.
DotNetHowToSynonyms Source code for Example: Add synonyms in C#. Synonym lists are used for query expansion, providing matchable terms that are external to an index.
DotNetToIndexers Source code for Tutorial: Index Azure SQL data using the .NET SDK. This article shows how to configure an Azure SQL indexer that has a schedule, field mappings, and parameters.
DotNetHowToEncryptionUsingCMK Source code for How to configure customer-managed keys for data encryption.
Create your first app in C# Source code for Tutorial: Create your first search app. While most samples are console applications, this MVC sample uses a web page to front the sample Hotels index, demonstrating basic search, pagination, autocomplete and suggested queries, facets, and filters.
multiple-data-sources Source code for Tutorial: Index from multiple data sources.
optimize-data-indexing Source code for Tutorial: Optimize indexing with the push API.
tutorial-ai-enrichment Source code for Tutorial: AI-generated searchable content from Azure blobs using the .NET SDK.

Tip

Try the Samples browser to search for Microsoft code samples in GitHub, filtered by product, service, and language.

Other samples

The following samples are also published by the Cognitive Search team, but are not referenced in documentation. Associated readme files provide usage instructions.

Samples Description
Index Data Lake Gen2 using Azure AD Source code demonstrating indexer connections and indexing of Azure Data Lake Gen2 files and folders that are secured through Azure AD and role-based access controls.
azure-search-power-skills Source code for consumable custom skills that you can incorporate in your won solutions.
Knowledge Mining Solution Accelerator Includes templates, support files, and analytical reports to help you prototype an end-to-end knowledge mining solution.
Covid-19 Search App repository Source code repository for the Cognitive Search based Covid-19 Search App
JFK Learn more about the JFK solution.
Search + QnA Maker Accelerator A solution combining the power of Search and QnA Maker. See the live demo site.