How do I limit the number of results coming back from the MS Search Api? Here's what the code looks like:
public async Task<string> Search()
{
var ApplicationClientID = Environment.GetEnvironmentVariable("CLIENT_ID");
var ApplicationClientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var AzureTenantID = Environment.GetEnvironmentVariable("TENANT_ID");
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
try
{
var clientSecretCredential = new ClientSecretCredential(
AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/v1.0");
Console.WriteLine($"searchQuery :{searchQuery}");
var requestBody = new Microsoft.Graph.Search.Query.QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.DriveItem,
},
Query = new SearchQuery
{
QueryString = this.searchQuery,
},
Fields = new List<string>
{
"driveId",
"listItemId",
"author",
"title",
"url",
"rank"
},
QueryAlterationOptions = new SearchAlterationOptions
{
EnableSuggestion = true,
EnableModification = true,
}
, Region ="US"
},
},
};
var searchResults = await graphClient.Search.Query.PostAsync(requestBody).ConfigureAwait(false);
//return searchResults;
var jsonResults = ExtractDriveItemDetails(searchResults);
return jsonResults;
}
catch (Exception ex)
{
log.LogError(ex.Message);
return null;
}
}
The Environmment / Setup
I'm using MS Graph 1.0. Here are the references from the csproj file:
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.0" />
<PackageReference Include="Microsoft.Graph" Version="5.18.0" />
<PackageReference Include="Microsoft.Graph.Core" Version="3.0.9" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.54.1" />
What I've tried:
I've tried to change the SearchQuery to look like this:
Query = new SearchQuery
{
QueryString = this.searchQuery,
Top = 4
},
And this:
Query = new SearchQuery
{
QueryString = this.searchQuery,
Count = 4
},
Neither work.
I also read about the EnableTopResults option but it only seems to be available for messages. https://learn.microsoft.com/en-us/graph/api/resources/searchrequest?view=graph-rest-1.0
Can you point me in the right direction?
Thank you.
What about the size property. It defines the number of items per page to be retrieved.