How to set Lucene boosting on Predictive Search

Jacob Mallach asked on March 9, 2018 17:08

Hello,

We have the following ascx tag in place for predictive search.

<cms:SearchBox ImageUrl="/site/common/img/svg/mag_glass.svg" PredictiveSearchEnabled="true" PredictiveSearchIndexes="AllPages;EventsCustomTable" runat="server" SearchLabelCssClass="site-search__label" SearchLabelText="Site Search" SearchMode="anyword" SearchResultsPageUrl="/search-results" SearchTextboxCssClass="site-search__input" ShowImageButton="true" ShowSearchLabel="true" SearchTextRequired="false"/>

I'd like to add the following code to alter the predictive search results with Lucene boosting.

herotitle: ({% QueryString.searchtext %})^2

I've tried using the PredictiveSearchCondition public prop.

PredictiveSearchCondition="herotitle:({QueryString.searchtext})^2"

However, the event log throws a syntax error regarding the "{" curly brackets.

How can I make this happen?

Appreciate the help.

Recent Answers


Peter Mogilnitski answered on March 9, 2018 21:38 (last edited on March 24, 2018 12:16)

It should be: (herotitle:{% QueryString["searchtext"]%})^2, but this is a bit dangerous because you dont know a user input, you probably need to filter noise words etc.

0 votesVote for this answer Mark as a Correct answer

Jacob Mallach answered on March 9, 2018 22:09

Hi Peter,

Gave this a try but it breaks my component with an error stating "The server tag is not well formed".

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on March 10, 2018 16:24 (last edited on March 24, 2018 12:16)

Hi Jacob,

I don’t know if you made a typo when you asked this question, but in your third code sample you dont have ‘%’ sign when you used macro syntax. It should be like this: PredictiveSearchCondition="herotitle:({%QueryString.searchtext%})^2"

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 12, 2018 15:06 (last edited on March 24, 2018 12:16)

Take a look at FAQ The syntax should be: DocumentName:({% QueryString.searchtext %}

0 votesVote for this answer Mark as a Correct answer

Jacob Mallach answered on March 12, 2018 16:25 (last edited on March 12, 2018 16:30)

@peter

I'm able to successfully boost results while using static text i.e., 'batman is a hero'

+herotitle: (batman is a hero)^2

However, your conditional breaks my component with the server error 'The server tag is not well formed'.

@dragoljub - Thank you for catching the syntax error. It was not my intention to remove. Applying that back did not resolve the issue.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 12, 2018 16:46 (last edited on March 24, 2018 12:16)

You can use quote &quot; or single quote: here are some examples.

PredictiveSearchCondition='{%String.IsNullOrEmpty(QueryString.searchtext)? "" : "herotitle:(" + QueryString.searchtext + ")^2" %}'

0 votesVote for this answer Mark as a Correct answer

Jacob Mallach answered on March 12, 2018 19:49 (last edited on March 24, 2018 12:16)

@Peter

Thanks for the help. I managed to suppress the 'not well formed' error with your conditional. However, the boosting is still not registering.

I've verified that boosting does work with static text 'biology' in the following example.

PredictiveSearchCondition="herotitle:(biology)^2"

However, when dropping the dynamic query string macro in place.

PredictiveSearchCondition="herotitle:({%QueryString.searchtext%})^2': Encountered " "}" "} "" at line 1, column 128. Was expecting one of: "TO" ... ... ...

The search text 'biology' definitely exists in my index file. It also appears that QueryString.searchtext is indeed working. What's a dev to do?

Appreciate the help.

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on March 12, 2018 19:59 (last edited on March 24, 2018 12:16)

Hi Jacob,

It looks like you are using aspx transformation with K# macro expression and because of that macro is not resolved at all (you pass '{%QueryString.searchtext%}' as value for boosting which contains forbidden characters). Can you try something like this: PredictiveSearchCondition="herotitle:(<%# Request.QueryString["searchtext"] %>)^2

If this works for you, then you should think about custom macro, or some additional checks before you directly pass query string to predictive search.

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Jacob Mallach answered on March 12, 2018 20:22 (last edited on March 12, 2018 20:24)

@Dragoljub - Thanks for the feedback. Your snippet breaks my component. However, your explanation has definitely helped me to understand the issue. +1

1 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 12, 2018 22:33 (last edited on March 12, 2018 22:40)

Ok. The problem is with the smart search box webpart i.e. ~/CMSWebParts/SmartSearch/SearchBox.ascx. PredictiveSearchCondition property does not resolve macro . It is missing ContextResolver.ResolveMacroExpression(...) or LocalResolver.ResolveMacros(..) or similar. The current code is:

public string PredictiveSearchCondition
{
    get
    {
        return ValidationHelper.GetString(GetValue("PredictiveSearchCondition"), null);
    }
    set
    {
        SetValue("PredictiveSearchCondition", value);
    }
}

It should be for example:

 get
        {
            return LocalResolver.ResolveMacros(ValidationHelper.GetString(GetValue("PredictiveSearchCondition"), null));
        }

So you can clone this control and create SearchBoxCustom which would be a copy of existing one but you add macro resolver yourself. Or you can do it in pure ASP way if you are not planning not to use macros:

PredictiveSearchCondition='<%# String.IsNullOrEmpty(Request.QueryString["searchtext"])? "": "herotitle:(Request.QueryString[\"searchtext\"])" %>

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.