Altering Search Results Before They Are Displayed in a Search Interface
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, 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:
-
A user performs a query in a search interface.
-
The query is handled by the Coveo REST endpoint running in the Sitecore context.
-
The REST endpoint calls CES and receives a JSON response with the search results.
-
The
coveoProcessRestJsonResponse
pipeline kicks in and deserializes the JSON response into .NET objects. -
The
coveoProcessParsedRestResponse
pipeline is called, therefore altering search results. -
The search results are serialized in JSON again.
-
The new JSON response is sent to the search interface.
-
A user performs a query in a search interface.
-
The query is handled by the Coveo REST endpoint running in the Sitecore context.
-
The REST endpoint calls the Coveo Platform and receives a JSON response with the search results.
-
The
coveoProcessRestJsonResponse
pipeline kicks in and deserializes the JSON response into .NET objects. -
The
coveoProcessParsedRestResponse
pipeline is called, therefore altering search results. -
The search results are serialized in JSON again.
-
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 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
-
Assuming that you already have set up a C# project, you need to add these assembly references:
-
Coveo.Framework.dll
-
Coveo.SearchProvider.Rest.dll
-
Sitecore.Kernel.dll
-
-
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; } } } }
-
Build the assembly and copy the DLL file to the website
bin
folder. For this tutorial, the assembly isTutorials.Lib.dll
. -
Finally, modify the configuration file to tell Sitecore when the new processor must be called:
-
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.
-
Under
sitecore
, add the following element, and insert your processor definition.<pipelines> <coveoProcessParsedRestResponse> <processor type="Tutorials.Lib.Processors.coveoProcessParsedRestResponse.ComputeNumberOfIndexedFields, Tutorials.Lib" /> </coveoProcessParsedRestResponse> </pipelines>
-
-
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.