private CatalogSearchQuery NormalizeQuery(CatalogSearchQuery query) { var controllingData = WebApiCachingControllingData.Data(); query = query .BuildFacetMap(false) .Slice(query.Skip, Math.Min(query.Take, controllingData.MaxTop)); return(query); }
public async Task <IActionResult> InstantSearch(CatalogSearchQuery query) { if (!query.Term.HasValue() || query.Term.Length < _searchSettings.InstantSearchTermMinLength) { return(Content(string.Empty)); } query .BuildFacetMap(false) .Slice(0, Math.Min(16, _searchSettings.InstantSearchNumberOfProducts)) .SortBy(ProductSortingEnum.Relevance); var result = await _catalogSearchService.SearchAsync(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; }); // TODO: (mh) (core) What about scoped services? //using (_localizedEntityService.BeginScope(false)) //{ var hits = await result.GetHitsAsync(); // InstantSearch should be REALLY very fast! No time for smart caching stuff. if (result.TotalHitsCount > 0) { await _localizedEntityService.PrefetchLocalizedPropertiesAsync( nameof(Product), Services.WorkContext.WorkingLanguage.Id, hits.Select(x => x.Id).ToArray()); } // Add product hits. model.TopProducts = await _catalogHelper.MapProductSummaryModelAsync(hits, mappingSettings); // Add spell checker suggestions (if any). model.AddSpellCheckerSuggestions(result.SpellCheckerSuggestions, T, x => Url.RouteUrl("Search", new { q = x })); //} return(PartialView(model)); }
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; }); mappingSettings.MapPictures = _searchSettings.ShowProductImagesInInstantSearch; mappingSettings.ThumbnailSize = _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage; var summaryModel = _catalogHelper.MapProductSummaryModel(result.Hits, mappingSettings); // Add product hits model.TopProducts = summaryModel; // Add spell checker suggestions (if any) AddSpellCheckerSuggestionsToModel(result.SpellCheckerSuggestions, model); return(PartialView(model)); }
public async Task <IActionResult> RecentlyAddedProducts(CatalogSearchQuery query) { if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0) { return(View(ProductSummaryModel.Empty)); } query.Sorting.Clear(); query = query .BuildFacetMap(false) .SortBy(ProductSortingEnum.CreatedOn) .Slice(0, _catalogSettings.RecentlyAddedProductsNumber); var result = await _catalogSearchService.SearchAsync(query); var settings = _helper.GetBestFitProductSummaryMappingSettings(_helper.GetSearchQueryViewMode(query)); var model = await _helper.MapProductSummaryModelAsync(result, settings); model.GridColumnSpan = GridColumnSpan.Max5Cols; return(View(model)); }
public async Task <IActionResult> RecentlyAddedProductsRSS(CatalogSearchQuery query) { // TODO: (mc) find a more prominent place for the "NewProducts" link (may be in main menu?) var store = Services.StoreContext.CurrentStore; var protocol = Services.WebHelper.IsCurrentConnectionSecured() ? "https" : "http"; var selfLink = Url.RouteUrl("RecentlyAddedProductsRSS", null, protocol); var recentProductsLink = Url.RouteUrl("RecentlyAddedProducts", null, protocol); var title = $"{store.Name} - {T("RSS.RecentlyAddedProducts")}"; var feed = new SmartSyndicationFeed(new Uri(recentProductsLink), title, T("RSS.InformationAboutProducts")); feed.AddNamespaces(true); feed.Init(selfLink, Services.WorkContext.WorkingLanguage.LanguageCulture); if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0) { return(new RssActionResult { Feed = feed }); } var items = new List <SyndicationItem>(); query.Sorting.Clear(); query = query .BuildFacetMap(false) .SortBy(ProductSortingEnum.CreatedOn) .Slice(0, _catalogSettings.RecentlyAddedProductsNumber); var result = await _catalogSearchService.SearchAsync(query); var hits = await result.GetHitsAsync(); var storeUrl = store.GetHost(); // Prefetching. var fileIds = hits .Select(x => x.MainPictureId ?? 0) .Where(x => x != 0) .Distinct() .ToArray(); var files = (await Services.MediaService.GetFilesByIdsAsync(fileIds)).ToDictionarySafe(x => x.Id); foreach (var product in hits) { var productUrl = Url.RouteUrl("Product", new { SeName = await product.GetActiveSlugAsync() }, protocol); if (productUrl.HasValue()) { var content = product.GetLocalized(x => x.FullDescription).Value; if (content.HasValue()) { content = WebHelper.MakeAllUrlsAbsolute(content, Request); } var item = feed.CreateItem( product.GetLocalized(x => x.Name), product.GetLocalized(x => x.ShortDescription), productUrl, product.CreatedOnUtc, content); try { // We add only the first media file. if (files.TryGetValue(product.MainPictureId ?? 0, out var file)) { var url = Services.MediaService.GetUrl(file, _mediaSettings.ProductDetailsPictureSize, storeUrl, false); feed.AddEnclosure(item, file, url); } } catch { } items.Add(item); } } feed.Items = items; Services.DisplayControl.AnnounceRange(hits); return(new RssActionResult { Feed = feed }); }
public ActionResult RecentlyAddedProductsRss(CatalogSearchQuery query) { // TODO: (mc) find a more prominent place for the "NewProducts" link (may be in main menu?) var protocol = Services.WebHelper.IsCurrentConnectionSecured() ? "https" : "http"; var selfLink = Url.RouteUrl("RecentlyAddedProductsRSS", null, protocol); var recentProductsLink = Url.RouteUrl("RecentlyAddedProducts", null, protocol); var title = "{0} - {1}".FormatInvariant(Services.StoreContext.CurrentStore.Name, T("RSS.RecentlyAddedProducts")); var feed = new SmartSyndicationFeed(new Uri(recentProductsLink), title, T("RSS.InformationAboutProducts")); feed.AddNamespaces(true); feed.Init(selfLink, Services.WorkContext.WorkingLanguage); if (!_catalogSettings.RecentlyAddedProductsEnabled || _catalogSettings.RecentlyAddedProductsNumber <= 0) { return(new RssActionResult { Feed = feed }); } var items = new List <SyndicationItem>(); query.Sorting.Clear(); query = query .BuildFacetMap(false) .SortBy(ProductSortingEnum.CreatedOn) .Slice(0, _catalogSettings.RecentlyAddedProductsNumber); var result = _catalogSearchService.Search(query); var storeUrl = Services.StoreService.GetHost(Services.StoreContext.CurrentStore); // Prefecthing var allPictureInfos = Services.PictureService.GetPictureInfos(result.Hits); //_mediaSettings.ProductDetailsPictureSize, false, storeUrl foreach (var product in result.Hits) { string productUrl = Url.RouteUrl("Product", new { SeName = product.GetSeName() }, protocol); if (productUrl.HasValue()) { var item = feed.CreateItem( product.GetLocalized(x => x.Name), product.GetLocalized(x => x.ShortDescription), productUrl, product.CreatedOnUtc, product.FullDescription); try { // we add only the first picture var picture = Services.PictureService.GetPictureById(product.MainPictureId.GetValueOrDefault()); if (picture != null) { feed.AddEnclosure(item, picture, Services.PictureService.GetUrl(picture, _mediaSettings.ProductDetailsPictureSize, FallbackPictureType.NoFallback, storeUrl)); } } catch { } items.Add(item); } } feed.Items = items; Services.DisplayControl.AnnounceRange(result.Hits); return(new RssActionResult { Feed = feed }); }