From 4125a7aa00cf862b9f57535b59d40b8696348823 Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Fri, 19 Jul 2019 13:06:40 -0700 Subject: [PATCH] Default luceneQuery to true in Azure Search Service (#603) This is necessary for hijack to work properly. Old search defaulted it to true. Address https://github.com/NuGet/NuGetGallery/issues/7360 --- .../Controllers/SearchController.cs | 2 +- .../TestSupport/V2SearchBuilder.cs | 8 ++- .../BasicTests/V2SearchProtocolTests.cs | 52 +++++++++++++++++++ .../Controllers/SearchControllerFacts.cs | 2 +- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Services.SearchService/Controllers/SearchController.cs b/src/NuGet.Services.SearchService/Controllers/SearchController.cs index 409654db7..3655be92f 100644 --- a/src/NuGet.Services.SearchService/Controllers/SearchController.cs +++ b/src/NuGet.Services.SearchService/Controllers/SearchController.cs @@ -77,7 +77,7 @@ public async Task V2SearchAsync( string semVerLevel = null, string q = null, string sortBy = null, - bool luceneQuery = false, + bool luceneQuery = true, bool debug = false) { await EnsureInitializedAsync(); diff --git a/tests/BasicSearchTests.FunctionalTests.Core/TestSupport/V2SearchBuilder.cs b/tests/BasicSearchTests.FunctionalTests.Core/TestSupport/V2SearchBuilder.cs index cc687786a..af2edc441 100644 --- a/tests/BasicSearchTests.FunctionalTests.Core/TestSupport/V2SearchBuilder.cs +++ b/tests/BasicSearchTests.FunctionalTests.Core/TestSupport/V2SearchBuilder.cs @@ -1,7 +1,6 @@ // 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.Specialized; namespace BasicSearchTests.FunctionalTests.Core.TestSupport @@ -20,6 +19,8 @@ public class V2SearchBuilder : QueryBuilder public string SortBy { get; set; } + public bool? LuceneQuery { get; set; } + public V2SearchBuilder() : base("/search/query?") { } protected override NameValueCollection GetQueryString() @@ -50,6 +51,11 @@ protected override NameValueCollection GetQueryString() queryString["sortBy"] = SortBy; } + if (LuceneQuery.HasValue) + { + queryString["luceneQuery"] = LuceneQuery.ToString(); + } + return queryString; } } diff --git a/tests/NuGet.Services.AzureSearch.FunctionalTests/BasicTests/V2SearchProtocolTests.cs b/tests/NuGet.Services.AzureSearch.FunctionalTests/BasicTests/V2SearchProtocolTests.cs index f49b76d54..df3fc7b84 100644 --- a/tests/NuGet.Services.AzureSearch.FunctionalTests/BasicTests/V2SearchProtocolTests.cs +++ b/tests/NuGet.Services.AzureSearch.FunctionalTests/BasicTests/V2SearchProtocolTests.cs @@ -86,6 +86,58 @@ public async Task CountOnlyReturnsCountOfSearchResults() Assert.Null(results.Data); } + /// + /// This is the query pattern used by gallery to handle "FindPackagesById()?id={id}" OData queries. + /// + [Fact] + public async Task ODataFindPackagesById() + { + var results = await V2SearchAsync(new V2SearchBuilder + { + Query = $"Id:\"{Constants.TestPackageId}\"", + Skip = 0, + Take = 100, + SortBy = "relevance", + IncludeSemVer2 = true, + Prerelease = true, + IgnoreFilter = true, + LuceneQuery = null, + }); + + Assert.NotNull(results); + Assert.True(results.TotalHits >= 1); + Assert.NotEmpty(results.Data); + foreach (var result in results.Data) + { + Assert.Equal(Constants.TestPackageId, result.PackageRegistration.Id); + } + } + + /// + /// This is the query pattern used by gallery to handle "Packages(Id='{id}',Version='{version}')" OData queries. + /// + [Fact] + public async Task ODataSpecificPackage() + { + var results = await V2SearchAsync(new V2SearchBuilder + { + Query = $"Id:\"{Constants.TestPackageId}\" AND Version:\"{Constants.TestPackageVersion}\"", + Skip = 0, + Take = 1, + SortBy = "relevance", + IncludeSemVer2 = true, + Prerelease = true, + IgnoreFilter = true, + LuceneQuery = null, + }); + + Assert.NotNull(results); + Assert.Equal(1, results.TotalHits); + var package = Assert.Single(results.Data); + Assert.Equal(Constants.TestPackageId, package.PackageRegistration.Id); + Assert.Equal(Constants.TestPackageVersion, package.NormalizedVersion); + } + [Fact] public async Task ResultsHonorPreReleaseField() { diff --git a/tests/NuGet.Services.SearchService.Tests/Controllers/SearchControllerFacts.cs b/tests/NuGet.Services.SearchService.Tests/Controllers/SearchControllerFacts.cs index 59538eaed..7446b4223 100644 --- a/tests/NuGet.Services.SearchService.Tests/Controllers/SearchControllerFacts.cs +++ b/tests/NuGet.Services.SearchService.Tests/Controllers/SearchControllerFacts.cs @@ -150,7 +150,7 @@ public async Task HasDefaultParameters() Assert.False(lastRequest.IncludePrerelease); Assert.False(lastRequest.IncludeSemVer2); Assert.Null(lastRequest.Query); - Assert.False(lastRequest.LuceneQuery); + Assert.True(lastRequest.LuceneQuery); Assert.False(lastRequest.ShowDebug); }