Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Computed SOLR Index Fields

author-icon-1 Mitesh Patel

category-icon Sitecore Training

The SOLR index is your quick access to Sitecore content. You can extend this access by adding computed index fields. This is a way of enriching your searches with content that is not part of your Sitecore templates, but is needed when doing quick searches.

You can use custom computed fields as per our requirement on fields. Like if you require a special formatted date and do not want to recompile code each time the format changes but want to just change the configuration then, there are various other reasons why you can use custom computed fields.

Please find below the steps to create a computed field on the code side.

To create a computed index field:

  1. Create a class that implements the Sitecore.ContentSearch.ComputedFields.IComputedIndexField interface.
  2. Implement simple string properties named FieldName,ReferencedFieldName and ReturnType. The FieldName string is the name the field uses in the index.
  3. Implement a method called ComputeFieldValue(). This method accepts an argument that implements the Sitecore.ContentSearch.IIndexable interface and that specifies the data to index. It returns an object that represents the value for the field.

You need to create a patch config file for computed fields.

                            <?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">

  <sitecore>

    <contentSearch> 

        <indexConfigurations> 

            <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">

                <documentOptions type="Sitecore.ContentSearch.DocumentBuilderOptions, Sitecore.ContentSearch"> 

                    <fields hint="raw:AddComputedIndexField">

                    <field fieldName="Date" referencedFieldName="__Updated" dateFormatPattern="yyyyMMdd" returnType="string">

                        xyz.Foundation.Search.ComputedFields.DateFormateField,xyz.Foundation.Search 

                    </field> 

                    </fields> 

                </documentOptions>

            </defaultSolrIndexConfiguration> 

        </indexConfigurations>

    </contentSearch>

  </sitecore>

</configuration> 
                            
                        

Then after we need to create ComputedFields Class and our logic in this class file.

                            
namespace xyz.Foundation.Search.ComputedFields 

{ 

    public class DateFormateField : IComputedIndexField 

    { 

        public DateField(XmlNode configurationNode) 

        { 

            FieldName = XmlUtil.GetAttribute("fieldName", configurationNode); 

            ReferencedFieldName = XmlUtil.GetAttribute("referencedFieldName", configurationNode); 

            DateFormatPattern = ”yyyyMMdd”; 

        } 

         

        public string FieldName { get; set; } 

         

        public string ReferencedFieldName { get; set; } 

         

        public string DateFormatPattern { get; set; } 

         

        public virtual object ComputeFieldValue(IIndexable indexable) 

        { 

            var item = (SitecoreIndexableItem)indexable; 

             

            if (item?.Item == null) 

            { 

                Log.Error("DateField: indexable is not provided"); 

                return string.Empty; 

            } 

                        

            var field = (Sitecore.Data.Fields.DateField)item.Item.Fields[ReferencedFieldName]; 

             

            if (field == null) 

            { 

                Log.Debug($"DateField: Cannot find field '{ReferencedFieldName}'"); 

                return string.Empty; 

            } 

                                  

            return field.DateTime.ToString(DateFormatPattern, CultureInfo.InvariantCulture); 

        } 

    } 

} 

                            
                        

Recent Posts