Gravatar for jason.wilkerson@rbaconsulting.com

Question by jason_wilkerson, Jun 15, 2015 2:46 PM

Issue with filtering programmatically

I am attempting to follow the example in this article: https://developers.coveo.com/display/public/SC201410/Adding+Filter+and+Boost+Expressions+Programmatically

However, Step 2 says to locate in SearchView.cshtml:

<script type="text/javascript">
Coveo.$(function() {
    Coveo.$('#search').coveoForSitecore('init', CoveoForSitecore.componentsOptions);
});
</script>

But in our SearchView.cshtml, it looks like:

<script type="text/javascript">
Coveo.$(function() {
   CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()));
});
</script>

I have tried a few different variations of adding the filters as suggested, but every time, the page doesn't render properly. Here's my latest attempt:

<script type="text/javascript">
Coveo.$(function() {
    CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()));
});

Coveo.$("#search").on("buildingQuery", function(e, args) {
    args.queryBuilder.constantExpression.add("@(Model.ToCoveoFieldName("templatename")) == 'Event'");
}).coveoForSitecore("init", CoveoForSitecore.componentsOptions);
</script>

Any help would be appreciated!

Thanks! Jason

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Jun 15, 2015 4:15 PM

Hi Jason,

The step 2 says to look for the last script element. I think you are looking at the first one in your file. This documentation page is for ASP.Net Web Forms. It's normal to have a few differences in MVC.

Your code is almost right. The only problem is that the .coveoForSitecore('init', ...) call is not enclosed in a DOM ready callback like the CoveoForSitecore.componentsOptions variable assignation. This cause the initialization of the UI to be executed too early when the page loads.

Here's the corrected code:

@* First script element near the top of the file *@
<script type="text/javascript">
    Coveo.$(function() {
        CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()));
    });
</script>
...
@* Last script element near the end of the file *@
<script type="text/javascript">
    Coveo.$(function() {
        Coveo.$('#search')
        .on("buildingQuery", function(e, args) {
            args.queryBuilder.constantExpression.add('@(Model.ToCoveoFieldName("templatename")) == "Event"');
        }).coveoForSitecore('init', CoveoForSitecore.componentsOptions);
    });
</script>
Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jun 15, 2015 4:15 PM

Also, I see that you are looking at the October 2014 documentation. It is a bit old and I bet you have a newer version of the product installed. Here's the link to the May 2015 version of the documentation: https://developers.coveo.com/display/SC201505

Gravatar for jason.wilkerson@rbaconsulting.com

Comment by jason_wilkerson, Jun 15, 2015 8:45 PM

Thank you! Works perfectly!

Gravatar for jason.wilkerson@rbaconsulting.com

Comment by jason_wilkerson, Jun 15, 2015 9:00 PM

One follow up… When I try to nest AND and ORs, I get no results:


Coveo.$(function() {
      Coveo.$('#search')
      .on("buildingQuery", function(e, args) {
         args.queryBuilder.constantExpression.add('(@(Model.ToCoveoFieldName("templatename")) == "Event"' +
            ' OR @(Model.ToCoveoFieldName("templatename")) == "Event Series")' +
            ' AND (@(Model.ToCoveoFieldName("templatename")) != "__Standard Values")');
         }).coveoForSitecore('init', CoveoForSitecore.componentsOptions);
      });

Is there a special way to handle this?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jun 16, 2015 9:57 AM

The problem is not the AND/OR. It's just that the != operator is not a Coveo field operator. The correct one is <>. Here the list of Coveo operators: https://onlinehelp.coveo.com/en/ces/7.0/user/searchprefixand_operators.htm

But you don't need to filter out "__Standard Values" based items if you already only filter in "Event" or "Event Series" ones.

Gravatar for jason.wilkerson@rbaconsulting.com

Comment by jason_wilkerson, Jun 16, 2015 10:19 AM

Perfect, thank you!

And you are correct - the proper final AND should have been:

' AND (@(Model.ToCoveoFieldName("displayname")) <> "__Standard Values")'

Thanks for the help and the link!

Ask a question