Using Boosting or Filtering Rules on the Tab Component Programmatically Using the Coveo for Sitecore Legacy Search UI Framework
Using Boosting or Filtering Rules on the Tab Component Programmatically Using the Coveo for Sitecore Legacy Search UI Framework
Coveo for Sitecore 4.1 (November 2018)
There’s a quicker and more intuitive procedure for Enterprise edition clients (see Rule Set Editor Boosting and Filtering Rules Reference).
This procedure is intended mostly for Free and Pro edition clients. It can help you define filters that content editors and marketers won’t have access to.
The Coveo Tab component offers a variety of rules to filter or boost your search results.
The defined rules are only applied when the tab is selected.
Contrary to the Enterprise edition of Coveo for Sitecore, the Free and Pro editions don’t allow you to add filter and boost expressions by using a point-and-click approach. Instead, you need to add them through code. Remember that Coveo for Sitecore has an embedded version of the JavaScript Search Framework, so it shouldn’t be difficult to implement such expressions.
Adding a Filter Expression
A filter expression is used to filter search results based on certain conditions that you define. For example, you may want to only display items with a template equal to Flight
.
-
Open the code-behind file associated with the Coveo Search component using a text editor (
CoveoSearch.ascx
for ASP.NET andSearchView.cshtml
for ASP.NET MVC).Make sure that you duplicate the component before you modify its code-behind file. To learn how to do this, see Duplicating the Coveo Search Component.
-
Locate the last
script
element. It should look like this:<script type="text/javascript"> Coveo.$(function() { Coveo.$('#<%= Model.Id %>').coveoForSitecore('init', CoveoForSitecore.componentsOptions); }); </script>
-
Locate the tab ID you want to filter results on.
-
Before
Coveo.$('#<%= Model.Id %>')
, insert the following statement:CoveoForSitecore.componentsOptions["MyTabId"].expression = '<%= ToCoveoFieldName("templatename") %> == "Flight"';
-
The complete code should look like this:
// Add a filter expression to display only Flight items. CoveoForSitecore.componentsOptions["MyTabId"].expression = '<%= ToCoveoFieldName("templatename") %> == "Flight"'; Coveo.$("#<%= Model.Id %>").coveoForSitecore("init", CoveoForSitecore.componentsOptions);
In a MVC view rendering, you need to replace
<%= ToCoveoFieldName("templatename") %>
with@(Model.ToCoveoFieldName("templatename"))
. - Save the file.
- Reload your search interface in a web browser.
- Select the tab.
- Validate that only Flight items appear in your search results.
Adding a Boost Expression
A boost expression is used to modify the ranking of a particular subset of search results. It can be implemented in fashion similar to a filter expression, except that you need to use a QRE instead (see Add Query Ranking Expressions at Query Time). For example, if you wanted Flight items with a Price
field lower than $500 to appear first in the list of search results, you would need to follow this procedure.
-
Open the code-behind file associated with the Coveo Search component using a text editor (
CoveoSearch.ascx
for ASP.NET andSearchView.cshtml
for ASP.NET MVC).Make sure that you duplicate the component before you modify its code-behind file. To learn how to do this, see Duplicating the Coveo Search Component.
-
Locate the last
script
element. It should look like this:<script type="text/javascript"> Coveo.$(function() { Coveo.$('#<%= Model.Id %>').coveoForSitecore('init', CoveoForSitecore.componentsOptions); }); </script>
-
Locate the tab ID you want to filter results on.
-
Before
Coveo.$('#<%= Model.Id %>')
, insert the following statement:CoveoForSitecore.componentsOptions["MyTabId"].expression = "$qre(expression:'<%= ToCoveoFieldName("price") %> < 500', modifier:'100')";
A modifier typically has a value comprised within the range [-100, 100]. A positive value means that the subset of search results matching your
expression
are boosted in the search results. A negative value means the opposite; they appear near the end of the list. -
The complete code should look like this:
// Add a boost expression to make flights with a price lower than 500$ appear first in the list of search results. // This is actually represented as a QRE. CoveoForSitecore.componentsOptions["MyTabId"].expression = "$qre(expression:'<%= ToCoveoFieldName("price") %> < 500', modifier:'100')"; Coveo.$('#<%= Model.Id %>').coveoForSitecore('init', CoveoForSitecore.componentsOptions);
In a MVC view rendering, you need to replace
<%= ToCoveoFieldName("templatename") %>
with@(Model.ToCoveoFieldName("templatename"))
. - Save the file.
- Reload your search interface in a web browser.
- Select the tab.
- Validate that only the cheapest flights (< 500$) appear first in your search results.
What’s Next?
- Learn more about the point-and-click approach used to add filter and boost expressions in the Enterprise Edition (see Inserting and Customizing a Search Interface Component).
- Learn more about QREs to modify the ranking of your search results programmatically (see Add Query Ranking Expressions at Query Time).