public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { // Get Document Id string documentId = req.Query["id"];; // Cognitive Search Uri serviceEndpoint = new Uri($"https://{searchServiceName}.search.windows.net/"); SearchClient searchClient = new SearchClient( serviceEndpoint, searchIndexName, new AzureKeyCredential(searchApiKey) ); var response = await searchClient.GetDocumentAsync <SearchDocument>(documentId); var output = new LookupOutput { Document = response.Value }; return(new OkObjectResult(output)); }
public async Task Run(string learnAimRef, ILogger log) { if (string.IsNullOrWhiteSpace(learnAimRef)) { log.LogError($"{nameof(PurgeLarsFromDatabaseAndSearch)} failed with invalid {nameof(learnAimRef)} {{{nameof(learnAimRef)}}}.", learnAimRef); } log.LogInformation($"{nameof(PurgeLarsFromDatabaseAndSearch)} invoked for {nameof(learnAimRef)} {{{nameof(learnAimRef)}}}.", learnAimRef); var deletedResult = await _sqlQueryDispatcher.ExecuteQuery(new DeleteLarsLearningDelivery(learnAimRef)); deletedResult.Switch( _ => log.LogInformation($"{nameof(PurgeLarsFromDatabaseAndSearch)} successfully deleted {nameof(learnAimRef)}: {{{nameof(learnAimRef)}}} from database.", learnAimRef), _ => log.LogWarning($"{nameof(PurgeLarsFromDatabaseAndSearch)} failed to find {nameof(learnAimRef)} {{{nameof(learnAimRef)}}} in database.", learnAimRef)); try { await _searchClient.GetDocumentAsync <dynamic>(learnAimRef); await _searchClient.DeleteDocumentsAsync("LearnAimRef", new[] { learnAimRef }, new IndexDocumentsOptions { ThrowOnAnyError = true }); log.LogInformation($"{nameof(PurgeLarsFromDatabaseAndSearch)} successfully deleted {nameof(learnAimRef)} {{{nameof(learnAimRef)}}} from search index.", learnAimRef); } catch (RequestFailedException ex) when(ex.Status == StatusCodes.Status404NotFound) { log.LogWarning($"{nameof(PurgeLarsFromDatabaseAndSearch)} failed to find {nameof(learnAimRef)} {{{nameof(learnAimRef)}}} in search.", learnAimRef); } }
public async Task GetDocumentDict() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); Response <SearchDocument> response = await client.GetDocumentAsync <SearchDocument>("3"); Assert.AreEqual(200, response.GetRawResponse().Status); Assert.AreEqual("3", response.Value["hotelId"]); }
public async Task ThrowsWhenNotFound() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await client.GetDocumentAsync <SearchDocument>("ThisDocumentDoesNotExist")); Assert.AreEqual(404, ex.Status); }
public async Task ThrowsWhenMalformed() { await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this); SearchClient client = resources.GetQueryClient(); RequestFailedException ex = await CatchAsync <RequestFailedException>( async() => await client.GetDocumentAsync <SearchDocument>( "3", new GetDocumentOptions() { SelectedFields = new[] { "ThisFieldDoesNotExist" } })); Assert.AreEqual(400, ex.Status); StringAssert.StartsWith("Invalid expression: Could not find a property named 'ThisFieldDoesNotExist' on type 'search.document'.", ex.Message); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string id = req.Query["id"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); id = id ?? data?.id; var response = await searchClient.GetDocumentAsync <SearchDocument>(id); object metadata_storage_path; response.Value.TryGetValue("metadata_storage_path", out metadata_storage_path); var storagePath = metadata_storage_path.ToString(); var startIndex = storagePath.IndexOf(Constants.containerName) + Constants.containerName.Length + 1; var blobName = storagePath.Substring(startIndex); blobName = Uri.UnescapeDataString(blobName); log.LogInformation(blobName); var policy = new BlobSasBuilder { Protocol = SasProtocol.HttpsAndHttp, BlobContainerName = storageContainerName, BlobName = blobName, Resource = "b", StartsOn = DateTimeOffset.UtcNow, ExpiresOn = DateTimeOffset.UtcNow.AddHours(1), IPRange = new SasIPRange(IPAddress.None, IPAddress.None) }; policy.SetPermissions(BlobSasPermissions.Read); var sas = policy.ToSasQueryParameters(sharedStorageCredentials); LookupOutput output = new LookupOutput(); output.document = response.Value; output.sasToken = sas.ToString(); return(new OkObjectResult(output)); }
public async Task Structs() { await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this); SimpleStructHotel document = new SimpleStructHotel { HotelId = "4", HotelName = "Value Inn" }; await resources.GetSearchClient().IndexDocumentsAsync( IndexDocumentsBatch.Upload(new[] { document })); await resources.WaitForIndexingAsync(); SearchClient client = resources.GetQueryClient(); Response <SimpleStructHotel> response = await client.GetDocumentAsync <SimpleStructHotel>(document.HotelId); Assert.AreEqual(document, response.Value); }