Applying refinements, sorting rules and inventory filters to a ProductSearchModel
Key Takeaway
On a ProductSearchModel, setRefinementValues() replaces an attribute's existing refinement values, while addRefinementValues() adds to them. To refine by several values, pass them as one pipe-separated string, such as 'Cotton|Linen', and results match any of them.
ProductSearchModel (dw.catalog.ProductSearchModel) is the B2C Commerce Script API class that runs a product search and holds its result and refinement options. You set criteria on it, such as a category, refinements, a sorting rule and inventory lists. Then you call search() and read the hits and refinements. Merchants define the refinement definitions and sorting rules in Business Manager, and your code applies them to each search.
How to run a product search
- Create the model with
new ProductSearchModel(). - Set criteria, such as
setSearchPhrase()orsetCategoryID(). - Call
search(). - Read the results with
getProductSearchHits()and the refinement options withgetRefinements().
search() returns a SearchStatus. A status of SUCCESSFUL or LIMITED means the search worked. Calling search() on a model with no search term and no filter returns EMPTY_QUERY.
getProductSearchHits() can include hits for products that were removed or taken offline since the last index update. Check each hit before you call ProductSearchHit.getProduct(). getProducts() skips those products, but getProducts() is deprecated because loading a Product for each result is very expensive.