Example #1
0
        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));
        }
Example #2
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;
            });

            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)
            model.AddSpellCheckerSuggestions(result.SpellCheckerSuggestions, T, x => Url.RouteUrl("Search", new { q = x }));

            return(PartialView(model));
        }
Example #3
0
        public ActionResult Search(CatalogSearchQuery query)
        {
            var model = new SearchResultModel(query);
            CatalogSearchResult result = null;

            if (query.Term == null || query.Term.Length < _searchSettings.InstantSearchTermMinLength)
            {
                model.SearchResult = new CatalogSearchResult(query);
                model.Error        = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength);
                return(View(model));
            }

            // 'Continue shopping' URL
            _genericAttributeService.SaveAttribute(Services.WorkContext.CurrentCustomer,
                                                   SystemCustomerAttributeNames.LastContinueShoppingPage,
                                                   Services.WebHelper.GetThisPageUrl(false),
                                                   Services.StoreContext.CurrentStore.Id);

            try
            {
                result = _catalogSearchService.Search(query);
            }
            catch (Exception exception)
            {
                model.Error = exception.ToString();
                result      = new CatalogSearchResult(query);
            }

            if (result.TotalHitsCount == 0 && result.SpellCheckerSuggestions.Any())
            {
                // No matches, but spell checker made a suggestion.
                // We implicitly search again with the first suggested term.
                var oldSuggestions = result.SpellCheckerSuggestions;
                var oldTerm        = query.Term;
                query.Term = oldSuggestions[0];

                result = _catalogSearchService.Search(query);

                if (result.TotalHitsCount > 0)
                {
                    model.AttemptedTerm = oldTerm;
                    // Restore the original suggestions.
                    result.SpellCheckerSuggestions = oldSuggestions.Where(x => x != query.Term).ToArray();
                }
                else
                {
                    query.Term = oldTerm;
                }
            }

            model.SearchResult       = result;
            model.Term               = query.Term;
            model.TotalProductsCount = result.TotalHitsCount;

            var mappingSettings = _catalogHelper.GetBestFitProductSummaryMappingSettings(query.GetViewMode());
            var summaryModel    = _catalogHelper.MapProductSummaryModel(result.Hits, mappingSettings);

            // Prepare paging/sorting/mode stuff
            _catalogHelper.MapListActions(summaryModel, null, _catalogSettings.DefaultPageSizeOptions);

            // Add product hits
            model.TopProducts = summaryModel;

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

            return(View(model));
        }