0

We have a requirement where we want to boost results based on a value from a cookie/session. The items that contains the value from cookie in a Certain field should be boosted. Hence the rule should be something as below. "where field value contains this value from cookie"

I hope this should be done thru custom boosting condition, but not sure how the same can be done. Any help is appreciated.

2

1 Answer 1

1

This is achieved by imitating existing rules. Works as expected.

        string storeVisited = CookieHelper.GetCookie("VisitedStore");

        WhenFieldAndQueryStringMatches<RuleBoostingContext> ruleCondition = args.RuleCondition as WhenFieldAndQueryStringMatches<RuleBoostingContext>;
        string operatorId = ruleCondition.OperatorId;
        string fieldName = ruleCondition.FieldName;
        if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.IsEqualTo.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => c[fieldName].Equals(storeVisited).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.IsNotEqualTo.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => !c[fieldName].Equals(storeVisited).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.Contains.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => c[fieldName].Contains(storeVisited).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.StartsWith.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => c[fieldName].StartsWith(storeVisited).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.EndsWith.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => c[fieldName].EndsWith(storeVisited).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.IsCaseInsensitivelyEqualTo.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => c[fieldName].Equals(storeVisited, StringComparison.OrdinalIgnoreCase).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else if (operatorId.EqualsId(Sitecore.XA.Foundation.Search.Templates.IsNotCaseInsensitivelyEqualTo.Id))
            args.Predicate = (Expression<Func<ContentPage, bool>>)(c => !c[fieldName].Equals(storeVisited, StringComparison.OrdinalIgnoreCase).Boost<bool>(args.Boost) || c.Name != string.Empty);
        else
            args.Predicate = (Expression<Func<ContentPage, bool>>)(contentPage => true);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.