NuGet search results aren't sorted by relevance

The sort order seems random. Sometimes it changes when you repeat the search. This makes it a bit harder to find/install NuGet packages.

  • Studio version: 2021.4.4
  • Steps to reproduce
    1. In a Studio project, click Manage Packages.
    2. Click All Packages.
    3. Type Newtonsoft.Json in the search box. Note the search result order.
    4. Clear the search box.
    5. Type Newtonsoft.Json again. The search result order may be different.
  • Expected behavior
    • Newtonsoft.Json is the top result when searching for Newtonsoft.Json.
    • Search is deterministic (except when the feed changes).
  • Actual behavior
    • Newtonsoft.Json doesn’t appear in the search results at all.
    • The search result order changes from search to search.

Here are the results from my first search.

I cleared the search box and typed in Newtonsoft.Json again. 5 of the 7 results changed. The order of the 2 results that were the same also changed. Newtonsoft.Json didn’t appear in the results at all.

Update:

Ok, I figured part of this out. Newtonsoft.Json doesn’t show up in UiPath’s search results because UiPath apparently displays packages’ titles instead of their ids. nuget.org does the opposite, favoring ids and not displaying titles at all as far as I can tell.

nuget.org does consider title in searches but weights id higher. For example, search for Json.NET. The ID of the first result (currently) is Json.Net (n.b. the capitalization), which is not the famous Newtonsoft.Json package by James Newton-King but “a minimalistic JSON handler library” by GitHub user obarlik. The second result is the Newton-King package. Its title is set to Json.NET, confusingly enough, so NuGet included it as a secondary result.

When you search nuget.org for Newtonsoft.Json, it’s the first result as expected, because that’s its ID—even though its title is Json.NET.

Most NuGet clients work like that in my experience—they search for and install packages by id (Visual Studio, NuGet’s official SearchQueryService; Install-Package, dotnet add package, Interactive, Visual Studio). UiPath’s Manage Packages interface is the first I’ve encountered where title seems to play that role instead. That seems counterintuitive. Even once it’s understood, it gives the developer the additional work of finding and remembering aliases for their dependencies (title), since they use id everywhere else (even in UiPath’s own project.json, if they ever pop the hood).

Anyway, this doesn’t explain search result order. Even when searching by title for Json.NET, neither Newtonsoft.Json nor Json.NET is among the top results.

Here’s a workaround for those who want to conveniently install NuGet packages by ID.

This PowerShell script searches NuGet for the package of your choice, grabs its version, and adds them to your UiPath project.json's dependencies.

Save it as Add-UiPathNuGetPackage.ps1 somewhere in your PATH, or put it in a module, or drop it in your project directory.

Sample usage: Add-UiPathNuGetPackage Newtonsoft.Json

This searches NuGet for Newtonsoft.Json, gets the version number of the first result, and adds it to project.json in the current directory.

PowerShell:

param
(
    [Parameter(Mandatory)]
    [string]
    $PackageName
)

$nugetSearchBaseUrl = (Invoke-RestMethod https://api.nuget.org/v3/index.json | Select-Object -ExpandProperty resources | Where-Object { $_.'@type' -eq 'SearchQueryService' -and $_.comment -like '*primary*' }).'@id'
$topResult = (Invoke-RestMethod "$($nugetSearchBaseUrl)?q=$PackageName").data | Select-Object -First 1

$project = Get-Content '.\project.json' | ConvertFrom-Json
$project.dependencies | Add-Member -NotePropertyName $topResult.id -NotePropertyValue "[$($topResult.version)]"
$project | ConvertTo-Json -Depth 100 | Set-Content '.\project.json'