Azure Search .Net SDK potentially does not return all requested results to a request. In this case it will return a ContinuationToken as part of the result (DocumentSearchResult).
If Azure Search can't include all results in a single response, the response returned will include a continuation token that can be passed to ContinueSearch to retrieve more results. See DocumentSearchResultBase.ContinuationToken for more information. Note that this method is not meant to help you implement paging of search results. You can implement paging using the Top and Skip parameters to the Search method.
As such, it is recommended that when a ContinuationToken is returned, a call is made to ContinueSearch to get the rest of the results.
What is the best/recommended way to combine two objects of Type DocumentSearchResult<T>
(one having come from the original Search and the other from ContinueSearch) so that I can return all results together to the consumer?
Here's my first stab at it ("PerformSearch" is the method to be called that should return all results):
private DocumentSearchResult<T> PerformSearch<T>(string searchText, SearchParameters searchParameters) where T : class
{
var searchIndexClient = GetSearchIndexClient<T>();
var searchResults = searchIndexClient.Documents.Search<T>(searchText, searchParameters);
if (searchResults.ContinuationToken != null)
{
ContinueSearch(searchResults, searchIndexClient, searchResults.ContinuationToken);
}
return searchResults;
}
private void ContinueSearch<T>(DocumentSearchResult<T> previousResults, SearchIndexClient searchIndexClient, SearchContinuationToken continuationToken) where T : class
{
var results = searchIndexClient.Documents.ContinueSearch<T>(continuationToken);
previousResults.AddResults(results);
if (results.ContinuationToken != null)
{
ContinueSearch(previousResults, searchIndexClient, results.ContinuationToken);
}
}
public static void AddResults<T>(this DocumentSearchResult<T> first, DocumentSearchResult<T> second) where T : class
{
foreach (var searchResult in second.Results)
{
first.Results.Add(searchResult);
}
foreach (var facet in second.Facets)
{
first.Facets.Add(facet.Key, facet.Value);
}
}