Alter Search Results Before They Are Displayed in a Search Interface

Warning
Legacy feature

This article pertains to the Coveo Hive framework which is now in maintenance mode.

Choose one of Coveo’s more modern, lightweight, and responsive libraries for any future search interface development. See the search interface Implementation guide for more details.

Important

The features presented in this section are only available when the Coveo for Sitecore reverse proxy is enabled.

There are many different ways to change the appearance of your search results. For example:

  • You can choose which fields and data are indexed for each Sitecore item, therefore effectively changing what data is available to be displayed in your search interface. This mostly involves custom code running in a server-side .NET context.

  • You can customize the layout of your search page so that you only display the fields and data that you’re interested in (even though many more fields are available to be displayed). This mostly involves adding custom, client-side JavaScript code in your layout.

Now, an alternative approach is to use the coveoProcessParsedRestResponse and coveoProcessRestJsonResponse pipelines. Here is how they work:

  1. A user performs a query in a Coveo Hive search interface.

  2. The query is handled by the Coveo REST endpoint running in the Sitecore context.

  3. The REST endpoint calls Coveo and receives a JSON response with the search results.

  4. The coveoProcessRestJsonResponse pipeline kicks in and deserializes the JSON response into .NET objects.

  5. The coveoProcessParsedRestResponse pipeline is called, therefore altering search results.

  6. The search results are serialized in JSON again.

  7. The new JSON response is sent to the search interface.

About the coveoProcessParsedRestResponse Pipeline

This pipeline allows you to modify search results using .NET objects; you don’t have to worry about the serialization process. The pipeline is defined in the Coveo.SearchProvider.Rest.config file.

It calls its processors by passing them an instance of type Coveo.SearchProvider.Rest.Pipelines.CoveoProcessParsedRestResponseArgs. You can access the response information with the ResponseContent property of the CoveoProcessParsedRestResponseArgs instance. The property returns an object of type Coveo.SearchProvider.Rest.Serialization.SearchResponse, which is a dictionary with shorthand properties to get and set the most commonly used values.

Creating a Custom Processor for the coveoProcessParsedRestResponse Pipeline

  1. Assuming that you already have set up a C# project named Tutorials.Lib, you need to add these assembly references:

    1. Coveo.Framework.dll

    2. Coveo.SearchProvider.Rest.dll

    3. Sitecore.Kernel.dll

  2. Create a processor based on the sample code below. This one adds a new field on every result. It simply returns the number of fields that are indexed for the item.

    ComputeNumberOfIndexedFields.cs

     using Coveo.Framework.Processor;
     using Coveo.SearchProvider.Rest.Pipelines;
     using Coveo.SearchProvider.Rest.Serialization;
     namespace Tutorials.Lib.Processors.CoveoProcessParsedRestResponse
     {
         /// <summary>
         /// Custom processor that computes the number of indexed fields for every search result and
         /// add it to the response.
         /// </summary>
         public class ComputeNumberOfIndexedFields : IProcessor<CoveoProcessParsedRestResponseArgs>
         {
             /// <summary>
             /// The "Process" method is called by the pipeline. The "p_Args" instance
             /// is transferred from one processor to another until the pipeline ends.
             /// </summary>
             /// <param name="p_Args">The pipeline arguments.</param>
             public void Process(CoveoProcessParsedRestResponseArgs p_Args)
             {
                 SearchResponse response = p_Args.ResponseContent;
                 foreach (SearchResult result in response.Results) {
                     // The "Raw" property returns all the fields that are indexed on the item.
                     int numberOfIndexedFields = result.Raw.Count;
                     // Define a new field name and set its value.
                     result["numberOfIndexedFields"] = numberOfIndexedFields;
                 }
             }
         }
     }
  3. Build the assembly and copy the DLL file to the website bin folder. For this tutorial, the assembly is Tutorials.Lib.dll.

  4. Finally, modify the configuration file to tell Sitecore when the new processor must be called:

    1. Open the Coveo.SearchProvider.Rest.Custom.config file.

      Note

      In a more formal project, you would want to use a separate include file and register the processor there.

    2. Under sitecore, add the following element, and insert your processor definition.

       <pipelines>
         <coveoProcessParsedRestResponse>
           <processor type="Tutorials.Lib.Processors.coveoProcessParsedRestResponse.ComputeNumberOfIndexedFields, Tutorials.Lib" />
         </coveoProcessParsedRestResponse>
       </pipelines>
  5. Validate that you can now access the numberOfIndexedFields field from a search interface.

For more details on handling field name translation in this pipeline, see Remove Fields From Search Results.