Gravatar for jbachmann@singlestoneconsulting.com

Question by jbachmann, Oct 29, 2014 1:04 PM

Pre-selecting facet option based on where a user is when using a global searchbox

Let's build a hypothetical scenario:

In the environment I'm working in, I have multiple sites within a single Sitecore instance. The main search results page lives under site 'SharedSite'. On many pages in sites 'MySite1' and 'MySite2', I've added a Global Searchbox as described by the following developers page: https://developers.coveo.com/display/public/SC201410/Create+a+Global+Searchbox+for+Your+Site

I have indexed data from all of the sites in my Sitecore instance and all data has a calculated field which indicates from which site the content was indexed: @fsourcesite12345 = 'MySite2'

On the search results page, I have a facet which is driven by the @fsourcesite12345 field.

Now the question:

I would like to send the site name ('MySite1') along with the query when using the Global Searchbox, and upon arrival on the search results page, I would like the appropriate facet option to be selected, based on the site name value.

Expanding on the developers tutorial example, I tried adding &aq=(@fsourcesite12345=="MySite1") to the end of the url generation, but when I arrived on the query results page and inspected the REST query object, my aq parameter was not included in the aq field.

Any thoughts?

  • Jordan
3 Replies
Gravatar for jbachmann@singlestoneconsulting.com

Answer by jbachmann, Oct 30, 2014 10:52 AM

Since my ultimate goal was to pre-select a facet value on the search results page based on context from the referring page, I gave this a shot and it appears to work just fine:

http://localhost/search#q=bananas&f:@fsourcesite12345=[MySite1]

I simply append the f: parameter during the newQuery event on the global searchbox. I'm able to get the MySite1 value through the current context of the specific page on which the global searchbox resides.

Upon landing on the search results page, the query is run and the facet option is already checked and properly filtering.

If for any reason the appropriate facet is not present on the search results page, the f: parameter seems to simply be ignored, which is the behavior I was hoping for.

I imagine this is not a completely ideal solution, but it does save me from manually parsing out any values from the request object when landing on the search results page.

If anyone has any specific warnings about using this approach, please let me know.

Gravatar for vseguin@coveo.com

Comment by Vincent Séguin, Oct 30, 2014 10:55 AM

The only problem i see with that is that the field name changes according to which database you're querying. Make sure to use the ToCoveoFieldName extension when you append the :f parameter and not just hardcoding 'fsourcesite12345', and you should be fine.

Gravatar for jbachmann@singlestoneconsulting.com

Comment by jbachmann, Oct 30, 2014 11:24 AM

Yes, I'm using the helper methods. I left those details out for brevity. Thanks!

Gravatar for vseguin@coveo.com

Answer by Vincent Séguin, Oct 29, 2014 3:25 PM

Hi,

Only the q parameter is used from the URL. You will need to extract yourself the aq parameter from the URL and add it in the advanced expression in your query results page.

For instance, in your query results page :

        Coveo.$('#search').on('buildingQuery', function(e, args) {
            args.queryBuilder.advancedExpression.add('<%= ToCoveoFieldName("sourcesite")%>=' + MYEXTRACTEDPARAMETER);
        });
Gravatar for jbachmann@singlestoneconsulting.com

Comment by jbachmann, Nov 20, 2014 3:38 PM

Doing what you described doesn't appear to actually check the facet option as active, it simply filters the results down to only include the relevant sourcesite items. That's not the behavior I'm looking for. I literally want to just have a facet option checked by default upon landing on the page based on something in the URL.

Is there a javascript event I can fire to activate a single facet option?

Gravatar for matthew.hile@vml.com

Answer by matthile, Jan 13, 2016 4:32 PM

I think you're looking for https://developers.coveo.com/display/public/JsSearchV1/Facet+Component#FacetComponent-selectValue

Wire up to the afterInitialization event and fire that off for the facet you want to select an item for.

Ask a question