Beispiel #1
0
        public virtual async Task <IHttpActionResult> AutoComplete(AutoCompleteSearchViewModel request, int limit = MAXIMUM_AUTOCOMPLETE_RESULT)
        {
            var originalSearchTerms = request.Query.Trim();
            var searchTerms         = SearchTermsTransformationProvider.TransformSearchTerm(originalSearchTerms, ComposerContext.CultureInfo.Name);

            var searchCriteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(searchTerms, RequestUtils.GetBaseUrl(Request).ToString(), false).ConfigureAwait(false);

            searchCriteria.NumberOfItemsPerPage = limit;

            var searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false);

            if (searchResultsViewModel.ProductSearchResults?.TotalCount == 0 && originalSearchTerms != searchTerms)
            {
                searchCriteria.Keywords = originalSearchTerms;
                searchResultsViewModel  = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false);
            }

            var vm = new AutoCompleteViewModel()
            {
                Suggestions = new List <ProductSearchViewModel>()
            };

            if (searchResultsViewModel.ProductSearchResults?.SearchResults?.Count > 0)
            {
                vm.Suggestions = searchResultsViewModel.ProductSearchResults.SearchResults.Take(limit)
                                 .Select(p => { p.SearchTerm = searchTerms; return(p); })
                                 .ToList();
            }

            return(Ok(vm));
        }
Beispiel #2
0
        public virtual async Task <IHttpActionResult> SuggestCategories(AutoCompleteSearchViewModel request, int limit = MAXIMUM_CATEGORIES_SUGGESTIONS)
        {
            string language   = ComposerContext.CultureInfo.Name;
            string searchTerm = request.Query.Trim().ToLower();

            List <Category> categories = await SearchViewService.GetAllCategories();

            List <Facet> categoryCounts = await SearchViewService.GetCategoryProductCounts(language);

            List <CategorySuggestionViewModel> categorySuggestionList = new List <CategorySuggestionViewModel>();

            foreach (var category in categories)
            {
                if (!category.DisplayName.TryGetValue(language, out string displayName))
                {
                    continue;
                }

                // Find the parents of the category
                List <Category> parents     = new List <Category>();
                Category        currentNode = category;
                while (!string.IsNullOrWhiteSpace(currentNode.PrimaryParentCategoryId) && currentNode.PrimaryParentCategoryId != RootCategoryId)
                {
                    Category parent = categories.Single((cat) => cat.Id == currentNode.PrimaryParentCategoryId);
                    parents.Add(parent);
                    currentNode = parent;
                }
                parents.Reverse();

                FacetValue categoryCount = categoryCounts
                                           .Where((facet) => int.TryParse(CategoryFieldName.Match(facet.FieldName).Groups[1].Value, out int n) && parents.Count == n - 1)
                                           .FirstOrDefault()?
                                           .Values
                                           .Where((facetValue) => facetValue.Value == category.DisplayName[language]).SingleOrDefault();

                if (categoryCount != null)
                {
                    categorySuggestionList.Add(new CategorySuggestionViewModel
                    {
                        DisplayName = displayName,
                        Parents     = parents.Select((parent) => parent.DisplayName[language]).ToList(),
                        Quantity    = categoryCount.Count
                    });
                }
            }
            ;

            List <CategorySuggestionViewModel> finalSuggestions = categorySuggestionList
                                                                  .OrderByDescending((category) => category.Quantity)
                                                                  .Where((suggestion) => suggestion.DisplayName.ToLower().Contains(searchTerm))
                                                                  .Take(limit)
                                                                  .ToList();

            CategorySuggestionsViewModel vm = new CategorySuggestionsViewModel
            {
                Suggestions = finalSuggestions
            };

            return(Ok(vm));
        }
Beispiel #3
0
        public virtual async Task <IHttpActionResult> SuggestTerms(AutoCompleteSearchViewModel request, int limit = MAXIMUM_SEARCH_TERMS_SUGGESTIONS)
        {
            string searchTerm = request.Query.Trim().ToLower();

            List <string> suggestedTerms = await AutocompleteProvider.GetSearchSuggestedTerms(ComposerContext.CultureInfo, searchTerm).ConfigureAwait(false);

            SearchTermsSuggestionsViewModel vm = new SearchTermsSuggestionsViewModel()
            {
                Suggestions = suggestedTerms
                              .OrderBy(term => term)
                              .Select(term => new SearchTermsSuggestionViewModel {
                    DisplayName = term
                })
                              .Take(limit).ToList()
            };

            return(Ok(vm));
        }
Beispiel #4
0
        public virtual async Task <IHttpActionResult> AutoComplete(AutoCompleteSearchViewModel request, int limit = MAXIMUM_AUTOCOMPLETE_RESULT)
        {
            var originalSearchTerms = request.Query.Trim();
            var searchTerms         = SearchTermsTransformationProvider.TransformSearchTerm(originalSearchTerms, ComposerContext.CultureInfo.Name);;

            var searchCriteria = new SearchCriteria
            {
                Keywords             = searchTerms,
                NumberOfItemsPerPage = limit,
                IncludeFacets        = false,
                StartingIndex        = 0,
                SortBy               = "score",
                SortDirection        = "desc",
                Page                 = 1,
                BaseUrl              = RequestUtils.GetBaseUrl(Request).ToString(),
                Scope                = ComposerContext.Scope,
                CultureInfo          = ComposerContext.CultureInfo,
                InventoryLocationIds = await InventoryLocationProvider.GetInventoryLocationIdsForSearchAsync().ConfigureAwait(false),
            };

            var searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false);

            if (searchResultsViewModel.ProductSearchResults?.TotalCount == 0 && originalSearchTerms != searchTerms)
            {
                searchCriteria.Keywords = originalSearchTerms;
                searchResultsViewModel  = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false);
            }

            var vm = new AutoCompleteViewModel()
            {
                Suggestions = new List <ProductSearchViewModel>()
            };

            if (searchResultsViewModel.ProductSearchResults?.SearchResults?.Count > 0)
            {
                vm.Suggestions = searchResultsViewModel.ProductSearchResults.SearchResults.Take(limit)
                                 .Select(p => { p.SearchTerm = searchTerms; return(p); })
                                 .ToList();
            }

            return(Ok(vm));
        }
Beispiel #5
0
        public virtual async Task <IHttpActionResult> SuggestBrands(AutoCompleteSearchViewModel request, int limit = MAXIMUM_BRAND_SUGGESTIONS)
        {
            string searchTerm = request.Query.Trim().ToLower();

            List <Facet> facets = await SearchViewService.GetBrandProductCounts(ComposerContext.CultureInfo.Name).ConfigureAwait(false);

            List <BrandSuggestionViewModel> brandList = facets.Single().Values.Select(facetValue => new BrandSuggestionViewModel
            {
                DisplayName = facetValue.DisplayName
            }).ToList();

            BrandSuggestionsViewModel vm = new BrandSuggestionsViewModel()
            {
                Suggestions = brandList
                              .Where((suggestion) => suggestion.DisplayName.ToLower().Contains(searchTerm))
                              .OrderBy(x => x.DisplayName)
                              .Take(limit).ToList()
            };

            return(Ok(vm));
        }