This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Azure Search] Support manually boosted search results (#676)
This change lets us override a package's download count in the Azure Search index, thereby allowing us to manually boost search results. This will be used to temporarily boost Azure SDK packages until we improve our new search rankings algorithm. See [NuGetDeployment#1177](https://nuget.visualstudio.com/NuGetMicrosoft/_git/NuGetDeployment/pullrequest/1177?_a=overview) Part of https://github.com/nuget/engineering/issues/2779
- Loading branch information
1 parent
5dc70b5
commit 01857b9
Showing
13 changed files
with
514 additions
and
2 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
90 changes: 90 additions & 0 deletions
90
src/NuGet.Services.AzureSearch/AuxiliaryFiles/DownloadDataExtensions.cs
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,90 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace NuGet.Services.AzureSearch.AuxiliaryFiles | ||
{ | ||
public static class DownloadDataExtensions | ||
{ | ||
public static DownloadData ApplyDownloadOverrides( | ||
this DownloadData originalData, | ||
IReadOnlyDictionary<string, long> downloadOverrides, | ||
ILogger logger) | ||
{ | ||
if (originalData == null) | ||
{ | ||
throw new ArgumentNullException(nameof(originalData)); | ||
} | ||
|
||
if (downloadOverrides == null) | ||
{ | ||
throw new ArgumentNullException(nameof(downloadOverrides)); | ||
} | ||
|
||
if (logger == null) | ||
{ | ||
throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
// Create a copy of the original data and apply overrides as we copy. | ||
var result = new DownloadData(); | ||
|
||
foreach (var downloadData in originalData) | ||
{ | ||
var packageId = downloadData.Key; | ||
|
||
if (ShouldOverrideDownloads(packageId)) | ||
{ | ||
logger.LogInformation( | ||
"Overriding downloads of package {PackageId} from {Downloads} to {DownloadsOverride}", | ||
packageId, | ||
originalData.GetDownloadCount(packageId), | ||
downloadOverrides[packageId]); | ||
|
||
var versions = downloadData.Value.Keys; | ||
|
||
result.SetDownloadCount( | ||
packageId, | ||
versions.First(), | ||
downloadOverrides[packageId]); | ||
} | ||
else | ||
{ | ||
foreach (var versionData in downloadData.Value) | ||
{ | ||
result.SetDownloadCount(downloadData.Key, versionData.Key, versionData.Value); | ||
} | ||
} | ||
} | ||
|
||
bool ShouldOverrideDownloads(string packageId) | ||
{ | ||
if (!downloadOverrides.TryGetValue(packageId, out var downloadOverride)) | ||
{ | ||
return false; | ||
} | ||
|
||
// Apply the downloads override only if the package has fewer total downloads. | ||
// In effect, this removes a package's manual boost once its total downloads exceed the override. | ||
if (originalData[packageId].Total >= downloadOverride) | ||
{ | ||
logger.LogInformation( | ||
"Skipping download override for package {PackageId} as its downloads of {Downloads} are " + | ||
"greater than its override of {DownloadsOverride}", | ||
packageId, | ||
originalData[packageId].Total, | ||
downloadOverride); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/NuGet.Services.AzureSearch/AuxiliaryFiles/DownloadOverrides.cs
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,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using NuGet.Indexing; | ||
|
||
namespace NuGet.Services.AzureSearch.AuxiliaryFiles | ||
{ | ||
public static class DownloadOverrides | ||
{ | ||
private static readonly JsonSerializer Serializer = new JsonSerializer(); | ||
|
||
public static IReadOnlyDictionary<string, long> Load(string fileName, ILoader loader, ILogger logger) | ||
{ | ||
try | ||
{ | ||
using (var reader = loader.GetReader(fileName)) | ||
{ | ||
var downloadOverrides = Serializer.Deserialize<Dictionary<string, long>>(reader); | ||
|
||
return new Dictionary<string, long>( | ||
downloadOverrides, | ||
StringComparer.OrdinalIgnoreCase); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(0, ex, "Unable to load download overrides {FileName} due to exception", fileName); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.