Search code examples
.netazurerestazure-search-.net-sdk

Azure Cognititve search API connection timeout


Created a sample azure search service and imported sample data for the same, able to get data using azure cognitive search API from Postman but getting following exception

' SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond '

internal async Task<string> Getresult()
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("api-key", adminApiKey);
    var result = await client.GetAsync(searchServiceName);//line where exception occurs
    string resultContent = result.Content.ReadAsStringAsync().Result;
    return resultContent;
}

Modified code

internal async Task<Hotel> Getresult(string search)
{
    SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, "hotels-sample-index", new SearchCredentials(adminApiKey));
    var results = indexClient.Documents.Search<Hotel>("*");
    return results;
}

gets error

'cannot implicitly convert type 'Microsoft.Azure.Search.Models.DocumentSearchResult' to 'Models.Hotel' '

enter image description here


Solution

  • You should use the Azure Cognitive Search API. It's much easier:

    1-Install Package Microsoft.Azure.Search

    SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(queryApiKey));
    
    ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);
    
    var results = indexClient.Documents.Search<Hotel>("motel", parameters);
    

    https://learn.microsoft.com/en-us/azure/search/search-howto-dotnet-sdk