Segmentation
Segmentation allows different parts of the search result to be ordered differently. Some e-commerce sites prefer to show new products first in the default view, while others might use top picks for the user based on their preferences (top low cost, top high quality, golden middle, etc.). This is where segmentation comes in.
Without segmentation support, the client would have to run multiple queries and merge the results on the client side, with each additional query excluding the results of the previous one. This is not only inefficient, but also prone to error. With segmentation, developers can simply define rules for ordering different segments, limit the size of each segment, and let the server do the heavy lifting.
Segments
- requireConstraint:segment+
one or more constraints that specify rules for each segment
Segments constraint container allows you to define multiple segments with limited size and different ordering rules. Entities listed in previous segments are excluded from the next segments. Each segment lists all entities that provide data for a particular order (see Note for more details) until the limit is reached. The order of the segments is important because it determines the order of the segments in the final result.
What is meant by "provide data for specific ordering"?
Each segment allows you to define an additional filtering constraint that is applied to the query result to select only a subset of entities that could be included in that particular segment.
Let's look at an example. Let's say we want to show first two newly added products, then the top-selling product with price over 500€, then the top-selling product with price under 500€, then the rest of the products that are currently in stock, and finally the rest of the products that we need to order from our suppliers. We can define the segments as follows:
As you can see, the first two positions are occupied by the newly added products. The third position is occupied by the top-selling product with a price over 500€. The fourth position is occupied by the best-selling product with a price below 500€. The fifth and subsequent positions are occupied by products that are currently in stock. If we run out of products in stock, the remaining positions are occupied by the products we need to order from our suppliers.
Segment
- filterConstraint:entityHaving?
an optional filtering constraint that takes the query result and applies additional filtering to it to select only the entities that could be included in this particular segment
- orderConstraint:orderBy
an ordering constraint that specifies how the entities in this segment should be ordered
- requireConstraint:limit?
an optional constraint that specifies the maximum number of entities that should be included in this segment