Gravatar for fcote@coveo.com

Question by fcote, Dec 8, 2016 4:15 PM

Restriting search results to 4 result per sources on a page with Coveo for Sitecore 4.0

The bellow featured used to work in Coveo for Sitecore 3.0 because of how the JS UI 0.9 is processing result template with conditions such as index < 4

1 - Your search page uses multiple sources such as:

@syssource = sourceA 
@syssource = sourceB
@syssource = sourceC
@syssource = sourceD

2 - The requirement you are looking for is to have Search Results organized this way on your Search Interface (for each pages). Hence, any queries will lead to result sorganized this way:

4 Results from @syssource = sourceA, followed by 
4 Results from @syssource = sourceB, followed by 
4 Results from @syssource = sourceC, followed by 
4 Results from @syssource = sourceD

3 - The Search Results from each sources will be using a different <script id="Default" class="result-template" type="text/underscore"...>

4 - Using JS UI 0.9, the way you were achieving this was to have:

<script id="All Sources" class="result-template" type="text/underscore" data-condition='index < 4'> 
</script> 
<script id="SourceA" class="result-template" type="text/underscore" data-condition='raw.syssource == "sourceA" && numberInSourceA < 4'> 
</script> 
<script id="SourceB" class="result-template" type="text/underscore" data-condition='raw.syssource == "sourceB" && numberInSourceB < 4'> 
</script> 
<script id="SourceC" class="result-template" type="text/underscore" data-condition='raw.syssource == "sourceC" && numberInSourceC < 4'> 
</script> 
<script id="SourceD" class="result-template" type="text/underscore" data-condition='raw.syssource == "sourceD" && numberInSourceD < 4'> 
</script> 
1 Reply
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Dec 9, 2016 10:35 AM

From what I understand, you need 4 results per source.

Seeing how this is done, each result-template counts the number of elements shown for each template (most likely incrementing the number within the underscore script itself).

However, this approach has many drawbacks:

  • It requires you to have a large query that hopefully covers 4 results for all of the sources, while what is really needed is approximately 4 results per source + 4 results combined.
  • Since the first 4 results in the large query for each source are shown, you can only have global relevancy, and not something tailored to each of the sources.
  • There is a chance that your large query contains only items from one source, showing results from one source while there could theoretically be 4 results in each.
  • Pager would not work, since you would be hiding results from the first page in all of your sources, further degraded by the fact that you need a large query.

There are more optimal approaches that will improve relevancy if applied correctly.

The examples will use MVC. Don't forget to change MVC calls to WebForms if needed.

After Coveo for Sitecore 4.0.364

Coveo for Sitecore 4.0.364 introduce a new Related Query Component that can be included in any search page.

  1. Duplicate the default Search Interface
    1. Remove the components you don't need and change the layout as required.
    2. Keep the Searchbox, you most likely want your user to search for something.
    3. Keep the coveo-recommendations-mvc section placeholder, so you can still add the Related Query Component.
  2. Use this new component and add a Related Query Component for each of your sources.
    1. Edit its properties to filter by source or add it programmatically.
    2. Set the number of results to 4.
    3. Check both "Bind to search interface" and "Use the search interface current query" to mirror the query done in the Searchbox.

You should now have a search interface that refreshes every time the user enter a new query. It should trigger a new query for each component included in the page, each with its relevancy and filters.

Before Coveo for Sitecore 4.0.364

Since the Related Query Component was not yet added, you need to replicate the behavior of the component by creating a simple search interface that contains only the ResultsList component.

  1. Duplicate the default Search Interface

    1. Remove the components you don't need and change the layout as required.
    2. Remove the Searchbox. We will use an external one.
    3. Keep the ResultList component.
    4. Add the following line at the beginning of the first Coveo.$(function() { ... })
      • CoveoForSitecore.componentsOptions = Coveo.$.extend({}, CoveoForSitecore.searchBoxComponentsOptions || {}, searchOptions);
      • This will add the Searchbox defined in Step #2 as an external component for this search interface
  2. Duplicate the Searchbox component

    1. Add the following line at the end of your Coveo.$(function() { ... })
      • CoveoForSitecore.searchBoxComponentsOptions = { externalComponents: [document.getElementById('@Model.SearchboxId')] };
      • This will define the Searchbox as an external component for the search interface.
  3. Add the Searchbox in your page

  4. Add multiple Search Interface and configure them to filter by source, either with its properties or by adding it programmatically.

After these steps, the Searchbox should trigger the query in all of the search interfaces.

I hope this helps.

Ask a question