Loading...
Area: Optimizely Commerce
Applies to versions: 10 and higher
Other versions:

Catalog product search

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

The catalog search API provides the functionality for finding Optimizely 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. Optimizely Commerce includes the following search providers:

Catalog Search API is used mostly on the front-end and provides advanced functionality for website visitors.

Implementation examples

The following examples show how to implement search features.

Simple catalog entry search

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

Fuzzy search results

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

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();

To learn more about fuzzy search, see Fuzzy Query and Fuzziness. Note that Fuzziness is based on Levenshtein distance.

Catalog entry search with paging

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.

[New in Commerce 14]

To change caching on the public site, use CatalogCacheOptions class, see Caching.

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading