1

I have a computed field which is not updated when Sitecore item getting into index (all other field values are indexed) but if I do manual rebuild for the index then my computed field value get updated properly. I tried diff index strategies but no luck. I am using Sitecore 9.3, below is my config.

 <?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
    <sitecore search:require="solr">
        <contentSearch>
            <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
                <indexes hint="list:AddIndex">
                    <index id="xyz" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
                        <param desc="name">$(id)</param>
                        <param desc="core">$(id)</param>
                        <param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
                        <configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
                            <enableReadAccessIndexing>true</enableReadAccessIndexing>
                            <documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">
                                <include hint="list:AddIncludedTemplate">
                                    <!-- template ids -->
                                    
                                </include>
                                <include hint="list:AddIncludedField">
                                    <fieldId>
                                            <!-- fields -->
                                    </fieldId>
                                </include>
                                <field hint="raw:AddComputedIndexField">
                                    <field fieldName="_myComputedField"  returnType ="int">xxx.xxx.xxx.xxx, xxx.xxx</field>                                 
                                </field>
                            </documentOptions>
                        </configuration>
                        <strategies hint="list:AddStrategy">
                            <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual" />
                            <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/syncItems" role:require="xyz and !Indexing"  />
                        </strategies>
                        <locations hint="list:AddCrawler">
                            <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                                <Database>NewsDB</Database>
                                <Root>/sitecore/content/xxx/</Root>
                            </crawler>
                        </locations>
                        <enableItemLanguageFallback>false</enableItemLanguageFallback>
                        <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
                    </index>
                </indexes>
            </configuration>
            <indexConfigurations>
                <indexUpdateStrategies>
                    <syncItems type="Sitecore.ContentSearch.Maintenance.Strategies.SynchronousStrategy, Sitecore.ContentSearch">
                        <param desc="database">xxx</param>
                    </syncItems>                    
                </indexUpdateStrategies>
            </indexConfigurations>
        </contentSearch>
    </sitecore>
</configuration>

any suggestion please. All your suggestions will be helpful to identify and solve the issue. Thanks!!!

Thanks, Prathik

3

1 Answer 1

0

Below is my full code for the computed field

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Items;
using System;

namespace SC.Solr.ComputedFields
{
    public class NewsOrderComputedField : IComputedIndexField
    {

        /// <summary>
        /// field name
        /// </summary>
        public string FieldName { get; set; }

        public string ReturnType { get; set; }
        /// <summary>
        /// computed field logic
        /// </summary>
        /// <param name="indexable"></param>
        /// <returns></returns>
        public object ComputeFieldValue(IIndexable indexable)
        {
            // Check that we have something to index
            if (indexable == null)
            {
                return 0;
            }

            try
            {
                var item = (Item)(indexable as SitecoreIndexableItem);
                if (item == null)
                {
                    return 0;
                }
                string siteName = item.Fields["siteName"].ToString();
                if (string.IsNullOrEmpty(siteName))
                {
                    return 0;
                }
               
                    var newsOrderfromConfig = Sitecore.Configuration.Settings.GetSetting(siteName);
                    if (string.IsNullOrEmpty(newsOrderfromConfig))
                    {
                        return 0;
                    }

                    if (Int32.TryParse(newsOrderfromConfig, out int newsOrder))
                    {
                        return newsOrder;
                    }
                
            }
            catch (Exception ex)
            {
                Logger.Error("Error : ComputedFieldValue ", ex, this);
            }
            return 0;
        }
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.