forked from loic-sharma/BaGet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Azure Search (loic-sharma#393)
Part of loic-sharma#362 See loic-sharma/baget.io#3⚠️ This change removes the Azure Search import tool as it has been moved here: https://github.com/loic-sharma/baget.io. If you'd like to use Azure Search, the new steps are: ```ps1 https://github.com/loic-sharma/baget.io cd baget.io/BaGet # Update the appsettings.json file with your Azure Search configurations dotnet run -- azure-search create dotnet run -- azure-search rebuild ``` The commands from baget.io will be ported to this project soon.
- Loading branch information
1 parent
c4546e0
commit a96c134
Showing
27 changed files
with
353 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Search; | ||
using Microsoft.Azure.Search.Models; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Rest.Azure; | ||
|
||
namespace BaGet.Azure.Search | ||
{ | ||
public class AzureSearchBatchIndexer | ||
{ | ||
/// <summary> | ||
/// Azure Search accepts batches of up to 1000 documents. | ||
/// </summary> | ||
public const int MaxBatchSize = 1000; | ||
|
||
private readonly ISearchIndexClient _indexClient; | ||
private readonly ILogger<AzureSearchBatchIndexer> _logger; | ||
|
||
public AzureSearchBatchIndexer( | ||
SearchServiceClient searchClient, | ||
ILogger<AzureSearchBatchIndexer> logger) | ||
{ | ||
if (searchClient == null) throw new ArgumentNullException(nameof(searchClient)); | ||
|
||
_indexClient = searchClient.Indexes.GetClient(PackageDocument.IndexName); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task IndexAsync( | ||
IReadOnlyList<IndexAction<KeyedDocument>> batch, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (batch.Count > MaxBatchSize) | ||
{ | ||
throw new ArgumentException( | ||
$"Batch cannot have more than {MaxBatchSize} elements", | ||
nameof(batch)); | ||
} | ||
|
||
IList<IndexingResult> indexingResults = null; | ||
Exception innerException = null; | ||
|
||
try | ||
{ | ||
await _indexClient.Documents.IndexAsync( | ||
IndexBatch.New(batch), | ||
cancellationToken: cancellationToken); | ||
|
||
_logger.LogInformation("Pushed batch of {DocumentCount} documents", batch.Count); | ||
|
||
} | ||
catch (IndexBatchException ex) | ||
{ | ||
_logger.LogError(ex, "An exception was thrown when pushing batch of documents"); | ||
indexingResults = ex.IndexingResults; | ||
innerException = ex; | ||
} | ||
catch (CloudException ex) when (ex.Response.StatusCode == HttpStatusCode.RequestEntityTooLarge && batch.Count > 1) | ||
{ | ||
var halfCount = batch.Count / 2; | ||
var halfA = batch.Take(halfCount).ToList(); | ||
var halfB = batch.Skip(halfCount).ToList(); | ||
|
||
_logger.LogWarning( | ||
0, | ||
ex, | ||
"The request body for a batch of {BatchSize} was too large. Splitting into two batches of size " + | ||
"{HalfA} and {HalfB}.", | ||
batch.Count, | ||
halfA.Count, | ||
halfB.Count); | ||
|
||
await IndexAsync(halfA, cancellationToken); | ||
await IndexAsync(halfB, cancellationToken); | ||
} | ||
|
||
if (indexingResults != null && indexingResults.Any(result => !result.Succeeded)) | ||
{ | ||
throw new InvalidOperationException("Failed to pushed batch of documents documents"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BaGet.Core; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace BaGet.Azure.Search | ||
{ | ||
public class AzureSearchIndexer : ISearchIndexer | ||
{ | ||
private readonly IPackageService _packages; | ||
private readonly IndexActionBuilder _actionBuilder; | ||
private readonly AzureSearchBatchIndexer _batchIndexer; | ||
private readonly ILogger<AzureSearchIndexer> _logger; | ||
|
||
public AzureSearchIndexer( | ||
IPackageService packages, | ||
IndexActionBuilder actionBuilder, | ||
AzureSearchBatchIndexer batchIndexer, | ||
ILogger<AzureSearchIndexer> logger) | ||
{ | ||
_packages = packages ?? throw new ArgumentNullException(nameof(packages)); | ||
_actionBuilder = actionBuilder ?? throw new ArgumentNullException(nameof(actionBuilder)); | ||
_batchIndexer = batchIndexer ?? throw new ArgumentNullException(nameof(batchIndexer)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task IndexAsync(Package package, CancellationToken cancellationToken = default) | ||
{ | ||
var packages = await _packages.FindAsync(package.Id, includeUnlisted: false); | ||
|
||
var actions = _actionBuilder.UpdatePackage( | ||
new PackageRegistration( | ||
package.Id, | ||
packages)); | ||
|
||
await _batchIndexer.IndexAsync(actions, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.