0

According to this thread, the "index" is the built-in variable that can be used in the Coveo search result templates. Is there a document that list all available built-in" variables in the scope of result template? especially I needed is to use the total count of the search result to have a logic, inject the survey block after 3rd search result, or append to last if the total count is less than 3.


<script class="result-template" type="text/underscore">
    <div>
        {{ if (index == 3 || index == totalCount ) { }}
            <div class="coveo-result-row survey-block">
                 Are these results helpful?
                 <a href="#" class="survey-yes">Yes</a>
                 <a href="#" class="survey-no">No</a>
            </div>
        {{ } }}
    <div>

1 Answer 1

1

The coveo for Sitecore shows the total number of count with the result like this.

enter image description here

However you can try this code to get it and show it where you want with your page.

Coveo.$(function() {
 var options = <%= Model.GetJavaScriptInitializationOptions() %>;

 // ADDED CODE -->
 Coveo.$('#search')
 .on('buildingQuery', function (e, args) {
 args.queryBuilder.expression.add('search');
 })
 .on('querySuccess', function (e, args) {
 console.log('Query returned ' + args.results.totalCount + ' results
in ' + args.results.duration + 'ms.');
 });
 // <-- ADDED CODE
 Coveo.$('#search').coveoForSitecore('init', options);
});

Using the args.results you can also get the attributes of the result returned.

Hope this helps.

3
  • Thanks for the hints. How can I use that inside the template? <script class="result-template" type="text/underscore"> <div> Index: {{= index }} Total: {{= total }} <div> <div class="search-card container coveo-result-item" id="{{= coveoFieldValue("npi") }}"> <div class="provider-details">
    – John Lee
    Commented Aug 23 at 14:28
  • You can bind it by taking the reference of your element. Commented Aug 23 at 15:18
  • Hi Sumit, can you elaborate a little bit? I do not see any javascript like the one you show in the result template file. where do I add this code? I also did not understand how could the search has the result and I do not see any code behind the search button.
    – John Lee
    Commented Aug 25 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.