Why does Azure Search return null for the calling method?

19 hours ago 1
ARTICLE AD BOX

I have a web application implementing Azure Search. It is also using interception to build filters for the search prior to execution.

The path of execution is:

controller calls search provider

interceptor builds filter and then invokes search provider

search provider constructs azure search client

search provider calls search

search provider returns search results to controller

another interceptor should run and do some work on the results

controller processes search results.

In the search provider we have these data members to store information for the search client:

private static readonly string AzSearchKey = WebConfigurationManager.AppSettings["Providers.AzSearchKey"]; private static readonly string AzSearchServiceName = WebConfigurationManager.AppSettings["Providers.AzSearchServiceName"]; private static readonly string AzSearchIndexName = WebConfigurationManager.AppSettings["Providers.AzSearchIndexName"]; private static readonly string AzSearchSemanticConfig = WebConfigurationManager.AppSettings["Providers.AzSearchSemanticConfig"]; private static readonly Logger Log = LogManager.GetCurrentClassLogger(); private readonly SearchClient _client;

and the constructor:

public SearchProvider() { Uri AzSearchUrl = new Uri($"https://{AzSearchServiceName}.search.windows.net"); var cred = new AzureKeyCredential(AzSearchKey); var options = new SearchClientOptions { Retry = { Mode = Azure.Core.RetryMode.Exponential, MaxRetries = 6, Delay = TimeSpan.FromMilliseconds(200), MaxDelay = TimeSpan.FromSeconds(10) } }; _client = new SearchClient(AzSearchUrl, AzSearchIndexName, cred, options); }

and the method to do the search, simplified. I will leave out the filter building interceptor because that seems to work - it builds the filter - and the presence or absence of the filter in the search doesn't make a difference whether it works.

public async Task<ResultContainer> SearchAsync(SearchModel search, IPrincipal principal) { var term = string.IsNullOrWhiteSpace(search.SearchTerm) ? "*" : EscapeSearchTerm(search.SearchTerm); var options = new SearchOptions { Size = 1,// search.PageSize, //Skip = Math.Max((search.PageNumber ?? 0 - 1) * search.PageSize, 0), IncludeTotalCount = true, //QueryType = Azure.Search.Documents.Models.SearchQueryType.Simple, //SearchMode = Azure.Search.Documents.Models.SearchMode.Any }; if (!string.IsNullOrWhiteSpace(search.OptionalFilter)) { options.Filter = search.OptionalFilter; } try { var g = _client.GetDocumentCount(); var azresult = await _client.SearchAsync<SearchDocument>(term, options); var page = azresult.Value.GetResultsAsync().AsPages(); var tootoo = azresult.Value; return new ResultContainer { Results = new List<Result>(), LongTotal = 0, SearchModel = search, Skip = 0, Take = 0, Total = 0 }; } catch (Exception ex) { Log.Error(ex, "Error doing Azure Search"); return new ResultContainer { Results = new List<Result>(), LongTotal = 0, SearchModel = search, Skip = 0, Take = 0, Total = 0 }; } }

And in the controller we're calling it like

var results = await _providerInstance.SearchAsync(searchModel, User);

So when I trigger the controller action for the search, it gets to the place where we call

var azresult = await _client.SearchAsync<SearchDocument>(term, options);

The preceding line is a bit of debugging I put in, and at the breakpoint where we're waiting for this to fire, "g" has a value which reflects the amount of items in the search index. So I know the azure portion is working at least in that way.

I have tried paring down the number of results to 1, removing the filters, testing the filters outside of the code within the azure search site, and always _client.SearchAsync just ends. It does not throw an exception, it ends, and provider.SearchAsync returns null. This generates a nullreferenceexception in the controller. So there's no helpful information for me from the error. If I try using F11 to "step into" the method that just boots me right to the controller where the exception is.

What can I do to even debug this, let alone resolve the problem?

Read Entire Article