Hide menu Last updated: Oct 12 2015

This topic describes the catalog search API in EPiserver Commerce. The Catalog Search API provides the functionality for finding products in Episerver Commerce with two methods: search providers and ICatalogSystem methods.

Subtopics

Search providers

Catalog Search API uses an index-based search engine to provide powerful search functionalities. The following search providers are provided by Episerver Commerce:

  • LuceneSearchProvider (comes with EPiServer.Commerce.Core package as the default search provider)
  • SolrSearchProvider and Solr35SearchProvider
  • FindSearchProvider (come as separate packages)

Catalog Search API is used mostly on the front-end to provide more advanced functionality for website visitors.

Implementation examples

The following examples show how to implement search features.

Example: Simple catalog entry search

C#
CatalogEntrySearchCriteria criteria = new CatalogEntrySearchCriteria();
            criteria.SearchPhrase = "canon";
            SearchManager manager = new SearchManager(AppContext.Current.ApplicationName);
            SearchResults results = manager.Search(criteria);

For LuceneSearchProvider, the SearchPhrase property can contain full Lucene Syntax like: title:"The Right Way" AND text:go. 

Example: Catalog entry fuzzy search

The following example shows fuzzy search results returned, where approximate results are returned.

C#
CatalogEntrySearchCriteria criteria = new CatalogEntrySearchCriteria();
            criteria.SearchPhrase = "fanon";
            SearchManager manager = new SearchManager(AppContext.Current.ApplicationName);
            SearchResults results = manager.Search(criteria);
            if (results.TotalCount == 0)
            {
            criteria.IsFuzzySearch = true;
            criteria.FuzzyMinSimilarity = 0.7f;
            results = manager.Search(criteria);
            }
            Console.Write("Total Results: " + results.TotalCount.ToString();

Example: Catalog entry search with paging

The following example shows how to return real Catalog Entries from the search API to bind directly to Web Controls like grid.

C#
            // 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); // we add padding here 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 severals methods to find Nodes and Entries with specific criterias. Those methods use SQL internally and might not be as fast or powerful as search providers.

Example: Browsing catalogs

The following example shows browsing catalogs and categories.

C#
            // 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);
            Entries entries = CatalogContext.Current.FindItems(pars,
            options,
            new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull));
            }
            }

 

Caching

The search API has a very flexible caching mechanism that lets a developer control how the search handles caching. You can specify whether results are cached (CacheResults (bool) property) and the specific amount of time 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 specific 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 appropriate values in the <Cache/> element.

Response groups

Response groups are another way to make sure the site is performing to the maximum. When searching for entries/products in Episerver Commerce, you can specify the response groups that should be returned. The search performs best with the smallest number of response groups returned.

Example: Searching on metafield values with results displayed via the API

C#
private void testCatalogEntiresSearch()
            {
            CatalogSearchParameters parameters = new CatalogSearchParameters();
            parameters.SqlMetaWhereClause = " META.MetaFieldName = 'Facet_Color' AND META.LongString = 'Tan'";
            CatalogSearchOptions options = new CatalogSearchOptions();
            options.Classes.Add("Brands");
            options.RecordsToRetrieve = 20;
            Entries result = CatalogContext.Current.FindItems (parameters, options, new CatalogEntryResponseGroup());
            }

Comments