Example #1
0
        public SearchResponse GetCategoryProducts(Item categoryItem, SearchOptions searchOptions)
        {
            if (categoryItem.IsDerived(Templates.Commerce.DynamicCategory.Id))
            {
                return(FindCatalogItems(categoryItem, searchOptions));
            }

            var searchIndex = CommerceSearchManager.GetIndex();

            using (var context = searchIndex.CreateSearchContext())
            {
                var searchResults = context.GetQueryable <CommerceProductSearchResultItem>()
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product)
                                    .Where(item => item.Language == Context.Language.Name)
                                    .Where(item => item.CommerceAncestorIds.Contains(categoryItem.ID))
                                    .Select(p => new CommerceProductSearchResultItem
                {
                    ItemId = p.ItemId,
                    Uri    = p.Uri
                });

                var commerceSearchOptions = searchOptions.ToCommerceSearchOptions();
                searchResults = CommerceSearchManager.AddSearchOptionsToQuery(searchResults, commerceSearchOptions);

                var results  = searchResults.GetResults();
                var response = SearchResponse.CreateFromSearchResultsItems(commerceSearchOptions, results);

                searchOptions = commerceSearchOptions.ToSearchOptions();

                return(response);
            }
        }
Example #2
0
        public SearchResponse FindCatalogItems(Item queryItem, SearchOptions searchOptions)
        {
            Assert.ArgumentNotNull(queryItem, nameof(queryItem));
            Assert.ArgumentNotNull(searchOptions, nameof(searchOptions));

            var defaultBucketQuery     = queryItem[Templates.Commerce.SearchSettings.Fields.DefaultBucketQuery];
            var persistentBucketFilter = CleanLanguageFromFilter(queryItem[Templates.Commerce.SearchSettings.Fields.PersistentBucketFilter]);

            var searchIndex = CommerceSearchManager.GetIndex();

            var defaultQuery    = defaultBucketQuery.Replace("&", ";");
            var persistentQuery = persistentBucketFilter.Replace("&", ";");
            var combinedQuery   = CombineQueries(persistentQuery, defaultQuery);

            try
            {
                var searchStringModel = SearchStringModel.ParseDatasourceString(combinedQuery);
                using (var context = searchIndex.CreateSearchContext(SearchSecurityOptions.EnableSecurityCheck))
                {
                    var query = LinqHelper.CreateQuery <SitecoreUISearchResultItem>(context, searchStringModel).Where(item => item.Language == Context.Language.Name);

                    var commerceSearchOptions = searchOptions.ToCommerceSearchOptions();
                    query = CommerceSearchManager.AddSearchOptionsToQuery(query, commerceSearchOptions);

                    var results  = query.GetResults();
                    var response = SearchResponse.CreateFromUISearchResultsItems(commerceSearchOptions, results);
                    searchOptions = commerceSearchOptions.ToSearchOptions();

                    return(response);
                }
            }
            catch (Exception e)
            {
                Log.Error("Could not find catalog items. Bucket query failed", e, this);
                return(null);
            }
        }