public async Task <IHttpActionResult> Search( ODataQueryOptions <V1FeedPackage> options, [FromODataUri] string searchTerm = "", [FromODataUri] string targetFramework = "") { return(await SearchAsync( options, searchTerm, targetFramework, _featureFlagService.IsODataV1SearchNonHijackedEnabled())); }
public async Task <IHttpActionResult> Search( ODataQueryOptions <V1FeedPackage> options, [FromODataUri] string searchTerm = "", [FromODataUri] string targetFramework = "") { // Handle OData-style |-separated list of frameworks. string[] targetFrameworkList = (targetFramework ?? "").Split(new[] { '\'', '|' }, StringSplitOptions.RemoveEmptyEntries); // For now, we'll just filter on the first one. if (targetFrameworkList.Length > 0) { // Until we support multiple frameworks, we need to prefer aspnet50 over aspnetcore50. if (targetFrameworkList.Contains("aspnet50")) { targetFramework = "aspnet50"; } else { targetFramework = targetFrameworkList[0]; } } // Perform actual search var packages = GetAll() .Include(p => p.PackageRegistration) .Include(p => p.PackageRegistration.Owners) .Where(p => p.Listed && !p.IsPrerelease && p.PackageStatusKey == PackageStatus.Available) .Where(SemVerLevelKey.IsUnknownPredicate()) .OrderBy(p => p.PackageRegistration.Id).ThenBy(p => p.Version) .AsNoTracking(); // todo: search hijack should take queryOptions instead of manually parsing query options var searchService = _searchServiceFactory.GetService(); var searchAdaptorResult = await SearchAdaptor.SearchCore( searchService, GetTraditionalHttpContext().Request, packages, searchTerm, targetFramework, includePrerelease : false, semVerLevel : null); // Packages provided by search service (even when not hijacked) var query = searchAdaptorResult.Packages; bool?customQuery = null; // If intercepted, create a paged queryresult if (searchAdaptorResult.ResultsAreProvidedBySearchService) { customQuery = false; // Add explicit Take() needed to limit search hijack result set size if $top is specified var totalHits = query.LongCount(); var pagedQueryable = query .Take(options.Top != null ? Math.Min(options.Top.Value, MaxPageSize) : MaxPageSize) .ToV1FeedPackageQuery(GetSiteRoot()); return(TrackedQueryResult( options, pagedQueryable, MaxPageSize, totalHits, (o, s, resultCount) => SearchAdaptor.GetNextLink(Request.RequestUri, resultCount, new { searchTerm, targetFramework }, o, s), customQuery)); } else { customQuery = true; } if (!_featureFlagService.IsODataV1SearchNonHijackedEnabled()) { return(BadRequest(Strings.ODataParametersDisabled)); } if (!ODataQueryVerifier.AreODataOptionsAllowed(options, ODataQueryVerifier.V1Search, _configurationService.Current.IsODataFilterEnabled, nameof(Search))) { return(BadRequest(ODataQueryVerifier.GetValidationFailedMessage(options))); } // If not, just let OData handle things var queryable = query.ToV1FeedPackageQuery(GetSiteRoot()); return(TrackedQueryResult(options, queryable, MaxPageSize, customQuery)); }