Create a Custom Field During Indexing
Add a new computed field that is indexed through content search.
Complete the following prerequisites:
-
To create Sitecore facets on custom catalog properties, follow the instructions in the Developer's Guide to Item Buckets and Search. It is also required that the search index contain a field for the custom catalog properties.
-
There are one or more virtual or base catalogs containing product and category definitions.
-
Search components are configured.
At your visual studio solution setup:
-
Create a new class that implements the
IComputedIndexFieldinterface to compute the value of the field.The base class
Sitecore.Commerce.Search.ComputedFields.BaseCommerceComputedFieldimplements most of this methods of this interface. You can find it at:configuration/sitecore/contentSearch/configuration/defaultIndexConfiguration/fieldsRequestResponsenamespace 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); } } } -
Register the computed field with the appropriate search provider. .
Create a new .config file in the
Website\App_Config\Includedirectory of your Sitecore site and open it for editingNoteThe 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 orZCommerceServer.Index.Lucene.configFor this sample, register with Lucene search.
RequestResponse<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> -
Rebuild the Index.
-
Open the Sitecore Desktop.
-
Open the Indexing Manager.
Click → → → →
-
Check the indexes you wish to rebuild and click Rebuild
-
The computed values are indexed and can be used in search queries