The catalog search API provides the functionality for finding Episerver Commerce products via two methods: search providers and ICatalogSystem.
Search providers
Catalog Search API uses an index-based search engine to provide powerful search functionalities. Episerver Commerce includes the following search providers:
LuceneSearchProvider (the default search provider for the EPiServer.Commerce.Core package)
The following example shows how to return catalog entries from the search API and to bind directly to Web Controls like grid.
// Get catalog lists
CatalogDto catalogs = system.GetCatalogDto();
// Create Entry Criteria
CatalogEntrySearchCriteria criteria = new CatalogEntrySearchCriteria();
// Bind default catalogs if none found
if (criteria.CatalogNames.Count == 0)
{
if (catalogs.Catalog.Count > 0)
{
foreach (CatalogDto.CatalogRow row in catalogs.Catalog)
{
if (row.IsActive &&
row.StartDate <= FrameworkContext.Current.CurrentDateTime &&
row.EndDate >= FrameworkContext.Current.CurrentDateTime)
{
criteria.CatalogNames.Add(row.Name);
}
}
}
}
// Define phrase we want to search
criteria.SearchPhrase = "canon";
// Create a manager
SearchManager manager = new SearchManager(AppContext.Current.ApplicationName);
SearchResults results = null;
// Define sort parameter
criteria.Sort = new SearchSort("DisplayName");
// Perform search
results = manager.Search(criteria);
Assert.IsTrue(results.TotalCount > 0, "No hits were found in Lucene index.");
// Get IDs we need
int[] resultIndexes = results.GetIntResults(0, 10 + 5);
// padding added to accomodate entries that might have been deleted since last indexing
// Retrieve actual entry objects, with no caching
Entries entries = CatalogContext.Current.GetCatalogEntries(resultIndexes, false, new TimeSpan(), new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull));
entries.TotalResults = results.TotalCount;
Assert.IsTrue(entries.TotalResults > 0, "No entries were returned from the database.");
ICatalogSystem methods
ICatalogSystem provides several methods for finding nodes and entries with specific criteria. The methods use SQL internally and might not be as fast or powerful as search providers. The following example shows browsing catalogs and categories.
// Get catalog lists
CatalogDto catalogs = system.GetCatalogDto();
foreach (CatalogDto.CatalogRow catalog in catalogs.Catalog)
{
string catalogName = catalog.Name;
// Get Catalog Nodes
CatalogNodeDto nodes = system.GetCatalogNodesDto(catalogName);
foreach (CatalogNodeDto.CatalogNodeRow node in nodes.CatalogNode)
{
CatalogSearchParameters pars = new CatalogSearchParameters();
CatalogSearchOptions options = new CatalogSearchOptions();
options.CacheResults = true;
pars.CatalogNames.Add(catalogName);
pars.CatalogNodes.Add(node.Code);
var entries = CatalogContext.Current.FindItemsDto(pars,
options);
}
}
Caching
The search API caching is flexible and lets the developer control how search handles caching. You can specify whether results are cached (CacheResults (bool) property), and if so, the amount of time for which they are cached (CacheTimeout (TimeSpan) property). You generally want to cache simple search requests, like browsing major categories; it benefits the site performance because the requests are the same for a large audience. On the other hand, you might not want to cache keyword searches, because those are unique to a user and have a smaller chance of benefiting from caching.
To change caching on the public site, go to the Configs\ecf.catalog.config file and change values in the <Cache/> element.
Do you find this information helpful? Please log in to provide feedback.