Exemplo n.º 1
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),
                });
            }
        }