Azure.Search.Documents
11.7.0-beta.2
Prefix Reserved
dotnet add package Azure.Search.Documents --version 11.7.0-beta.2
NuGet\Install-Package Azure.Search.Documents -Version 11.7.0-beta.2
<PackageReference Include="Azure.Search.Documents" Version="11.7.0-beta.2" />
paket add Azure.Search.Documents --version 11.7.0-beta.2
#r "nuget: Azure.Search.Documents, 11.7.0-beta.2"
// Install Azure.Search.Documents as a Cake Addin #addin nuget:?package=Azure.Search.Documents&version=11.7.0-beta.2&prerelease // Install Azure.Search.Documents as a Cake Tool #tool nuget:?package=Azure.Search.Documents&version=11.7.0-beta.2&prerelease
Azure AI Search client library for .NET
Azure AI Search (formerly known as "Azure Cognitive Search") is an AI-powered information retrieval platform that helps developers build rich search experiences and generative AI apps that combine large language models with enterprise data.
The Azure AI Search service is well suited for the following application scenarios:
- Consolidate varied content types into a single searchable index. To populate an index, you can push JSON documents that contain your content, or if your data is already in Azure, create an indexer to pull in data automatically.
- Attach skillsets to an indexer to create searchable content from images and unstructured documents. A skillset leverages APIs from Azure AI Services for built-in OCR, entity recognition, key phrase extraction, language detection, text translation, and sentiment analysis. You can also add custom skills to integrate external processing of your content during data ingestion.
- In a search client application, implement query logic and user experiences similar to commercial web search engines and chat-style apps.
Use the Azure.Search.Documents client library to:
- Submit queries using vector, keyword, and hybrid query forms.
- Implement filtered queries for metadata, geospatial search, faceted navigation, or to narrow results based on filter criteria.
- Create and manage search indexes.
- Upload and update documents in the search index.
- Create and manage indexers that pull data from Azure into an index.
- Create and manage skillsets that add AI enrichment to data ingestion.
- Create and manage analyzers for advanced text analysis or multi-lingual content.
- Optimize results through semantic ranking and scoring profiles to factor in business logic or freshness.
Source code | Package (NuGet) | API reference documentation | REST API documentation | Product documentation | Samples
Getting started
Install the package
Install the Azure AI Search client library for .NET with NuGet:
dotnet add package Azure.Search.Documents
Prerequisites
You need an Azure subscription and a search service to use this package.
To create a new search service, you can use the Azure portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI to create a free instance for getting started:
az search service create --name <mysearch> --resource-group <mysearch-rg> --sku free --location westus
See choosing a pricing tier for more information about available options.
Authenticate the client
To interact with the search service, you'll need to create an instance of the appropriate client class: SearchClient
for searching indexed documents, SearchIndexClient
for managing indexes, or SearchIndexerClient
for crawling data sources and loading search documents into an index. To instantiate a client object, you'll need an endpoint and Azure roles or an API key. You can refer to the documentation for more information on supported authenticating approaches with the search service.
Get an API Key
An API key can be an easier approach to start with because it doesn't require pre-existing role assignments.
You can get the endpoint and an API key from the search service in the Azure portal. Please refer the documentation for instructions on how to get an API key.
Alternatively, you can use the following Azure CLI command to retrieve the API key from the search service:
az search admin-key show --service-name <mysearch> --resource-group <mysearch-rg>
There are two types of keys used to access your search service: admin (read-write) and query (read-only) keys. Restricting access and operations in client apps is essential to safeguarding the search assets on your service. Always use a query key rather than an admin key for any query originating from a client app.
Note: The example Azure CLI snippet above retrieves an admin key so it's easier to get started exploring APIs, but it should be managed carefully.
Create a SearchClient
To instantiate the SearchClient
, you'll need the endpoint, API key and index name:
string indexName = "nycjobs";
// Get the service endpoint and API key from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY");
// Create a client
AzureKeyCredential credential = new AzureKeyCredential(key);
SearchClient client = new SearchClient(endpoint, indexName, credential);
Create a client using Microsoft Entra ID authentication
You can also create a SearchClient
, SearchIndexClient
, or SearchIndexerClient
using Microsoft Entra ID authentication. Your user or service principal must be assigned the "Search Index Data Reader" role.
Using the DefaultAzureCredential you can authenticate a service using Managed Identity or a service principal, authenticate as a developer working on an application, and more all without changing code. Please refer the documentation for instructions on how to connect to Azure AI Search using Azure role-based access control (Azure RBAC).
Before you can use the DefaultAzureCredential
, or any credential type from Azure.Identity, you'll first need to install the Azure.Identity package.
To use DefaultAzureCredential
with a client ID and secret, you'll need to set the AZURE_TENANT_ID
, AZURE_CLIENT_ID
, and AZURE_CLIENT_SECRET
environment variables; alternatively, you can pass those values
to the ClientSecretCredential
also in Azure.Identity.
Make sure you use the right namespace for DefaultAzureCredential
at the top of your source file:
using Azure.Identity;
Then you can create an instance of DefaultAzureCredential
and pass it to a new instance of your client:
string indexName = "nycjobs";
// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
DefaultAzureCredential credential = new DefaultAzureCredential();
// Create a client
SearchClient client = new SearchClient(endpoint, indexName, credential);
ASP.NET Core
To inject SearchClient
as a dependency in an ASP.NET Core app, first install the package Microsoft.Extensions.Azure
. Then register the client in the Startup.ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
services.AddAzureClients(builder =>
{
builder.AddSearchClient(Configuration.GetSection("SearchClient"));
});
services.AddControllers();
}
To use the preceding code, add this to your configuration:
{
"SearchClient": {
"endpoint": "https://<resource-name>.search.windows.net",
"indexname": "nycjobs"
}
}
You'll also need to provide your resource key to authenticate the client, but you shouldn't be putting that information in the configuration. Instead, when in development, use User-Secrets. Add the following to secrets.json
:
{
"SearchClient": {
"credential": { "key": "<you resource key>" }
}
}
When running in production, it's preferable to use environment variables:
SEARCH__CREDENTIAL__KEY="..."
Or use other secure ways of storing secrets like Azure Key Vault.
For more details about Dependency Injection in ASP.NET Core apps, see Dependency injection with the Azure SDK for .NET.
Key concepts
An Azure AI Search service contains one or more indexes that provide persistent storage of searchable data in the form of JSON documents. (If you're brand new to search, you can make a very rough analogy between indexes and database tables.) The Azure.Search.Documents client library exposes operations on these resources through three main client types.
SearchClient
helps with:- Searching your indexed documents using vector queries, keyword queries and hybrid queries
- Vector query filters and Text query filters
- Semantic ranking and scoring profiles for boosting relevance
- Autocompleting partially typed search terms based on documents in the index
- Suggesting the most likely matching text in documents as a user types
- Adding, Updating or Deleting Documents documents from an index
SearchIndexClient
allows you to:SearchIndexerClient
allows you to:
Azure AI Search provides two powerful features:
Semantic ranking
Semantic ranking enhances the quality of search results for text-based queries. By enabling semantic ranking on your search service, you can improve the relevance of search results in two ways:
- It applies secondary ranking to the initial result set, promoting the most semantically relevant results to the top.
- It extracts and returns captions and answers in the response, which can be displayed on a search page to enhance the user's search experience.
To learn more about Semantic Search, you can refer to the sample.
Additionally, for more comprehensive information about Semantic Search, including its concepts and usage, you can refer to the documentation. The documentation provides in-depth explanations and guidance on leveraging the power of Semantic Search in Azure Cognitive Search.
Vector search
Vector search is an information retrieval technique that uses numeric representations of searchable documents and query strings. By searching for numeric representations of content that are most similar to the numeric query, vector search can find relevant matches, even if the exact terms of the query are not present in the index. Moreover, vector search can be applied to various types of content, including images and videos and translated text, not just same-language text.
To learn how to index vector fields and perform vector search, you can refer to the sample. This sample provides detailed guidance on indexing vector fields and demonstrates how to perform vector search.
Additionally, for more comprehensive information about vector search, including its concepts and usage, you can refer to the documentation. The documentation provides in-depth explanations and guidance on leveraging the power of vector search in Azure AI Search.
The Azure.Search.Documents
client library (v11) provides APIs for data plane operations. The
previous Microsoft.Azure.Search
client library (v10) is now retired. It has many similar looking APIs, so please be careful to avoid confusion when
exploring online resources. A good rule of thumb is to check for the namespace
using Azure.Search.Documents;
when you're looking for API reference.
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts
Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime
Examples
The following examples all use a simple Hotel data set that you can import into your own index from the Azure portal. These are just a few of the basics - please check out our Samples for much more.
- Querying
- Creating an index
- Adding documents to your index
- Retrieving a specific document from your index
- Async APIs
Advanced authentication
Querying
Let's start by importing our namespaces.
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Core.GeoJson;
We'll then create a SearchClient
to access our hotels search index.
// Get the service endpoint and API key from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY");
string indexName = "hotels";
// Create a client
AzureKeyCredential credential = new AzureKeyCredential(key);
SearchClient client = new SearchClient(endpoint, indexName, credential);
There are two ways to interact with the data returned from a search query. Let's explore them with a search for a "luxury" hotel.
Use C# types for search results
We can decorate our own C# types with attributes from System.Text.Json
:
public class Hotel
{
[JsonPropertyName("HotelId")]
[SimpleField(IsKey = true, IsFilterable = true, IsSortable = true)]
public string Id { get; set; }
[JsonPropertyName("HotelName")]
[SearchableField(IsFilterable = true, IsSortable = true)]
public string Name { get; set; }
[SimpleField(IsFilterable = true, IsSortable = true)]
public GeoPoint GeoLocation { get; set; }
// Complex fields are included automatically in an index if not ignored.
public HotelAddress Address { get; set; }
}
public class HotelAddress
{
public string StreetAddress { get; set; }
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string City { get; set; }
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string StateProvince { get; set; }
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string Country { get; set; }
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string PostalCode { get; set; }
}
Then we use them as the type parameter when querying to return strongly-typed search results:
SearchResults<Hotel> response = client.Search<Hotel>("luxury");
foreach (SearchResult<Hotel> result in response.GetResults())
{
Hotel doc = result.Document;
Console.WriteLine($"{doc.Id}: {doc.Name}");
}
If you're working with a search index and know the schema, creating C# types is recommended.
Use SearchDocument
like a dictionary for search results
If you don't have your own type for search results, SearchDocument
can be
used instead. Here we perform the search, enumerate over the results, and
extract data using SearchDocument
's dictionary indexer.
SearchResults<SearchDocument> response = client.Search<SearchDocument>("luxury");
foreach (SearchResult<SearchDocument> result in response.GetResults())
{
SearchDocument doc = result.Document;
string id = (string)doc["HotelId"];
string name = (string)doc["HotelName"];
Console.WriteLine($"{id}: {name}");
}
SearchOptions
The SearchOptions
provide powerful control over the behavior of our queries.
Let's search for the top 5 luxury hotels with a good rating.
int stars = 4;
SearchOptions options = new SearchOptions
{
// Filter to only Rating greater than or equal our preference
Filter = SearchFilter.Create($"Rating ge {stars}"),
Size = 5, // Take only 5 results
OrderBy = { "Rating desc" } // Sort by Rating from high to low
};
SearchResults<Hotel> response = client.Search<Hotel>("luxury", options);
// ...
Creating an index
You can use the SearchIndexClient
to create a search index. Fields can be
defined from a model class using FieldBuilder
. Indexes can also define
suggesters, lexical analyzers, and more.
Using the Hotel
sample above,
which defines both simple and complex fields:
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY");
// Create a service client
AzureKeyCredential credential = new AzureKeyCredential(key);
SearchIndexClient client = new SearchIndexClient(endpoint, credential);
// Create the index using FieldBuilder.
SearchIndex index = new SearchIndex("hotels")
{
Fields = new FieldBuilder().Build(typeof(Hotel)),
Suggesters =
{
// Suggest query terms from the HotelName field.
new SearchSuggester("sg", "HotelName")
}
};
client.CreateIndex(index);
In scenarios when the model is not known or cannot be modified, you can
also create fields explicitly using convenient SimpleField
,
SearchableField
, or ComplexField
classes:
// Create the index using field definitions.
SearchIndex index = new SearchIndex("hotels")
{
Fields =
{
new SimpleField("HotelId", SearchFieldDataType.String) { IsKey = true, IsFilterable = true, IsSortable = true },
new SearchableField("HotelName") { IsFilterable = true, IsSortable = true },
new SearchableField("Description") { AnalyzerName = LexicalAnalyzerName.EnLucene },
new SearchableField("Tags", collection: true) { IsFilterable = true, IsFacetable = true },
new ComplexField("Address")
{
Fields =
{
new SearchableField("StreetAddress"),
new SearchableField("City") { IsFilterable = true, IsSortable = true, IsFacetable = true },
new SearchableField("StateProvince") { IsFilterable = true, IsSortable = true, IsFacetable = true },
new SearchableField("Country") { IsFilterable = true, IsSortable = true, IsFacetable = true },
new SearchableField("PostalCode") { IsFilterable = true, IsSortable = true, IsFacetable = true }
}
}
},
Suggesters =
{
// Suggest query terms from the hotelName field.
new SearchSuggester("sg", "HotelName")
}
};
client.CreateIndex(index);
Adding documents to your index
You can Upload
, Merge
, MergeOrUpload
, and Delete
multiple documents from
an index in a single batched request. There are
a few special rules for merging
to be aware of.
IndexDocumentsBatch<Hotel> batch = IndexDocumentsBatch.Create(
IndexDocumentsAction.Upload(new Hotel { Id = "783", Name = "Upload Inn" }),
IndexDocumentsAction.Merge(new Hotel { Id = "12", Name = "Renovated Ranch" }));
IndexDocumentsOptions options = new IndexDocumentsOptions { ThrowOnAnyError = true };
client.IndexDocuments(batch, options);
The request will succeed even if any of the individual actions fail and
return an IndexDocumentsResult
for inspection. There's also a ThrowOnAnyError
option if you only care about success or failure of the whole batch.
Retrieving a specific document from your index
In addition to querying for documents using keywords and optional filters, you can retrieve a specific document from your index if you already know the key. You could get the key from a query, for example, and want to show more information about it or navigate your customer to that document.
Hotel doc = client.GetDocument<Hotel>("1");
Console.WriteLine($"{doc.Id}: {doc.Name}");
Async APIs
All of the examples so far have been using synchronous APIs, but we provide full
support for async APIs as well. You'll generally just add an Async
suffix to
the name of the method and await
it.
SearchResults<Hotel> searchResponse = await client.SearchAsync<Hotel>("luxury");
await foreach (SearchResult<Hotel> result in searchResponse.GetResultsAsync())
{
Hotel doc = result.Document;
Console.WriteLine($"{doc.Id}: {doc.Name}");
}
Authenticate in a National Cloud
To authenticate in a National Cloud, you will need to make the following additions to your client configuration:
- Set the
AuthorityHost
in the credential options or via theAZURE_AUTHORITY_HOST
environment variable - Set the
Audience
inSearchClientOptions
// Create a SearchClient that will authenticate through AAD in the China national cloud
string indexName = "nycjobs";
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
SearchClient client = new SearchClient(endpoint, indexName,
new DefaultAzureCredential(
new DefaultAzureCredentialOptions()
{
AuthorityHost = AzureAuthorityHosts.AzureChina
}),
new SearchClientOptions()
{
Audience = SearchAudience.AzureChina
});
Troubleshooting
Any Azure.Search.Documents operation that fails will throw a
RequestFailedException
with
helpful Status
codes. Many of these errors are recoverable.
try
{
return client.GetDocument<Hotel>("12345");
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
Console.WriteLine("We couldn't find the hotel you are looking for!");
Console.WriteLine("Please try selecting another.");
return null;
}
You can also easily enable console logging if you want to dig deeper into the requests you're making against the service.
See our troubleshooting guide for details on how to diagnose various failure scenarios.
Next steps
- Go further with Azure.Search.Documents and our samples
- Read more about the Azure AI Search service
Contributing
See our Search CONTRIBUTING.md for details on building, testing, and contributing to this library.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.44.1)
- System.Text.Json (>= 6.0.10)
- System.Threading.Channels (>= 6.0.0)
-
net8.0
- Azure.Core (>= 1.44.1)
NuGet packages (52)
Showing the top 5 NuGet packages that depend on Azure.Search.Documents:
Package | Downloads |
---|---|
Hilma.Common
Shared entities and contracts for Hilma Domain |
|
Indigina.Data
This NuGet package contains Data Access Layer for Indigina projects. |
|
OrchardCore.Application.Cms.Core.Targets
Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. Converts the application into a modular OrchardCore CMS application with TheAdmin theme but without any front-end Themes. |
|
Microsoft.KernelMemory.MemoryDb.AzureAISearch
Azure AI Search connector for Microsoft Kernel Memory, to store and search memory using Azure AI Search vector indexing and semantic features. |
|
OrchardCore.Application.Cms.Targets
Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. Converts the application into a modular OrchardCore CMS application with following themes. - TheAdmin Theme - SafeMode Theme - TheAgency Theme - TheBlog Theme - TheComingSoon Theme - TheTheme theme |
GitHub repositories (18)
Showing the top 5 popular GitHub repositories that depend on Azure.Search.Documents:
Repository | Stars |
---|---|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
OrchardCMS/OrchardCore
Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
|
|
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
|
|
Xabaril/AspNetCore.Diagnostics.HealthChecks
Enterprise HealthChecks for ASP.NET Core Diagnostics Package
|
|
dotnet/aspire
Tools, templates, and packages to accelerate building observable, production-ready apps
|
Version | Downloads | Last updated |
---|---|---|
11.7.0-beta.2 | 4,160 | 11/26/2024 |
11.7.0-beta.1 | 22,279 | 9/24/2024 |
11.6.0 | 1,029,563 | 7/17/2024 |
11.6.0-beta.4 | 231,508 | 5/6/2024 |
11.6.0-beta.3 | 92,761 | 3/6/2024 |
11.6.0-beta.2 | 47,638 | 2/5/2024 |
11.6.0-beta.1 | 96,965 | 1/17/2024 |
11.5.1 | 2,793,859 | 11/29/2023 |
11.5.0 | 199,170 | 11/10/2023 |
11.5.0-beta.5 | 214,114 | 10/9/2023 |
11.5.0-beta.4 | 330,630 | 8/7/2023 |
11.5.0-beta.3 | 164,971 | 7/11/2023 |
11.5.0-beta.2 | 914,016 | 10/11/2022 |
11.5.0-beta.1 | 40,951 | 9/7/2022 |
11.4.0 | 4,547,720 | 9/6/2022 |
11.4.0-beta.9 | 15,051 | 8/8/2022 |
11.4.0-beta.8 | 3,611 | 7/7/2022 |
11.4.0-beta.7 | 113,241 | 3/8/2022 |
11.4.0-beta.6 | 11,721 | 2/8/2022 |
11.4.0-beta.5 | 83,168 | 11/19/2021 |
11.4.0-beta.4 | 12,333 | 10/7/2021 |
11.4.0-beta.3 | 13,561 | 9/8/2021 |
11.4.0-beta.2 | 22,586 | 8/10/2021 |
11.4.0-beta.1 | 4,126 | 7/9/2021 |
11.3.0 | 4,458,452 | 6/10/2021 |
11.3.0-beta.2 | 2,438 | 5/11/2021 |
11.3.0-beta.1 | 10,488 | 4/6/2021 |
11.2.1 | 216,279 | 5/10/2021 |
11.2.0 | 3,399,150 | 2/10/2021 |
11.2.0-beta.2 | 54,400 | 11/11/2020 |
11.2.0-beta.1 | 5,673 | 10/9/2020 |
11.1.1 | 1,023,147 | 8/18/2020 |
11.0.0 | 48,652 | 7/7/2020 |
1.0.0-preview.4 | 630 | 6/9/2020 |
1.0.0-preview.3 | 355 | 5/5/2020 |
1.0.0-preview.2 | 703 | 4/6/2020 |