Devops

How to Force the Index to Update on the CD Server

Blue Glow Laptop@1x.jpg

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.

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

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&amp;lt;Job&amp;gt; RebuildAll()
{
CrawlingLog.Log.Warn("IndexCustodian. RebuildAll triggered.", (Exception) null);
return ContentSearchManager.Indexes.Select&amp;lt;ISearchIndex, Job&amp;gt;((Func&amp;lt;ISearchIndex, Job&amp;gt;) (index =&amp;gt; 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&amp;lt;JobStartedEventArgs&amp;gt;(IndexCustodian.RebuildStartedHandler), new EventHandler&amp;lt;JobFinishedEventArgs&amp;gt;(IndexCustodian.RebuildFinishedHandler), start, (object[]) null);
}

 

Rebuild index by name

C# section

&amp;lt;%@ Page Language="C#" %&amp;gt;
&amp;lt;%@ Import Namespace="Sitecore.ContentSearch" %&amp;gt;
&amp;lt;%@ Import Namespace="Sitecore.ContentSearch.Maintenance" %&amp;gt;

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("&amp;lt;span style='color:red'&amp;gt;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.&amp;lt;/span&amp;gt;");
}
}
}
catch(Exception ex)
{
Response.Write("Error occurred with the following error message: &amp;lt;span style='color:red'&amp;gt;" + ex.Message + " " + ex.StackTrace + "&amp;lt;/span&amp;gt;");
}
}

HTML section

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

CJ is a software developer with over 15 years of experience. In 2015 he became a Sitecore certified developer. CJ has a BS in Computer Science from East Carolina University. He enjoys learning and solving issues that help me to become a better programmer. He also likes teaching others because it mutually benefits him and the person learning--so win-win! In his spare time, CJ enjoys playing video games, basketball, racquetball, and hanging out with family.

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories