Gravatar for

Question by Tony, Mar 8, 2016 1:42 PM

Avoiding an Initial search with no keywords


We are using a global search box and a search page (Coveo View - MVC) in place. We would like to block the initial search from getting triggered if there are no keywords.

For example, a user comes to the site and enters "Ham" in the search box. The search box eventually redirects to the search results page. The query string looks something like http://qa.factory.local/search#q=Ham - This works.

Now the issue comes if the user comes to the search page without entering any keywords. In such case the request URL would look like http://qa.factory.local/search - If we enter this they will see a page with general results (no keyword was entered). We would like to block this search from getting executed.

Disabling the Auto Trigger Query checkbox is not an option since we want to be able to display results from the global search box. If We disable this checkbox then invoking the following URL won't work. - http://qa.factory.local/search#q=Ham

We only want to block the trigger if there are no keywords (like http://qa.factory.local/search#)

I looked on this forum and saw a solution where they patch the "QueryController.executeQuery" method and only execute the query if there is a value entered in the search box.

Coveo.$(this).coveo('patch', "QueryController.executeQuery", function () {
    Coveo.$.trim($("#search").coveo('state', 'q')) != '' && this.__executeQuery();

This works but it also breaks the paging functionality which creates another issue.

Any tips?

We are using JS v0.9 - I also saw this post on your site that deals with a similar issue but it is for version 1.0

Any help would be appreciated.



2 Replies
Gravatar for

Answer by Tony, Mar 8, 2016 5:01 PM

I managed to get this working.

Just for reference and if somebody else needs it this is what I had to do. If there is a simpler way to go about it then let me know.

I had to add the following:

Coveo.$('#search').on('afterComponentsInitialization', function(e, args) {
    Coveo.$(this).coveo('patch', 'QueryController.executeQuery', function(cArgs) {
        Coveo.$.trim($("#search").coveo('state', 'q')) != '' && this.__executeQuery(cArgs);
Gravatar for

Comment by Thy Long, Oct 24, 2016 8:57 AM

I was able to get it working using the above code but when a user hits the 'X' to clear out the search keyword, the spinner in the search button appears. To get rid of it, the user would have to type in another search term and hit enter to get the results. It also spins when you hit search with an empty search box. Anyone know of a way to hide or disable to spinner?

We are using Coveo for Sitecore 4.0.


Gravatar for

Comment by François Lachance-Guillemette, Oct 24, 2016 10:11 AM

Hi @tlong, I have added a new answer regarding this use case for Coveo for Sitecore 4.0


Gravatar for

Answer by François Lachance-Guillemette, Oct 24, 2016 10:10 AM

Coveo for Sitecore 4.0 bundles JS Framework 1.0, and since the September 2016 release, uses the open source version.

Instead of trying to patch the executeQuery method, I would recommend something similar to this sample:

  1. Setting the Auto Trigger Query parameter to false so you don't execute a query when first landing on the page
    • Coveo for Sitecore fronts this option in the Coveo Search Component properties, under the "Advanced" section.
  2. Execute the query under valid conditions by detecting the first state change and see if its value was empty or not.

Here is a sample code that, coupled with the data-auto-trigger-query="false" attribute, executes the query when the value is set in the URL.

var searchComponent = Coveo.$('#search');"state:change:q", function(e, state) {
    if(state.value != "") {


Gravatar for

Comment by Thy Long, Oct 24, 2016 4:03 PM

This works with a few modifications.

 var searchInterface = Coveo.$('#search');

      .on("state:change:q", function(e, state) {
          if(Coveo.$.trim(searchInterface.coveo('state', 'q')) != ""){
       .on("buildingQuery", function(e, args) {
                    args.queryBuilder.advancedExpression.add('(NOT @Model.ToCoveoFieldName("Excluded Location Identifiers") == "' + selectedLocation + '")');
       .coveoForSitecore('init', CoveoForSitecore.componentsOptions);

The issue I'm running into now is that whenever the 'X' button is clicked to clear out the search box, the results of all indexed documents will appear. The X button seems to bypass the logic to check the state and executes the query anyway. Is there a way to prevent this from happening?

Gravatar for

Comment by François Lachance-Guillemette, Oct 24, 2016 4:30 PM

The IBuildingQueryEventArgs have a cancel property. Set it to false directly in your buildingQuery to cancel the current query.

Let me know if this works for you :)


Gravatar for

Comment by Jean-François L'Heureux, Oct 24, 2016 4:41 PM

There's a new triggerQueryOnClear option on the CoveoQuerybox component. Its value is true by default but can be set to false.

<div class="CoveoSearchbox" data-trigger-query-on-clear="false"></div>


if (!CoveoForSitecore.componentOptions.Searchbox) { CoveoForSitecore.componentOptions.Searchbox = {}; }
CoveoForSitecore.componentOptions.Searchbox.triggerQueryOnClear = false;
Gravatar for

Comment by Thy Long, Oct 25, 2016 8:59 AM

The Coveo for Sitecore build we're using is Which build was that option released with?

Gravatar for

Comment by François Lachance-Guillemette, Oct 25, 2016 9:11 AM

The triggerQueryOnClear option was added in the coveo-search-ui version bundled with the October release of Coveo for Sitecore.

The cancel on the buildingQuery should be available in 4.0.171 though :)


Gravatar for

Comment by Jean-François L'Heureux, Oct 25, 2016 9:16 AM

It is available since the September 2016 release of the Coveo JavaScript Search Framework:

As for Coveo for Sitecore, it is available since the October 2016 release (4.0.450):

Gravatar for

Comment by Thy Long, Oct 25, 2016 9:48 AM

In the buildingQuery event, I've added logic to check if the 'state', 'q' was empty and then set to cancel property to true if so. This works.

Thanks guys!

Gravatar for

Comment by Jean-François L'Heureux, Oct 25, 2016 11:32 AM

@tlong, can you share your complete solution to this question please? I'm sure it will be helpful to other people in the future.

Gravatar for

Comment by Thy Long, Oct 25, 2016 3:51 PM

Please see below for the complete solution.

 Coveo.$(function() {
            var searchComponent = Coveo.$('#search');

                .one("state:change", function(e, args) {
                    if(Coveo.$.trim(searchComponent.coveo('state', 'q')) != ""){
                .on("buildingQuery", function(e, args) {
                    if(Coveo.$.trim(searchComponent.coveo('state', 'q')) === ""){
                        args.cancel = true;
                        return false;
                }).coveoForSitecore('init', CoveoForSitecore.componentsOptions);
Ask a question