Sorting and Filtering
Sorting and Filtering
The default sort order is relevancy and it is always recommended unless the use case is very specific or if the user has specifically requested to order the results in a specific order.
Filtering is similar to faceting, it includes only products that match criteria set by the filter. Whereas facets are intended for filtering for end users, filters as presented here are intended for implementation level filtering. For example you may choose not to include products out of stock by using a filter.
When can Sorting and Filtering be Applied
Sorting and filtering can be applied on the following calls:
Search
Category listings
Related products
The examples on this page show how to add facets to a getEntitiesByAttribute
call. The same logic can be applied to search
by replacing the getEntitiesByAttribute
call with search
call and for Java and C# create the request
object as an instance of SearchRequest
instead of GetEntitiesByAttributeRequest
.
Sorting
The sort order is defined by supplying a list of hierarchical attributes along with the Order
parameter to specify ascending or descending sort order. The first item in the list specifies the primary sort order, the second specifies the secondary sort order etc.
// Category listing with sorting
var request = new GetEntitiesByAttributeRequest("Category", categoryName);
//Set the sort order of the products in the category
request.ResultsOptions.SortBy = new List<EntitySortingParameter>{
new EntitySortingParameter("Price")
{ Order = SortOrders.Asc}, // Primary sorting: Sort on attribute Price, ascending order
new EntitySortingParameter(EntitySortingParameter.Types.Popularity)
{ Order = SortOrders.Desc} // Secondary sorting: Sort on popularity, descending order
};
var response = _loop54Client.GetEntitiesByAttribute(request);
C# source code on Github: CategoryListingController.cs
Filters
Filter results based on attribute values by supplying the attribute and the value that you want to filter on.
// Category listing with filters
var request = new GetEntitiesByAttributeRequest("Category", categoryName);
//Filter the products in the category
//In this case, we only want products that have got
//the price attribute, and where the organic attribute is set to "True"
request.ResultsOptions.Filter = new AndFilterParameter(
new AttributeExistsFilterParameter("Price"),
//Because the organic attribute is stored as a string in the engine we need to filter with that type.
//If it would have been stored as a boolean we would have used bool instead.
new AttributeFilterParameter<string>("Organic", "True")
);
var response = _loop54Client.GetEntitiesByAttribute(request);
C# source code on Github: CategoryListingController.cs
Last updated