Posted on 04/11/2025 13:24:10
Ok - we need something like this in our query:
(AssortmentIDs:MY_ASSORTMENT_ID)^0 OR (BoostedAssortmentIDs:MY_ASSORTMENT_ID)^5
What you can do is to create a custom field for assortments that basically have a copy of the regular assortment field and then boos that custom field with customer specific assortment keys.
So first extend the index:
public class IndexBuilderExtenderExample : IndexBuilderExtenderBase<ProductIndexBuilder>{
public override void ExtendDocument(IndexDocument document)
{
if (document.ContainsKey("AssortmentIDs"))
{
var assortments = document["AssortmentIDs"] as string[];
if (assortments != null && assortments.Contains("MY_ASSORTMENT_ID"))
{
// Copy to a special boosted field
document.Add("BoostedAssortmentID", "MY_ASSORTMENT_ID");
}
}
}
}
And then do something like this in the notification subscriber
var userAssortmentId = "MY_ASSORTMENT_ID";
var fieldNormal = Expression.Field("AssortmentIDs", "AssortmentIDs");
var fieldBoosted = Expression.Field("BoostedAssortmentID", "BoostedAssortmentID");
var matchNormal = Expression.Equal(fieldNormal, Expression.Term(userAssortmentId));
var matchBoosted = Expression.Equal(fieldBoosted, Expression.Term(userAssortmentId));
var combined = Expression.Group(false, OperatorType.Or, new List<Expression> { matchBoosted, matchNormal });
beforeQueryArgs.Query.Expression = Expression.Group(false, OperatorType.And, new List<Expression> { combined });
// Apply boost to the boosted field only
beforeQueryArgs.Settings.Boost.Add("BoostedAssortmentID", 5f);
Of course the MY_ASSORTMENT_ID is what you find from the current user - and you have more of them maybe?
See if this is possible.
BR Nicolai