Esempio n. 1
0
        public V3SearchResponse V3FromSearch(
            V3SearchRequest request,
            string text,
            SearchParameters parameters,
            DocumentSearchResult <SearchDocument.Full> result,
            TimeSpan duration)
        {
            var results = result.Results;

            result.Results = null;

            var registrationsBaseUrl = GetRegistrationsBaseUrl(request.IncludeSemVer2);

            return(new V3SearchResponse
            {
                Context = GetV3SearchContext(registrationsBaseUrl),
                TotalHits = result.Count.Value,
                Data = results
                       .Select(x =>
                {
                    var package = ToV3SearchPackage(x.Document, registrationsBaseUrl);
                    package.Debug = request.ShowDebug ? x : null;
                    return package;
                })
                       .ToList(),
                Debug = DebugInformation.CreateFromSearchOrNull(
                    request,
                    _options.Value.SearchIndexName,
                    parameters,
                    text,
                    result,
                    duration,
                    AuxiliaryData.Metadata),
            });
        }
        private V2SearchResponse ToResponse <T>(
            V2SearchRequest request,
            SearchParameters parameters,
            string text,
            string indexName,
            DocumentSearchResult <T> result,
            TimeSpan duration,
            Func <T, V2SearchPackage> toPackage)
            where T : class
        {
            var results = result.Results;

            result.Results = null;

            if (request.CountOnly)
            {
                return(new V2SearchResponse
                {
                    TotalHits = result.Count.Value,
                    Debug = DebugInformation.CreateFromSearchOrNull(
                        request,
                        indexName,
                        parameters,
                        text,
                        result,
                        duration,
                        AuxiliaryData.Metadata),
                });
            }
            else
            {
                return(new V2SearchResponse
                {
                    TotalHits = result.Count.Value,
                    Data = results
                           .Select(x =>
                    {
                        var package = toPackage(x.Document);
                        package.Debug = request.ShowDebug ? x : null;
                        return package;
                    })
                           .ToList(),
                    Debug = DebugInformation.CreateFromSearchOrNull(
                        request,
                        indexName,
                        parameters,
                        text,
                        result,
                        duration,
                        AuxiliaryData.Metadata),
                });
            }
        }
Esempio n. 3
0
        public AutocompleteResponse AutocompleteFromSearch(
            AutocompleteRequest request,
            string text,
            SearchParameters parameters,
            DocumentSearchResult <SearchDocument.Full> result,
            TimeSpan duration)
        {
            var results = result.Results;

            result.Results = null;

            List <string> data;

            switch (request.Type)
            {
            case AutocompleteRequestType.PackageIds:
                data = results.Select(x => x.Document.PackageId).ToList();
                break;

            case AutocompleteRequestType.PackageVersions:
                if (result.Count > 1 || results.Count > 1)
                {
                    throw new ArgumentException(
                              "Package version autocomplete queries should have a single document result",
                              nameof(result));
                }

                data = results.SelectMany(x => x.Document.Versions).ToList();
                break;

            default:
                throw new InvalidOperationException($"Unknown autocomplete request type '{request.Type}'");
            }

            return(new AutocompleteResponse
            {
                Context = GetAutocompleteContext(),
                TotalHits = result.Count.Value,
                Data = data,
                Debug = DebugInformation.CreateFromSearchOrNull(
                    request,
                    _options.Value.SearchIndexName,
                    parameters,
                    text,
                    result,
                    duration,
                    auxiliaryFilesMetadata: null),
            });
        }
Esempio n. 4
0
        private V2SearchResponse ToResponse <T>(
            V2SearchRequest request,
            SearchParameters parameters,
            string text,
            string indexName,
            DocumentSearchResult <T> result,
            TimeSpan duration,
            Func <T, V2SearchPackage> toPackage)
            where T : class
        {
            var results = result.Results;

            result.Results = null;

            if (request.CountOnly)
            {
                return(new V2SearchResponse
                {
                    TotalHits = result.Count.Value,
                    Debug = DebugInformation.CreateFromSearchOrNull(
                        request,
                        indexName,
                        parameters,
                        text,
                        result,
                        duration,
                        AuxiliaryData.Metadata),
                });
            }
            else
            {
                var resultData = results.Select(x =>
                {
                    var package   = toPackage(x.Document);
                    package.Debug = request.ShowDebug ? x : null;
                    return(package);
                });

                // The real sorting happens in Azure Search. However, we do another round of sorting here since the sorting
                // on Azure Search index's downloads may produce different results from what customers see on
                // the Gallery (which uses Auxiliary file's download count)
                if (request.SortBy == V2SortBy.TotalDownloadsAsc)
                {
                    resultData = resultData.OrderBy(x => x.PackageRegistration.DownloadCount).ThenBy(x => x.Created);
                }
                else if (request.SortBy == V2SortBy.TotalDownloadsDesc)
                {
                    resultData = resultData.OrderByDescending(x => x.PackageRegistration.DownloadCount).ThenByDescending(x => x.Created);
                }

                return(new V2SearchResponse
                {
                    TotalHits = result.Count.Value,
                    Data = resultData
                           .ToList(),
                    Debug = DebugInformation.CreateFromSearchOrNull(
                        request,
                        indexName,
                        parameters,
                        text,
                        result,
                        duration,
                        AuxiliaryData.Metadata),
                });
            }
        }