Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 1.59 KB

Sample01a_HelloWorld.md

File metadata and controls

50 lines (39 loc) · 1.59 KB

Azure.Search.Documents Samples - Hello World (sync)

Import the namespaces

using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.Models;

Create a client

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.");

Handle Errors

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.");
}