/// <summary> /// Gets all the products under a specific category. /// </summary> /// <param name="categoryId">The category name.</param> /// <param name="searchOptions">The paging options for this query.</param> /// <returns>A list of child products.</returns> public static Search.SearchResponse GetCategoryProducts(ID categoryId, Search.Models.CommerceSearchOptions searchOptions) { var searchManager = ContextTypeLoader.CreateInstance <ICatalogSearchManager>(); var searchIndex = searchManager.GetIndex(); using (var context = searchIndex.CreateSearchContext()) { var searchResults = context.GetQueryable <CommerceProductSearchResultItem>() .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product) .Where(item => item.Language == CurrentLanguageName) .Where(item => item.CommerceAncestorIds.Contains(categoryId)) .Select(p => new CommerceProductSearchResultItem() { ItemId = p.ItemId, Uri = p.Uri }); searchResults = searchManager.AddSearchOptionsToQuery(searchResults, searchOptions.ConnectSearchOptions); var results = searchResults.GetResults(); var response = Search.SearchResponse.CreateFromSearchResultsItems(searchOptions, results); return(response); } }
/// <summary> /// Searches for site content items based on keyword. /// </summary> /// <param name="keyword">The keyword to search for.</param> /// <param name="searchOptions">The paging options for this query.</param> /// <returns>A list of child products. </returns> /// TODO:remove unused public static Search.SearchResponse SearchSiteByKeyword(string keyword, Search.Models.CommerceSearchOptions searchOptions) { const string IndexNameFormat = "sitecore_{0}_index"; string indexName = string.Format( System.Globalization.CultureInfo.InvariantCulture, IndexNameFormat, Sitecore.Context.Database.Name); var searchIndex = ContentSearchManager.GetIndex(indexName); using (var context = searchIndex.CreateSearchContext()) { var searchResults = context.GetQueryable <SearchResultItem>(); searchResults = searchResults.Where(item => item.Path.StartsWith(Sitecore.Context.Site.ContentStartPath)); searchResults = searchResults.Where(item => item.Language == CurrentLanguageName); searchResults = searchResults.Where(GetContentExpression(keyword)); searchResults = searchResults.Page(searchOptions.StartPageIndex, searchOptions.NumberOfItemsToReturn); var results = searchResults.GetResults(); var response = Search.SearchResponse.CreateFromSearchResultsItems(searchOptions, results); return(response); } }
/// <summary> /// Returns the navigation categories based on a root navigation ID identified by a Data Source string. /// </summary> /// <param name="navigationDataSource">A Sitecore Item ID or query that identifies the root navigation ID.</param> /// <param name="searchOptions">The paging options for this query.</param> /// <returns>A list of category items.</returns> /// TODO:remove unused public static Search.SearchResponse GetNavigationCategories(string navigationDataSource, Search.Models.CommerceSearchOptions searchOptions) { ID navigationId; var searchManager = ContextTypeLoader.CreateInstance <ICatalogSearchManager>(); var searchIndex = searchManager.GetIndex(); if (navigationDataSource.IsGuid()) { navigationId = ID.Parse(navigationDataSource); } else { using (var context = searchIndex.CreateSearchContext()) { var query = LinqHelper.CreateQuery <SitecoreUISearchResultItem>(context, SearchStringModel.ParseDatasourceString(navigationDataSource)) .Select(result => result.GetItem().ID); if (query.Any()) { navigationId = query.First(); } else { return(new Search.SearchResponse()); } } } using (var context = searchIndex.CreateSearchContext()) { var searchResults = context.GetQueryable <CommerceBaseCatalogSearchResultItem>() .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Category) .Where(item => item.Language == CurrentLanguageName) .Where(item => item.CommerceAncestorIds.Contains(navigationId)) .Select(p => new CommerceBaseCatalogSearchResultItem() { ItemId = p.ItemId, Uri = p.Uri }); searchResults = searchManager.AddSearchOptionsToQuery(searchResults, searchOptions.ConnectSearchOptions); var results = searchResults.GetResults(); var response = Search.SearchResponse.CreateFromSearchResultsItems(searchOptions, results); return(response); } }
/// <summary> /// Gets all the products under a specific category /// </summary> /// <param name="categoryId">The parent category id</param> /// <param name="searchOptions">The paging options for this query</param> /// <returns>A list of child products</returns> public static CategorySearchResults GetCategoryChildCategories(ID categoryId, Search.Models.CommerceSearchOptions searchOptions) { var childCategoryList = new List <Item>(); var searchManager = ContextTypeLoader.CreateInstance <ICatalogSearchManager>(); var searchIndex = searchManager.GetIndex(); using (var context = searchIndex.CreateSearchContext()) { var searchResults = context.GetQueryable <CommerceBaseCatalogSearchResultItem>() .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Category) .Where(item => item.Language == CurrentLanguageName) .Where(item => item.ItemId == categoryId) .Select(p => p); var list = searchResults.ToList(); if (list.Count > 0) { if (list[0].Fields.ContainsKey(StorefrontConstants.KnownIndexFields.ChildCategoriesSequence)) { var childCategoryDelimitedString = list[0][StorefrontConstants.KnownIndexFields.ChildCategoriesSequence]; string[] categoryIdArray = childCategoryDelimitedString.Split('|'); foreach (var childCategoryId in categoryIdArray) { var childCategoryItem = Sitecore.Context.Database.GetItem(ID.Parse(childCategoryId)); if (childCategoryItem != null) { childCategoryList.Add(childCategoryItem); } } } } } return(new CategorySearchResults(childCategoryList, childCategoryList.Count, 1, 1, new List <FacetCategory>())); }
/// <summary> /// Searches for catalog items based on keyword. /// </summary> /// <param name="keyword">The keyword to search for.</param> /// <param name="catalogName">The name of the catalog containing the keyword.</param> /// <param name="searchOptions">The paging options for this query.</param> /// <returns>A list of child products.</returns> public static Search.SearchResponse SearchCatalogItemsByKeyword(string keyword, string catalogName, Search.Models.CommerceSearchOptions searchOptions) { Sitecore.Diagnostics.Assert.ArgumentNotNullOrEmpty(catalogName, "catalogName"); var searchManager = ContextTypeLoader.CreateInstance <ICatalogSearchManager>(); var searchIndex = searchManager.GetIndex(catalogName); using (var context = searchIndex.CreateSearchContext()) { var searchResults = context.GetQueryable <CommerceProductSearchResultItem>() .Where(item => item.Name.Equals(keyword) || item["_displayname"].Equals(keyword) || item.Content.Contains(keyword)) .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product || item.CommerceSearchItemType == CommerceSearchResultItemType.Category) .Where(item => item.CatalogName == catalogName) .Where(item => item.Language == CurrentLanguageName) .Select(p => new CommerceProductSearchResultItem() { ItemId = p.ItemId, Uri = p.Uri }); searchResults = searchManager.AddSearchOptionsToQuery(searchResults, searchOptions.ConnectSearchOptions); var results = searchResults.GetResults(); var response = Search.SearchResponse.CreateFromSearchResultsItems(searchOptions, results); return(response); } }
/// <summary> /// Executes a search 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> /// <param name="searchOptions">The search options.</param> /// <returns>A list of catalog items.</returns> public static Search.SearchResponse SearchCatalogItems(string defaultBucketQuery, string persistentBucketFilter, Search.Models.CommerceSearchOptions searchOptions) { var searchManager = ContextTypeLoader.CreateInstance <ICatalogSearchManager>(); 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 <SitecoreUISearchResultItem>(context, searchStringModel) .Where(item => item.Language == SearchNavigation.CurrentLanguageName); query = searchManager.AddSearchOptionsToQuery(query, searchOptions.ConnectSearchOptions); var results = query.GetResults(); var response = Search.SearchResponse.CreateFromUISearchResultsItems(searchOptions, results); return(response); } }