Create a Custom Field During Indexing

Add a new computed field that is indexed through content search.

Complete the following prerequisites:

At your visual studio solution setup:

  1. Create a new class that implements the IComputedIndexField interface to compute the value of the field.
    The base class Sitecore.Commerce.Search.ComputedFields.BaseCommerceComputedField implements most of this methods of this interface. You can find it at: configuration/sitecore/contentSearch/configuration/defaultIndexConfiguration/fields
    namespace My.Commerce.Overrides
    {
        using System.Collections.Generic;
        using Sitecore.Data;
        using Sitecore.Data.Items;
        using Sitecore.Commerce;
        using Sitecore.Commerce.Search.ComputedFields;
        using Sitecore.ContentSearch;
    
        public class PriceRangeComputedField : BaseCommerceComputedField
        {
            private static List<ID> _validBaseTemplates = new List<ID>
            {
                CommerceConstants.KnownTemplateIds.CommerceProductTemplate
            };
    
            protected override IEnumerable<ID> ValidTemplates
            {
                get { return _validBaseTemplates; }
            }
    
            public override object ComputeValue(IIndexable itemToIndex)
            {
                var validatedItem = GetValidatedItem(itemToIndex);
                if (validatedItem == null)
                {
                    return CommerceSearchResultItemType.Unknown;
                }
    
                if (!validatedItem.HasChildren)
                {
                    return validatedItem["ListPrice"];
                }
    
                decimal currentPrice, minPrice = decimal.MaxValue, maxPrice = decimal.MinValue;
                foreach (Item variantItem in validatedItem.Children)
                {
                    currentPrice = decimal.Parse(variantItem["ListPrice"]);
                    minPrice = (currentPrice < minPrice) ? currentPrice : minPrice;
                    maxPrice = (currentPrice > maxPrice) ? currentPrice : maxPrice;
                }
    
                if (minPrice != maxPrice)
                {
                    return string.Format("{0} - {1}", minPrice, maxPrice);
                }
    
                return string.Format("{0}", minPrice);
            }
        }
    }
  2. Register the computed field with the appropriate search provider. .
    Create a new .config file in the Website\App_Config\Include directory of your Sitecore site and open it for editing
    Note: The system applies the files in alphnumeric order. Ensure the file containing your overrides is named to run after CommerceServer.config, For example ZMy.CommerceServer.Overrides.config or ZCommerceServer.Index.Lucene.config
    For this sample, register with Lucene search.
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <contentSearch>
          <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <defaultIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
              <fields hint="raw:AddComputedIndexField">
                <field fieldName="PriceRange" returnType="string">Sitecore.Commerce.Search.ComputedFields.PriceRangeComputedField, Sitecore.Commerce</field>
              </fields>
            </defaultIndexConfiguration>
          </configuration>
        </contentSearch>
      </sitecore>
    </configuration>
    
  3. Rebuild the Index.
    1. Open the Sitecore Desktop.
    2. Open the Indexing Manager.
      Click Sitecore > Control Panel > Indexing > Indexing Manager >
    3. Check the indexes you wish to rebuild and click Rebuild
The computed values are indexed and can be used in search queries