コード例 #1
0
        public ActionResult InstantSearch(CatalogSearchQuery query)
        {
            if (string.IsNullOrWhiteSpace(query.Term) || query.Term.Length < _searchSettings.InstantSearchTermMinLength)
            {
                return(Content(string.Empty));
            }

            query
            .BuildFacetMap(false)
            .Slice(0, Math.Min(16, _searchSettings.InstantSearchNumberOfProducts))
            .SortBy(ProductSortingEnum.Relevance);

            var result = _catalogSearchService.Search(query);

            var model = new SearchResultModel(query)
            {
                SearchResult       = result,
                Term               = query.Term,
                TotalProductsCount = result.TotalHitsCount
            };

            var mappingSettings = _catalogHelper.GetBestFitProductSummaryMappingSettings(ProductSummaryViewMode.Mini, x =>
            {
                x.MapPrices            = false;
                x.MapShortDescription  = true;
                x.MapPictures          = _searchSettings.ShowProductImagesInInstantSearch;
                x.ThumbnailSize        = _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage;
                x.PrefetchTranslations = true;
                x.PrefetchUrlSlugs     = true;
            });

            using (_urlRecordService.BeginScope(false))
                using (_localizedEntityService.BeginScope(false))
                {
                    // InstantSearch should be REALLY very fast! No time for smart caching stuff.
                    if (result.Hits.Count > 0)
                    {
                        _localizedEntityService.PrefetchLocalizedProperties(
                            nameof(Product),
                            Services.WorkContext.WorkingLanguage.Id,
                            result.Hits.Select(x => x.Id).ToArray());
                    }

                    // Add product hits.
                    model.TopProducts = _catalogHelper.MapProductSummaryModel(result.Hits, mappingSettings);

                    // Add spell checker suggestions (if any).
                    model.AddSpellCheckerSuggestions(result.SpellCheckerSuggestions, T, x => Url.RouteUrl("Search", new { q = x }));
                }

            return(PartialView(model));
        }