コード例 #1
0
        /// <summary>
        /// Executes a search in a bucket to retrieve catalog items.
        /// </summary>
        /// <param name="defaultBucketQuery">The search default bucket query value.</param>
        /// <param name="persistentBucketFilter">The search persistent bucket filter value.</param>
        /// <returns>A list of catalog items.</returns>
        public static SearchResponse SearchBucketForCatalogItems(string defaultBucketQuery, string persistentBucketFilter)
        {
            var searchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();
            var searchIndex   = searchManager.GetIndex();

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

            using (var context = searchIndex.CreateSearchContext(SearchSecurityOptions.EnableSecurityCheck))
            {
                var query = LinqHelper.CreateQuery <Sitecore.ContentSearch.SearchTypes.SitecoreUISearchResultItem>(context, searchStringModel)
                            .Where(item => item.Language == SearchNavigation.CurrentLanguageName);

                var results  = query.GetResults();
                var response = SearchResponse.CreateFromUISearchResultsItems(new CommerceSearchOptions(), results);

                return(response);
            }
        }
コード例 #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);
            }
        }