コード例 #1
0
        public async Task <SearchResponse> SearchAsync(
            string query,
            int skip = 0,
            int take = 20,
            bool includePrerelease = true,
            bool includeSemVer2    = true,
            string packageType     = null,
            string framework       = null,
            CancellationToken cancellationToken = default)
        {
            var result   = new List <SearchResult>();
            var packages = await SearchImplAsync(
                query,
                skip,
                take,
                includePrerelease,
                includeSemVer2,
                packageType,
                framework,
                cancellationToken);

            foreach (var package in packages)
            {
                var versions = package.OrderByDescending(p => p.Version).ToList();
                var latest   = versions.First();

                var versionResults = versions.Select(p => new SearchResultVersion(
                                                         registrationLeafUrl: _url.GetRegistrationLeafUrl(p.Id, p.Version),
                                                         p.Version,
                                                         p.Downloads));

                result.Add(new SearchResult(
                               latest.Id,
                               latest.Version,
                               latest.Description,
                               latest.Authors,
                               latest.IconUrlString,
                               latest.LicenseUrlString,
                               latest.ProjectUrlString,
                               registrationIndexUrl: _url.GetRegistrationIndexUrl(latest.Id),
                               latest.Summary,
                               latest.Tags,
                               latest.Title,
                               versions.Sum(p => p.Downloads),
                               versionResults.ToList()));
            }

            return(new SearchResponse(
                       result.Count,
                       result,
                       SearchContext.Default(_url.GetPackageMetadataResourceUrl())));
        }
コード例 #2
0
        public async Task <SearchResponse> SearchAsync(
            SearchRequest request,
            CancellationToken cancellationToken)
        {
            var result   = new List <SearchResult>();
            var packages = await SearchImplAsync(
                request,
                cancellationToken);

            foreach (var package in packages)
            {
                var versions = package.OrderByDescending(p => p.Version).ToList();
                var latest   = versions.First();
                var iconUrl  = latest.HasEmbeddedIcon
                    ? _url.GetPackageIconDownloadUrl(latest.Id, latest.Version)
                    : latest.IconUrlString;

                result.Add(new SearchResult
                {
                    PackageId            = latest.Id,
                    Version              = latest.Version.ToFullString(),
                    Description          = latest.Description,
                    Authors              = latest.Authors,
                    IconUrl              = iconUrl,
                    LicenseUrl           = latest.LicenseUrlString,
                    ProjectUrl           = latest.ProjectUrlString,
                    RegistrationIndexUrl = _url.GetRegistrationIndexUrl(latest.Id),
                    Summary              = latest.Summary,
                    Tags           = latest.Tags,
                    Title          = latest.Title,
                    TotalDownloads = versions.Sum(p => p.Downloads),
                    Versions       = versions
                                     .Select(p => new SearchResultVersion
                    {
                        RegistrationLeafUrl = _url.GetRegistrationLeafUrl(p.Id, p.Version),
                        Version             = p.Version.ToFullString(),
                        Downloads           = p.Downloads,
                    })
                                     .ToList()
                });
            }

            return(new SearchResponse
            {
                TotalHits = result.Count,
                Data = result,
                Context = SearchContext.Default(_url.GetPackageMetadataResourceUrl())
            });
        }
コード例 #3
0
        public async Task <RegistrationIndexResponse> GetRegistrationIndexOrNullAsync(string id, CancellationToken cancellationToken = default)
        {
            // Find the packages that match the given package id from the upstream, if
            // one is configured. If these packages cannot be found on the upstream,
            // we'll return the local packages instead.
            var packages = await _mirror.FindPackagesOrNullAsync(id, cancellationToken);

            if (packages == null)
            {
                packages = await _packages.FindAsync(id, includeUnlisted : true);
            }

            if (!packages.Any())
            {
                return(null);
            }

            var versions = packages.Select(p => p.Version).ToList();

            // TODO: Paging of registration items.
            // "Un-paged" example: https://api.nuget.org/v3/registration3/newtonsoft.json/index.json
            // Paged example: https://api.nuget.org/v3/registration3/fake/index.json
            return(new BaGetRegistrationIndexResponse(
                       type: RegistrationIndexResponse.DefaultType,
                       count: packages.Count,
                       totalDownloads: packages.Sum(p => p.Downloads),
                       pages: new[]
            {
                new RegistrationIndexPage(
                    pageUrl: _url.GetRegistrationIndexUrl(packages.First().Id),
                    count: packages.Count(),
                    itemsOrNull: packages.Select(ToRegistrationIndexPageItem).ToList(),
                    lower: versions.Min(),
                    upper: versions.Max())
            }));
        }
