Gravatar for corrac@paragon-inc.com

Question by Cris Corra, Jul 23, 2014 1:43 PM

How can I set the default sort criteria to a custom field using Coveo for Sitecore?

I need to change the default sort of the query to a custom field without the user having to click a sort component. How can I pass/invoke the custom sort field to the query? I tried adding this code to my query init, but it did not invoke the sort correctly (the url param kept putting a real space instead of %20 in between the fieldname and the direction):

var options = @(Html.Raw(Model.GetJavaScriptInitializationOptions())); Coveo.$('#search') .coveoForSitecore('init', options) .coveo('state', 'sort', '@(Html.Coveo().ToCoveoFieldName("Start DateTime")) ascending') .coveo('executeQuery'); });

Resulting url param: /Meetings#first=0&sort=%40fstartz32xdatetime29877 ascending

-Cris Corra

1 Reply
Gravatar for vseguin@coveo.com

Answer by Vincent Séguin, Jul 23, 2014 3:35 PM

Hi Cris,

We currently have a story to add this in the product. Meanwhile, this could do the trick, add it before the init :

var isFirstQuery = true;
Coveo.$('#search').on('afterInitialization', function(e){
    if (isFirstQuery) {
        isFirstQuery = false;
        Coveo.$('.CoveoSort').eq(0).coveo('select'); // Where the '0' corresponds to the index of the sort component
    }
});
Gravatar for corrac@paragon-inc.com

Comment by Cris Corra, Jul 24, 2014 9:20 AM

That worked great! Thanks! Cris

Gravatar for vseguin@coveo.com

Comment by Vincent Séguin, Jul 24, 2014 9:24 AM

Don't forget to accept the answer as the good one :)

Gravatar for daniel.reedy@tcw.com

Comment by Daniel Reedy, May 19, 2015 4:18 PM

Has this feature been added into the product? If so, what version?

Gravatar for vseguin@coveo.com

Comment by Vincent Séguin, May 19, 2015 4:52 PM

Yes, you can see it here : https://developers.coveo.com/display/public/SC201504/Understanding+the+Coveo+Search+Component%27s+Properties

In the "Default Sort Options". It has been added in the October 2014 version.

Gravatar for daniel.reedy@tcw.com

Comment by Daniel Reedy, May 19, 2015 6:09 PM

great, thanks.

Ask a question