Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new Azure Search API #1015

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions src/Statiq.Web.Azure/DeploySearchIndex.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Azure;
using Microsoft.Extensions.Logging;
using Polly;
using Statiq.Common;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.Models;

namespace Statiq.Web.Azure
{
Expand Down Expand Up @@ -41,7 +40,7 @@ public DeploySearchIndex(
Config<string> searchServiceName,
Config<string> indexName,
Config<string> apiKey,
Config<IEnumerable<Field>> fields)
Config<IEnumerable<SearchField>> fields)
: base(
new Dictionary<string, IConfig>
{
Expand All @@ -59,70 +58,67 @@ protected override async Task<IEnumerable<IDocument>> ExecuteConfigAsync(IDocume
string searchServiceName = values.GetString(SearchServiceName) ?? throw new ExecutionException("Invalid search service name");
string indexName = values.GetString(IndexName) ?? throw new ExecutionException("Invalid search index name");
string apiKey = values.GetString(ApiKey) ?? throw new ExecutionException("Invalid search API key");
IList<Field> fields = values.GetList<Field>(Fields)?.ToList() ?? throw new ExecutionException("Invalid search fields");
IList<SearchField> fields = values.GetList<SearchField>(Fields)?.ToList() ?? throw new ExecutionException("Invalid search fields");

SearchServiceClient client = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));

// Delete the index if it currently exists (recreating is the easiest way to update it)
CorsOptions corsOptions = null;
if (await client.Indexes.ExistsAsync(indexName, null, context.CancellationToken))
if (!Uri.TryCreate(searchServiceName, UriKind.Absolute, out Uri searchServiceUri))
{
// Get the CORS options because we'll need to recreate those
Microsoft.Azure.Search.Models.Index existingIndex = await client.Indexes.GetAsync(indexName, null, context.CancellationToken);
corsOptions = existingIndex.CorsOptions;

// Delete the existing index
context.LogDebug($"Deleting existing search index {indexName}");
await client.Indexes.DeleteAsync(indexName, null, null, context.CancellationToken);
throw new ExecutionException("Invalid search service name");
}

SearchIndexClient searchIndexClient = new SearchIndexClient(searchServiceUri, new AzureKeyCredential(apiKey));

// Create the index
Microsoft.Azure.Search.Models.Index index = new Microsoft.Azure.Search.Models.Index
SearchIndex index = new SearchIndex(indexName)
{
Name = indexName,
Fields = fields,
CorsOptions = corsOptions
};
context.LogDebug($"Creating search index {indexName}");
await client.Indexes.CreateAsync(index, null, context.CancellationToken);
await searchIndexClient.CreateOrUpdateIndexAsync(index, true, true, context.CancellationToken);

// Upload the documents to the search index in batches
context.LogDebug($"Uploading {context.Inputs.Length} documents to search index {indexName}...");
ISearchIndexClient indexClient = client.Indexes.GetClient(indexName);
SearchClient indexClient = searchIndexClient.GetSearchClient(indexName);
int start = 0;
do
{
// Create the dynamic search documents and batch
IndexAction<Microsoft.Azure.Search.Models.Document>[] indexActions = context.Inputs
IndexDocumentsAction<SearchDocument>[] indexActions = context.Inputs
.Skip(start)
.Take(BatchSize)
.Select(doc =>
{
Microsoft.Azure.Search.Models.Document searchDocument = new Microsoft.Azure.Search.Models.Document();
foreach (Field field in fields)
SearchDocument searchDocument = new SearchDocument();
foreach (SearchField field in fields)
{
if (doc.ContainsKey(field.Name))
{
searchDocument[field.Name] = doc.Get(field.Name);
}
}
return IndexAction.Upload(searchDocument);

return IndexDocumentsAction.Upload(searchDocument);
})
.ToArray();
IndexBatch<Microsoft.Azure.Search.Models.Document> indexBatch = IndexBatch.New(indexActions);
IndexDocumentsBatch<SearchDocument> indexBatch = IndexDocumentsBatch.Create(indexActions);

// Upload the batch with exponential retry for failures
await Policy
.Handle<IndexBatchException>()
.WaitAndRetryAsync(
5,
attempt =>
{
context.LogWarning($"Failure while uploading batch {(start / BatchSize) + 1}, retry number {attempt}");
return TimeSpan.FromSeconds(Math.Pow(2, attempt));
},
(ex, _) => indexBatch = ((IndexBatchException)ex).FindFailedActionsToRetry(indexBatch, fields.Single(x => x.IsKey == true).Name))
.ExecuteAsync(async ct => await indexClient.Documents.IndexAsync(indexBatch, null, ct), context.CancellationToken);
.Handle<RequestFailedException>()
.OrResult<IndexDocumentsResult>(r => r.Results.Any(result => !result.Succeeded))
.WaitAndRetryAsync(
5,
attempt =>
{
context.LogWarning($"Failure while uploading batch {(start / BatchSize) + 1}, retry number {attempt}");
return TimeSpan.FromSeconds(Math.Pow(2, attempt));
},
(ex, _) =>
{
IEnumerable<string> failedResults = ex.Result.Results.Where(r => !r.Succeeded).Select(result => result.Key);
IEnumerable<IndexDocumentsAction<SearchDocument>> failedActions = indexActions.Where(action => action.Document.Keys.Any(key => failedResults.Contains(key)));
indexBatch = IndexDocumentsBatch.Create(failedActions.ToArray());
})
.ExecuteAsync(async ct => await indexClient.IndexDocumentsAsync(indexBatch, null, ct), context.CancellationToken);

context.LogDebug($"Uploaded {start + indexActions.Length} documents to search index {indexName}");
start += 1000;
Expand All @@ -132,4 +128,4 @@ await Policy
return context.Inputs;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Statiq.Web.Azure/Statiq.Web.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageTags>Statiq Static StaticContent StaticSite Blog BlogEngine Azure AppService</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Search" Version="10.1.0" />
<PackageReference Include="Azure.Search.Documents" Version="11.4.0" />
<PackageReference Include="Polly" Version="7.1.1" />
</ItemGroup>

Expand All @@ -21,4 +21,4 @@
</Otherwise>
</Choose>

</Project>
</Project>
Loading