Coveo for Sitecore 5 is now available!

Altering Search Results Before They Are Displayed in a Search Interface

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, thus 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 are 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 search interface.
  2. The query is handled by the Coveo REST endpoint running in the Sitecore context.
  3. The REST endpoint calls CES 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, thus altering search results.
  6. The search results are serialized in JSON again.
  7. The new JSON response is sent to the search interface.
  1. A user performs a query in a search interface.
  2. The query is handled by the Coveo REST endpoint running in the Sitecore context.
  3. The REST endpoint calls the Coveo Cloud platform 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, thus altering search results.
  6. The search results are serialized in JSON again.
  7. The new JSON response is sent to the search interface.

Understanding the coveoProcessParsedRestResponse Pipeline

This pipeline allows you to modify search results using .NET objects; you do not 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, 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;
                     // Simply 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.

      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 Removing Fields From Search Results.