Skip to content

Commit

Permalink
Prevent frontend search if Azure AI is not configured (#15135)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored and hishamco committed Feb 1, 2024
1 parent 5b758f9 commit 279a031
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Azure.Search.Documents;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Contents.Indexing;
using OrchardCore.Search.Abstractions;
using OrchardCore.Search.AzureAI.Models;
Expand All @@ -13,7 +14,8 @@ public class AzureAISearchService(
ISiteService siteService,
AzureAIIndexDocumentManager indexDocumentManager,
AzureAISearchIndexSettingsService indexSettingsService,
ILogger<AzureAISearchService> logger
ILogger<AzureAISearchService> logger,
IOptions<AzureAISearchDefaultOptions> azureAIOptions
) : ISearchService
{
public const string Key = "Azure AI Search";
Expand All @@ -22,17 +24,26 @@ ILogger<AzureAISearchService> logger
private readonly AzureAIIndexDocumentManager _indexDocumentManager = indexDocumentManager;
private readonly AzureAISearchIndexSettingsService _indexSettingsService = indexSettingsService;
private readonly ILogger<AzureAISearchService> _logger = logger;
private readonly AzureAISearchDefaultOptions _azureAIOptions = azureAIOptions.Value;

public string Name => Key;

public async Task<SearchResult> SearchAsync(string indexName, string term, int start, int size)
{
var result = new SearchResult();

if (!_azureAIOptions.IsConfigurationExists())
{
_logger.LogWarning("Azure AI Search: Couldn't execute search. The Azure AI Search has not been configured.");

return result;
}

var siteSettings = await _siteService.GetSiteSettingsAsync();
var searchSettings = siteSettings.As<AzureAISearchSettings>();

var index = !string.IsNullOrWhiteSpace(indexName) ? indexName.Trim() : searchSettings.SearchIndex;

var result = new SearchResult();
if (string.IsNullOrEmpty(index))
{
_logger.LogWarning("Azure AI Search: Couldn't execute search. No search provider settings was defined.");
Expand Down

0 comments on commit 279a031

Please sign in to comment.