Moorag
Moorag

Reputation: 93

How can i make Sitecore Content Search boost work within an Expression Tree

Im attempting to make a Sitecore search implementation that will allow Content editors to define item properties to search on and to set boost values individually against these fields.

In order to do this im building up a predicate using Expression Tree as follows:

foreach (KeyValuePair<string, float> searchResultProperty in searchResultProperties)
{
    Expression constant = Expression.Constant(term);
    ParameterExpression parameter = Expression.Parameter(typeof(FAQSearchResultItem), "s");
    Expression property = Expression.Property(parameter, typeof(FAQSearchResultItem).GetProperty(searchResultProperty.Key));
    Expression expression = Expression.Equal(property, constant);
    predicate = predicate.Or(Expression.Lambda<Func<FAQSearchResultItem, bool>>(expression, parameter)).Boost(searchResultProperty.Value);
}

return predicate;

I then intend to use this at the time when i execute the search in order to filter by whatever fields are passed in:

var query = _context
    .GetQueryable<CustomSearchResultItem>()
    .Where(predicate);

The problem i have is that applying a boost to the predicate built using the expression tree does not work.

if i do the striaghtforward:

var query = _context
                .GetQueryable<FAQSearchResultItem>();

query = query
   .Where(s => (s.Question == term).Boost(1.1f)
    || (s.WebsiteAnswer == term).Boost(1.5f));

then the expresion used by the query evaluates to :

{s => ((s.Question == "water").Boost(1.1) OrElse (s.WebsiteAnswer == "water").Boost(1.5))}

however the method that i want to use evaluates to :

{param => ((True AndAlso (False OrElse (param.Question == "water"))) AndAlso (param.Language == Context.Language.Name))}

with the boost not being applied.

How can i get the boost to be added in to the predicate generated using the expression tree?

Upvotes: 1

Views: 1116

Answers (1)

Moorag
Moorag

Reputation: 93

In the end I had to resort to using the predicate builder which appears to have inbuilt support for the boost. Its a shame as Im unable to use reflection to get the properties of the SearchResultObject but i can acheive what i was setting out to do... just with a lot more code

Upvotes: 1

Related Questions