Gravatar for gbakx@firstderivatives.com

Question by gideon, Sep 27, 2018 8:12 PM

Howto reset all facets, when a new search takes place?

We've a search page that contains 3 facets with various of options. When searching for a new search term, we want to clear out all facet selections.

Use case: Searching for "Microsoft" and selecting various facets. Selecting the facets, should filter down the results and not reset the facet selection.

By default, changing the search query from "Microsoft" to "Sony" will retain all facets. We need to clear all facets as the search query has changed.

Question: How to accomplish this?

--

Version details
Current Coveo for Sitecore version: 4.1.286.9
Current Sitecore version: 8.2.171121
Using Coveo Hive with Coveo Cloud v2

--

Things tried
Various items are tried. The initial thought was to add a listener to the buildingQuery event
and monitor changes to the search query from that point. Although, this seems to work, it triggers a race
condition where the facets are reset UI wise, but internally they are still being applied.

Code below

Various variations of below code. Including remove all logic that compares search terms:

search.on('buildingQuery', function(e, args) {
    var currentSearchTerm = args.queryBuilder.expression.parts.join();
        console.log("currentSearchTerm: " + currentSearchTerm);
    console.log("previousSearchTerm: " + previousSearchTerm);
    if (previousSearchTerm !== '' && previousSearchTerm !== currentSearchTerm) {
        console.log("currentSearchTerm != previousSearchTerm");
        Coveo.$('.CoveoFacet').coveo('reset') 
        Coveo.$("#coveoC606340E").coveo("executeQuery");
    }
    previousSearchTerm = currentSearchTerm;
});
1 Reply
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Sep 27, 2018 9:29 PM

I have tried your snippet, and it is causing an infinite loop because `previousSearchTerm` is never changed when the new query is executed.

I have changed it to the following and it worked:

search.on('buildingQuery', function (e, args) {
    var currentSearchTerm = args.queryBuilder.expression.parts.join();
    console.log("currentSearchTerm: " + currentSearchTerm);
    console.log("previousSearchTerm: " + previousSearchTerm);
    if (previousSearchTerm !== '' && previousSearchTerm !== currentSearchTerm) {
        console.log(currentSearchTerm != previousSearchTerm);
        previousSearchTerm = currentSearchTerm;
        Coveo.$('.CoveoFacet').coveo('reset');
        search.coveo("executeQuery");
    }
    previousSearchTerm = currentSearchTerm;
});
Gravatar for gbakx@firstderivatives.com

Comment by gideon, Sep 28, 2018 12:58 AM

@François Lachance-Guillemette Thank you very much. It looks like it's indeed working with that change.

Ask a question