Sitecore: Solr Index rebuild automatically using c# code



Note: if you are not aware or Sitecore CMS please click here

Problem Statement

In our project architecture we have multiple CD server in different azure cloud Geo-locations. But during Go live and outage we need to switch to DR (disaster recovery) server for certain period of time. So there is a requirement to sync the Live Sitecore CD (content deliver) server with the DR server after certain period of time. We achieve this using SQL database transactional replication.

But one challenge faced during Solr index rebuild. We have to do manual Solr index rebuild on DR server after every 24 hrs.

Solution:

Step 1: Create a new separate project "SitecoreTasks" with below code base


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SitecoreTasks
{
    public class AutoIndexRebuildAgent
    {
        public string CoreList { get; set; }
        public string Enable   { get; set; }
        public void Run()
        {
            if (Enable.Trim().ToLower() == "true")
            {
                foreach (string itemCoreName in CoreList.Split(','))
                {
                    if (!string.IsNullOrEmpty(itemCoreName))
                    {
                        Sitecore.ContentSearch.ISearchIndex index =
                            Sitecore.ContentSearch.ContentSearchManager.GetIndex(itemCoreName.Trim());
                        if (index != null)
                        {
                            index.Rebuild(Sitecore.ContentSearch.IndexingOptions.ForcedIndexing);
                        }
                        Sitecore.Diagnostics.Log.Info(this + " : " + itemCoreName.Trim(), this);
                    }
                }
            }
            else
            {
                Sitecore.Diagnostics.Log.Info("Auto Index Rebuild Agent is Disabled", this);
            }
        }
    }
}



Step 2: Create a Sitecore agent as shown below in “Sitecore.config” file or you may create any separate patch file

 
<agent type="SitecoreTasks.AutoIndexRebuildAgent" method="Run" interval="24:00:00">
      <CoreList>sitecore_web_webindex,sitecore_web_masterindex</CoreList>
      <Enable>true</Enable>
</agent>


Let’s check every attribute and property in details 


  • Agent type: "SitecoreTasks.AutoIndexRebuildAgent" is the class which contains the logic of index rebuild 
  • Interval: You can modify the setting as per requirement. In my case it should run after 24 hrs. Remember the setting should be in hh:mm:ss 
  • CoreList: comma separated name of the solr core to rebuild index.
  • Enable: Flag to decide whether the indexing should rebuild or not. You may pause the scheduler on demand without removing the complete Sitecore.config settings.

Now you can validate the result after 24 hrs which automatically rebuild solr index from Sitecore backend agent scheduler.

If you like this article please comment and share with others. 
Happy coding 😃

Comments

Popular posts from this blog

Sitecore Powershell Extension (SPE)