Cert++
Close

Search index queries versus catalog database iteration in storefront code

Key Takeaway

On high-traffic storefront pages, let the search index do the work. Query with ProductSearchModel, loop over the ProductSearchHit objects, and read price ranges from getMinPrice() and getMaxPrice(). Don't loop over products, their variants and their price models.

Many storefront pages list several products at once, such as category pages and search result pages. In B2C Commerce storefront code, the scalable way to find, filter and sort those products is to query the search index through dw.catalog.ProductSearchModel. The search does the matching, filtering and sorting itself. It then hands back a sorted, grouped list of ProductSearchHit objects. Loading a full Product for every result slows search down and goes against best practice. Looping over results in script to filter them does the same.

What to replace on high-traffic pages

Salesforce recommends replacing database-intensive or inefficient APIs with index-friendly APIs. Check the code of your most-visited pages for database-intensive APIs, such as Product.getVariationModel(). Index-friendly APIs that can replace custom logic include these:

  • ProductSearchModel.search()
  • ProductSearchModel.getProductSearchHits()
  • ProductSearchHit.getMinPrice()
  • ProductSearchHit.getMaxPrice()
  • ProductSearchHit.getRepresentedProductIDs()

Don't iterate over the variations of a base product on any page where multiple base products appear, such as a search result page. Iterating there can significantly increase the number of touched business objects. To show price ranges, read them from each hit with ProductSearchHit.getMinPrice() and ProductSearchHit.getMaxPrice() instead.

ProductSearchModel.getProducts() is deprecated and should not be used. The method loads a full Product for each search result, which is extremely expensive for performance. Iterate the ProductSearchHit objects from getProductSearchHits() instead.

A home page has a "Trending now" strip with 12 master products. Each master has 15 variants. The first draft of the script gets each master's variation model and loops over every variant. For each variant, it reads the ProductPriceModel to find the lowest and highest price. Each page view loads 180 variants to print 12 price ranges. The fixed version gets the 12 products from a ProductSearchModel query and reads getMinPrice() and getMaxPrice() from each hit. The script itself never touches a variant or a price model.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given code that violates documented best practices, identify the issues and modify the code to conform with best practices including performance and scalability

0/11

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers. 796 members and growing.