コード例 #4
0
ファイル: TableSearchService.cs プロジェクト: vjacquet/BaGet
        private SearchResult ToSearchResult(IReadOnlyList <PackageEntity> packages)
        {
            NuGetVersion  latestVersion  = null;
            PackageEntity latest         = null;
            var           versions       = new List <SearchResultVersion>();
            long          totalDownloads = 0;

            foreach (var package in packages)
            {
                var version = NuGetVersion.Parse(package.OriginalVersion);

                totalDownloads += package.Downloads;
                versions.Add(new SearchResultVersion
                {
                    RegistrationLeafUrl = _url.GetRegistrationLeafUrl(package.Id, version),
                    Version             = package.NormalizedVersion,
                    Downloads           = package.Downloads,
                });

                if (latestVersion == null || version > latestVersion)
                {
                    latest        = package;
                    latestVersion = version;
                }
            }

            var iconUrl = latest.HasEmbeddedIcon
                ? _url.GetPackageIconDownloadUrl(latest.Id, latestVersion)
                : latest.IconUrl;

            return(new SearchResult
            {
                PackageId = latest.Id,
                Version = latest.NormalizedVersion,
                Description = latest.Description,
                Authors = JsonConvert.DeserializeObject <string[]>(latest.Authors),
                IconUrl = iconUrl,
                LicenseUrl = latest.LicenseUrl,
                ProjectUrl = latest.ProjectUrl,
                RegistrationIndexUrl = _url.GetRegistrationIndexUrl(latest.Id),
                Summary = latest.Summary,
                Tags = JsonConvert.DeserializeObject <string[]>(latest.Tags),
                Title = latest.Title,
                TotalDownloads = totalDownloads,
                Versions = versions,
            });
        }
コード例 #5
0
        public async Task <SearchResponse> SearchAsync(BaGetSearchRequest request, CancellationToken cancellationToken)
        {
            var result   = new List <SearchResult>();
            var packages = await SearchImplAsync(request, cancellationToken);

            foreach (var package in packages)
            {
                var versions = package.OrderByDescending(p => p.Version).ToList();
                var latest   = versions.First();

                var versionResults = versions.Select(p => new SearchResultVersion(
                                                         registrationLeafUrl: _url.GetRegistrationLeafUrl(p.Id, p.Version),
                                                         p.Version,
                                                         p.Downloads));

                result.Add(new SearchResult(
                               latest.Id,
                               latest.Version,
                               latest.Description,
                               latest.Authors,
                               latest.IconUrlString,
                               latest.LicenseUrlString,
                               latest.ProjectUrlString,
                               registrationIndexUrl: _url.GetRegistrationIndexUrl(latest.Id),
                               latest.Summary,
                               latest.Tags,
                               latest.Title,
                               versions.Sum(p => p.Downloads),
                               versionResults.ToList()));
            }

            return(new SearchResponse(
                       result.Count,
                       result,
                       SearchContext.Default(_url.GetPackageMetadataResourceUrl())));
        }
コード例 #6
0
ファイル: AzureSearchService.cs プロジェクト: xiaopohou/BaGet
        public async Task <SearchResponse> SearchAsync(
            string query,
            int skip = 0,
            int take = 20,
            bool includePrerelease = true,
            bool includeSemVer2    = true,
            string packageType     = null,
            string framework       = null,
            CancellationToken cancellationToken = default)
        {
            var searchText = BuildSeachQuery(query, packageType, framework);
            var filter     = BuildSearchFilter(includePrerelease, includeSemVer2);
            var parameters = new SearchParameters
            {
                IncludeTotalResultCount = true,
                QueryType = QueryType.Full,
                Skip      = skip,
                Top       = take,
                Filter    = filter
            };

            var response = await _searchClient.Documents.SearchAsync <PackageDocument>(searchText, parameters, cancellationToken : cancellationToken);

            var results = new List <SearchResult>();

            foreach (var result in response.Results)
            {
                var document = result.Document;
                var versions = new List <SearchResultVersion>();

                if (document.Versions.Length != document.VersionDownloads.Length)
                {
                    throw new InvalidOperationException($"Invalid document {document.Key} with mismatched versions");
                }

                for (var i = 0; i < document.Versions.Length; i++)
                {
                    var downloads = long.Parse(document.VersionDownloads[i]);
                    var version   = NuGetVersion.Parse(document.Versions[i]);
                    var url       = _url.GetRegistrationLeafUrl(document.Id, version);

                    versions.Add(new SearchResultVersion(url, version, downloads));
                }

                results.Add(new SearchResult(
                                document.Id,
                                NuGetVersion.Parse(document.Version),
                                document.Description,
                                document.Authors,
                                document.IconUrl,
                                document.LicenseUrl,
                                document.ProjectUrl,
                                _url.GetRegistrationIndexUrl(document.Id),
                                document.Summary,
                                document.Tags,
                                document.Title,
                                document.TotalDownloads,
                                versions));
            }

            return(new SearchResponse(
                       response.Count.Value,
                       results,
                       SearchContext.Default(_url.GetPackageMetadataResourceUrl())));
        }
コード例 #7
0
        public async Task <SearchResponse> SearchAsync(
            SearchRequest request,
            CancellationToken cancellationToken)
        {
            var searchText = BuildSeachQuery(request.Query, request.PackageType, request.Framework);
            var filter     = BuildSearchFilter(request.IncludePrerelease, request.IncludeSemVer2);
            var parameters = new SearchParameters
            {
                IncludeTotalResultCount = true,
                QueryType = QueryType.Full,
                Skip      = request.Skip,
                Top       = request.Take,
                Filter    = filter
            };

            var response = await _searchClient.Documents.SearchAsync <PackageDocument>(
                searchText,
                parameters,
                cancellationToken : cancellationToken);

            var results = new List <SearchResult>();

            foreach (var result in response.Results)
            {
                var document = result.Document;
                var versions = new List <SearchResultVersion>();

                if (document.Versions.Length != document.VersionDownloads.Length)
                {
                    throw new InvalidOperationException($"Invalid document {document.Key} with mismatched versions");
                }

                for (var i = 0; i < document.Versions.Length; i++)
                {
                    var version = NuGetVersion.Parse(document.Versions[i]);

                    versions.Add(new SearchResultVersion
                    {
                        RegistrationLeafUrl = _url.GetRegistrationLeafUrl(document.Id, version),
                        Version             = document.Versions[i],
                        Downloads           = long.Parse(document.VersionDownloads[i]),
                    });
                }

                var iconUrl = document.HasEmbeddedIcon
                    ? _url.GetPackageIconDownloadUrl(document.Id, NuGetVersion.Parse(document.Version))
                    : document.IconUrl;

                results.Add(new SearchResult
                {
                    PackageId            = document.Id,
                    Version              = document.Version,
                    Description          = document.Description,
                    Authors              = document.Authors,
                    IconUrl              = iconUrl,
                    LicenseUrl           = document.LicenseUrl,
                    ProjectUrl           = document.ProjectUrl,
                    RegistrationIndexUrl = _url.GetRegistrationIndexUrl(document.Id),
                    Summary              = document.Summary,
                    Tags           = document.Tags,
                    Title          = document.Title,
                    TotalDownloads = document.TotalDownloads,
                    Versions       = versions
                });
            }

            return(new SearchResponse
            {
                TotalHits = response.Count.Value,
                Data = results,
                Context = SearchContext.Default(_url.GetPackageMetadataResourceUrl())
            });
        }