Set Up Personalized Boosting
Personalization can be a powerful benefit to your users. You can set up boost lists per user or user group. Applying a boost costs the same however many documents it covers; building a very large per-user list has its own cost, since each item in it is a filter.
Scenario: An online grocery store with two active users.
- User1 regularly buys broccoli and tomatoes
- User2 often buys cheese, especially discounted items
We boost on a single item if a user has bought it once. If they've purchased more than 10 times, we increase the boost strength. We also boost all discounted products globally.
// Set up global boost for all discounted items
Filter discountFilter = engine.CreateValueFilter("discounted", true, out _)!;
Boost globalDiscountBoost = engine.CreateBoost(discountFilter, BoostStrength.Low);
// PERSONALISATION PER USER
var userBoosts = new List<Boost>();
// Lists of purchased items by item number
List<long> highPurchases = new List<long>{2, 3}; // Broccoli and cherry tomatoes
List<long> lowPurchases = new List<long>{1}; // Strawberry jam
// Build a combined filter for each boost level by OR-ing per-item filters.
// item_number holds numbers, so each one is a range with equal limits rather than a
// value filter: a value filter compares text, and the engine refuses it on a numeric
// field. Equal limits compare as numbers and read the field's index.
Filter? highPurchaseFilter = null;
Filter? lowPurchaseFilter = null;
foreach (long itemNumber in highPurchases)
{
Filter fil = engine.CreateRangeFilter("item_number", itemNumber, itemNumber, out _)!;
highPurchaseFilter = highPurchaseFilter is null ? fil : highPurchaseFilter | fil;
}
foreach (long itemNumber in lowPurchases)
{
Filter fil = engine.CreateRangeFilter("item_number", itemNumber, itemNumber, out _)!;
lowPurchaseFilter = lowPurchaseFilter is null ? fil : lowPurchaseFilter | fil;
}
userBoosts.Add(engine.CreateBoost(highPurchaseFilter!, BoostStrength.Med));
userBoosts.Add(engine.CreateBoost(lowPurchaseFilter!, BoostStrength.Low));
userBoosts.Add(globalDiscountBoost);
// Use in query
query.Boosts = userBoosts.ToArray();
query.EnableBoost = true;