Gravatar for

Question by Ken McAndrew, Mar 13, 2019 2:42 PM

Reset facets on a new search

This has been asked before ( but was with Coveo 4.1, and some of the solution seemed potentially incomplete and maybe not applicable to my case. This is using Coveo 5.

I have a global search box and I have my search page. In the DOMContentLoaded event listener, I added the following:

            function (e, args) {
                var currentSearchTerm =;
                console.log("currentSearchTerm: " + currentSearchTerm);
                console.log("previousSearchTerm: " + previousSearchTerm);
                if (previousSearchTerm !== '' && previousSearchTerm !== currentSearchTerm) {
                    console.log(currentSearchTerm != previousSearchTerm);
                    previousSearchTerm = currentSearchTerm;
                previousSearchTerm = currentSearchTerm;

For the previousSearchTerm variable, I've declared it outside of the event listener call. The code is being added via a script block rendering to the page. Note that "coveo123456" is the ID of my search interface.

When I call the page the first time, it shows previousSearchTerm is blank (expected) but that currentSearchTerm is also blank. When I trigger a new search, both variables are shown as blank still, so it doesn't appear either are set. There's no "flash" of refreshing the search page as a whole on the new term, so I assume that being on the search page has that box acting like a local search box essentially; they're hooked to the same search interface.

Is this the right track for this kind of thing?

2 Replies
Gravatar for

Answer by Ken McAndrew, Mar 21, 2019 11:02 PM

One of our developers found a solution:

document.addEventListener("DOMContentLoaded", function () {
    Coveo.$('').on('buildingQuery', function (e, args) {
        console.log("inside buildingQuery");
        let queryController = Coveo.get(this, "QueryController");
        let omniBox = Coveo.get($(".CoveoOmnibox")[0], Coveo.Omnibox);
        let previousSearchTerm = queryController.lastQuery ? (queryController.lastQuery.q ? queryController.lastQuery.q : "") : "";
        let currentSearchTerm = omniBox.element.textContent ? omniBox.element.textContent : "";
        if (previousSearchTerm != currentSearchTerm) {
Gravatar for

Answer by Jean-François L'Heureux, Mar 22, 2019 2:48 PM

The problem is that the searchbox component itself also attaches an event handler on the `buildingQuery` event. In that handler, it adds the searchbox text into the `args.queryBuilder.expression`. This event handler is attached to the search interface when the components are initialized.

Your event handler is likely attached to the search interface before the components are initialized, so it is called before the searchbox event handler. At that time, `args.queryBuilder.expression` is empty.

One simple solution with your original code would be to attach your event handler to the `doneBuildingQuery` event instead of `buildingQuery`.

Ask a question