using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.Models;
Create a SearchServiceClient
and send a request.
// Get the service endpoint and API key from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
AzureKeyCredential credential = new AzureKeyCredential(
Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
// Create a new SearchIndexClient
SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential);
// Perform an operation
Response<SearchServiceStatistics> stats = indexClient.GetServiceStatistics();
Console.WriteLine($"You are using {stats.Value.Counters.IndexCounter.Usage} indexes.");
All Search operations will throw a RequestFailedException on failure.
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
AzureKeyCredential credential = new AzureKeyCredential(
Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
// Create an invalid SearchClient
string fakeIndexName = "doesnotexist";
SearchClient searchClient = new SearchClient(endpoint, fakeIndexName, credential);
try
{
searchClient.GetDocumentCount();
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
Console.WriteLine("Index wasn't found.");
}