Skip to content

Instantly share code, notes, and snippets.

@AndyButland
Created May 21, 2023 12:59
Show Gist options
  • Save AndyButland/024a7e389b26378271f330bc092cd1dd to your computer and use it in GitHub Desktop.
Save AndyButland/024a7e389b26378271f330bc092cd1dd to your computer and use it in GitHub Desktop.
public async Task<bool> AddOrUpdatePackage(PackageDto packageDto)
{
if (!IsSearchServiceConfigured())
{
_logger.LogWarning($"Package with Id {packageDto.PackageId} failed to index with Azure search as the search service is not configured.");
return false;
}
SearchIndexClient indexClient = GetIndexClient();
await CreateIndexIfNotExists(indexClient);
SearchClient searchClient = indexClient.GetSearchClient(_searchSettings.IndexName);
var batch = IndexDocumentsBatch.Create(IndexDocumentsAction.MergeOrUpload(new PackageModel(packageDto)));
IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch);
IndexingResult packageIndexResult = result.Results[0];
if (packageIndexResult.Succeeded)
{
_logger.LogInformation($"Package with Id {packageDto.PackageId} was indexed with Azure search.");
return true;
}
_logger.LogError($"Package with Id {packageDto.PackageId} failed to index with Azure search. Error message: {packageIndexResult.ErrorMessage}");
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment