THIS IS ARCHIVED DOCUMENTATION

Using Boosting or Filtering Rules on the Search 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 mostly intended for Free and Pro edition clients but might also be useful for Enterprise edition clients. It can help you define filters that content editors and marketers won’t have access to.

The Coveo Search component offers a variety of rules to filter or boost your search results.

The defined rules are only applied to the search page containing the search component.

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.

Although the examples in this tutorial are based on the Jetstream Sitecore demo instance, you could easily adapt the provided code to your own data.

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.

  1. Open the code-behind file associated with the Coveo Search component using a text editor (CoveoSearch.ascx for ASP.NET and SearchView.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.

  2. Locate the last script element. It should look like this:

     <script type="text/javascript">
         Coveo.$(function() {
             Coveo.$('#<%= Model.Id %>').coveoForSitecore('init', CoveoForSitecore.componentsOptions);
         });
     </script>
    
  3. Between Coveo.$('#<%= Model.Id %>') and .coveoForSitecore('init', ...), insert the following statement:

     .on("buildingQuery", function(e, args) {
         args.queryBuilder.advancedExpression.add('<%= ToCoveoFieldName("_templatename") %> == "Flight"');
     })
    
  4. The complete code should look like this:

     // Add a filter expression to display only Flight items.
     Coveo.$('#<%= Model.Id %>').on("buildingQuery", function(e, args) {
         args.queryBuilder.advancedExpression.add('<%= ToCoveoFieldName("_templatename") %> == "Flight"');
     }).coveoForSitecore("init", CoveoForSitecore.componentsOptions);
    

    In a MVC view rendering, you need to replace <%= ToCoveoFieldName("_templatename") %> with @(Model.ToCoveoFieldName("_templatename")).

  5. Save the file.

  6. Reload your search interface in a web browser.

  7. 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 a way similar to filter expressions, except that you need to use a QRE instead (see Add Query Ranking Expressions at Query Time). For example, if you would want Flight items with a Price field lower than 500$ to appear first in the list of search results, you would need to follow the following procedure.

  1. Open the code-behind file associated with the Coveo Search component using a text editor (CoveoSearch.ascx for ASP.NET and SearchView.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.

  2. Locate the last script element. It should look like this:

     <script type="text/javascript">
         Coveo.$(function() {
             Coveo.$('#<%= Model.Id %>').coveoForSitecore('init', CoveoForSitecore.componentsOptions);
         });
     </script>
    
  3. Between Coveo.$('#<%= Model.Id %>') and .coveoForSitecore('init', ...), insert the following statement:

     .on("buildingQuery", function(e, args) {
         args.queryBuilder.advancedExpression.add("$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 your search results. A negative value means the opposite; they will end up closer to the end of the list.

  4. 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.
     Coveo.$('#<%= Model.Id %>').on("buildingQuery", function(e, args) {
         args.queryBuilder.advancedExpression.add("$qre(expression:'<%= ToCoveoFieldName("price") %> < 500', modifier:'100')");
     }).coveoForSitecore("init", CoveoForSitecore.componentsOptions);
    

    In a MVC view rendering, you need to replace <%= ToCoveoFieldName("price") %> with @(Model.ToCoveoFieldName("price")).

  5. Save the file.

  6. Reload your search interface in a web browser.

  7. Validate that only the cheapest flights (< $500) appear first in your search results.

What’s Next?