How to Force the Index to Update on the CD Server

The issue: Index not updating properly

A while back I ran into an issue where the index on the CD server was not updating correctly. I checked the EventQueue table and the event existed to update the index but it just wasn’t happening. The client was frustrated since the items had been published but the index was not updating, so they weren’t able to see the new items on their site.

The Sitecore instance on CD was not available for me to use because it had SwitchMasterToWeb.config enabled; otherwise, I would have just logged in as Admin and updated the index from Control Panel > Index Manager.

So, what is a developer to do but come up with an out of box solution? I want to state that this should only be used as a last resort and not something you should use on a consistent basis.

Now let’s see how to make this work.
 

How do I run the code

  • Copy the code below and save it as RebuildIndexByName.aspx into the /sitecore/admin folder of your application.
  • Example:
    • Index Name: sitecore_master_index
    • https://<domainName>/sitecore/admin/RebuildIndexByName?index=sitecore_master_index
  • After you run the code, then go to the data/indexes/sitecore_master_index folder to see if the index is being updated.
  • Breathe a sigh of relief ­čÖé

 

Methods that can be used on the index

If we then decompile the dll Sitecore.ContentSearch.Maintenance, we will find a class called IndexCustodian. Inside that class we need to find a method to help us manually rebuild the index.

// You can Pause and Resume Indexing by using the following methods.
// Be aware, that PauseIndexing() will pause indexing for the entire site, and should be used with caution. This approach is prone to errors.
public static void PauseIndexing()
{
   EventHub.OnPauseIndexing((object) typeof (IndexCustodian));
}

public static void ResumeIndexing()
{
    EventHub.OnResumeIndexing((object) typeof (IndexCustodian));
}

// We could use this one if we want to rebuild all the indexes but that isn't what we want for our situation
public static IEnumerable<Job> RebuildAll()
{
    CrawlingLog.Log.Warn("IndexCustodian. RebuildAll triggered.", (Exception) null);
    return ContentSearchManager.Indexes.Select<ISearchIndex, Job>((Func<ISearchIndex, Job>) (index => IndexCustodian.FullRebuild(index, true)));
}

// For our example this is the code we want to use
public static Job FullRebuild(ISearchIndex index, bool start = true)
{
   return IndexCustodian.CreateRebuildJob(index, new EventHandler<JobStartedEventArgs>(IndexCustodian.RebuildStartedHandler), new EventHandler<JobFinishedEventArgs>(IndexCustodian.RebuildFinishedHandler), start, (object[]) null);
}

 

Rebuild index by name

C# section

<%@ Page Language="C#" %>
<%@ Import Namespace="Sitecore.ContentSearch" %>
<%@ Import Namespace="Sitecore.ContentSearch.Maintenance" %>

public void Page_Load(object sender, EventArgs e) 
{ 
     var name = ""; 
     try 
     { 
          name = this.Request.QueryString["index"]; 
          if (!string.IsNullOrEmpty(name)) 
          { 
                var index = ContentSearchManager.GetIndex(name); 
                if( index != null ) 
                { 
                     IndexCustodian.FullRebuild(ContentSearchManager.GetIndex(name)); 
                } 
                else 
                { 
                     Response.Write("<span style='color:red'>Index (" + name + ") does not exist, please double check the name. If the index has a space in the name then replace the space with '+' in the url.</span>"); 
                } 
           } 
       } 
       catch(Exception ex) 
       { 
           Response.Write("Error occurred with the following error message: <span style='color:red'>" + ex.Message + " " + ex.StackTrace + "</span>"); 
       } 
}

HTML section

 
<html> 
      <head> 
         <title>Index Rebuild</title> 
      </head> 
<body>
      <h1>Index rebuild</h1>
      <ul>
          <li>Index:<% if(Request.QueryString["index"] != null) { %> 
                        <%= Request.QueryString["index"] %> 
                    <% } else { %>
                       <span style='color:red'>Index name was not given in the query string: /sitecore/admin/RebuildIndexByName?index=[Need index name]"</span>
                    <% } %>
          </li>
          <li>Finished @: <%= System.DateTime.Now %></li>
          <li>If this ran correctly then check the index under Data/Indexes to see if it is doing a rebuild or not</li>
      </ul>
</body> 
</html> 
CJ Morgan

I am a software developer with over 15 years experience. In 2015, I joined BrainJocks and became a Sitecore certified developer. I have a BS degree in Computer Science from East Carolina University. I enjoy learning and solving issues that help me to become a better programmer. I also like helping others to learn because it mutually benefits me and the person learning–so win-win! In my spare time, I like playing video games, basketball, racquetball, and hanging out with my family.

Learn more about CJ Morgan.

Add a Comment

Your email address will not be published. Required fields are marked *

Or request call back