THIS IS ARCHIVED DOCUMENTATION

Removing Fields From Search Results

It’s quite common to use the coveoProcessParsedRestResponse pipeline to modify or remove existing fields. The following tutorial shows how you can implement a custom processor that removes fields from search results displayed in a search page (published in the web database). It also demonstrates how you can translate field names from the Sitecore format to the index format.

  1. Assuming that you already have a C# project set up, you need to add these assembly references:
    1. Coveo.Framework.dll
    2. Coveo.SearchProvider.Rest.dll
    3. Sitecore.ContentSearch.dll
    4. Sitecore.ContentSearch.Linq.dll
  2. Create a new class named RemoveCreatedByAndUpdatedByFields based on the sample code below.

    RemoveCreatedByAndUpdatedByFields.cs

     using System.Collections.Generic;
     using Coveo.Framework.ContentSearch;
     using Coveo.Framework.Items;
     using Coveo.Framework.Processor;
     using Coveo.SearchProvider.Rest.Pipelines;
     using Coveo.SearchProvider.Rest.Serialization;
     using Sitecore.ContentSearch;
    
     namespace Tutorials.Lib.Processors.coveoProcessParsedRestResponse
     {
         /// <summary>
         /// Custom processor that removes the "Created By" and "Updated By" fields when retrieving an item
         /// from the web database.
         /// </summary>
         public class RemoveCreatedByAndUpdatedByFields : 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)
             {
                 // This pipeline is invoked on a different HTTP request than
                 // the one that renders the search page, so we can't rely on "Sitecore.Context.Item".
                 // We must use the "CurrentContextItem" from the arguments instead.
                 // When the current item (that is, the search page) comes from the "web" database,
                 // we remove some fields.
                 if (p_Args.CurrentContextItem != null && p_Args.CurrentContextItem.DatabaseName == "web") {
                     // Get a list of fields that we want to remove from the search results.
                     // Those field names need to match the field names in the Coveo search index.
                     IEnumerable<string> fieldsToRemove = GetFieldNamesToRemove(p_Args.CurrentContextItem);
                     SearchResponse response = p_Args.ResponseContent;
                     foreach (SearchResult result in response.Results) {
                         // Let's remove the fields from the result instance.
                         RemoveFieldsFromResult(result, fieldsToRemove);
                     }
                 }
             }
             /// <summary>
             /// Removes a list of fields from the search result.
             /// </summary>
             /// <param name="p_Result">The search result from which fields are removed.</param>
             /// <param name="p_FieldsToRemove">The list of fields to remove.</param>
             private void RemoveFieldsFromResult(SearchResult p_Result,
                                                 IEnumerable<string> p_FieldsToRemove)
             {
                 foreach (string fieldToRemove in p_FieldsToRemove) {
                     p_Result.Raw.Remove(fieldToRemove);
                 }
             }
             /// <summary>
             /// Gets the <see cref="AbstractFieldNameTranslator"/> that matches the current context item.
             /// </summary>
             /// <param name="p_CurrentItem">The current context item (that is, the search page).</param>
             /// <returns>The <see cref="AbstractFieldNameTranslator"/> for the associated search index.</returns>
             private AbstractFieldNameTranslator GetFieldNameTranslator(IItem p_CurrentItem)
             {
                 // Instead of hardcoding the name of the search index, we use the context item
                 // to retrieve the right search index. Then we can get to the field name translator.
                 ISearchIndex searchIndex = new ContentSearchManagerWrapper().GetIndex(p_CurrentItem.ToIndexable());
                 return searchIndex.FieldNameTranslator;
             }
             /// <summary>
             /// Returns the index field names to remove from the results, translated in the index format.
             /// </summary>
             /// <param name="p_CurrentItem">The current context item (that is, the search page).</param>
             /// <returns>The field names to remove translated in the index format.</returns>
             private IEnumerable<string> GetFieldNamesToRemove(IItem p_CurrentItem)
             {
                 AbstractFieldNameTranslator fieldNameTranslator = GetFieldNameTranslator(p_CurrentItem);
                 return new [] {
                     fieldNameTranslator.GetIndexFieldName("parsedcreatedby").TrimStart('@'),
                     fieldNameTranslator.GetIndexFieldName("parsedupdatedby").TrimStart('@')
                 };
             }
         }
     }
    
  3. Build the project and copy the resulting assembly to the website bin folder. For this tutorial, the assembly is named 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.RemoveCreatedByAndUpdatedByFields, Tutorials.Lib" />
         </coveoProcessParsedRestResponse>
       </pipelines>
      
  5. Validate that the Created By and Updated By fields are displayed when viewing the search page in Preview mode. When visiting the page in Normal mode, the fields won’t appear in the results.

    1. In Preview mode.

    2. In Normal mode.