Beispiel #1
0
        public SearchResultPagedList <ProductOverviewModel> SearchProduct(
            int pageIndex                      = 0,
            int pageSize                       = int.MaxValue,
            IList <int> categoryIds            = null,
            IList <int> brandIds               = null,
            IList <int> productIds             = null,
            bool?enabled                       = default(bool?),
            bool?discontinued                  = default(bool?),
            bool?visibleIndividually           = default(bool?),
            bool includeDiscontinuedButInStock = false,
            decimal?priceMin                   = default(decimal?),
            decimal?priceMax                   = default(decimal?),
            string keywords                    = null,
            bool searchDescriptions            = false,
            bool useFullTextSearch             = false,
            FulltextSearchMode fullTextMode    = FulltextSearchMode.Or,
            bool applySearchAnalysis           = false,
            ProductSortingType orderBy         = ProductSortingType.Position,
            bool applyKeywordSuggestion        = false,
            bool displaySearchAnalysis         = false)
        {
            string suggestedKeywords = null;

            if (!string.IsNullOrEmpty(keywords))
            {
                // First, we check for search term
                var searchTerm = GetSearchTermByQuery(keywords);
                if (searchTerm != null)
                {
                    return(new SearchResultPagedList <ProductOverviewModel>(searchTerm));
                }

                // Then, we check for product name
                var product = _productService.GetActiveProductOverviewModelByName(keywords);
                if (product != null)
                {
                    return(new SearchResultPagedList <ProductOverviewModel>(new List <ProductOverviewModel> {
                        product
                    }));
                }

                suggestedKeywords = keywords;

                if (applyKeywordSuggestion == true && !string.IsNullOrWhiteSpace(keywords))
                {
                    // We check if it's a brand name
                    var   suggestionBrands = _spellCheckerService.Suggest(keywords);
                    Brand brand            = null;

                    if (suggestionBrands.Count > 0)
                    {
                        brand = _brandService.GetBrandByName(suggestionBrands[0]);
                        if (brand != null)
                        {
                            suggestedKeywords = suggestionBrands[0];
                            if (brandIds == null)
                            {
                                brandIds = new List <int> {
                                    brand.Id
                                }
                            }
                            ;
                            else
                            {
                                brandIds.Add(brand.Id);
                            }
                        }
                    }

                    if (brand == null)
                    {
                        var words = keywords.Split(' ');

                        for (int i = 0; i < words.Length; i++)
                        {
                            bool correct = _spellCheckerService.Spell(words[i]);

                            if (!correct)
                            {
                                var suggestions = _spellCheckerService.Suggest(words[i]);
                                if (suggestions.Count > 0)
                                {
                                    words[i] = suggestions[0];                        //Choose the first one.
                                }
                            }
                        }
                        suggestedKeywords = string.Join(" ", words);
                    }
                }
            }

            var list = _productService.GetPagedProductOverviewModel(
                pageIndex: pageIndex,
                pageSize: pageSize,
                categoryIds: categoryIds,
                brandIds: brandIds,
                productIds: productIds,
                enabled: enabled,
                discontinued: discontinued,
                visibleIndividually: visibleIndividually,
                includeDiscontinuedButInStock: includeDiscontinuedButInStock,
                priceMin: priceMin,
                priceMax: priceMax,
                //keywords: applyKeywordSuggestion ? suggestedKeywords : keywords,
                keywords: keywords,
                searchDescriptions: searchDescriptions,
                useFullTextSearch: useFullTextSearch,
                fullTextMode: fullTextMode,
                applySearchAnalysis: applySearchAnalysis,
                orderBy: orderBy,
                displaySearchAnalysis: displaySearchAnalysis);

            if (string.IsNullOrEmpty(suggestedKeywords) == false && suggestedKeywords.ToLower().CompareTo(keywords.ToLower()) == 0)
            {
                suggestedKeywords = null;
            }

            var result = new SearchResultPagedList <ProductOverviewModel>(
                list.Items, list.PageIndex, list.PageSize, list.TotalCount, suggestedKeywords,
                list.MaxPriceFilterByKeyword, list.MinPriceFilterByKeyword);

            return(result);
        }