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. I cleared the EventQueue table and tried again but in some cases it just didn’t seem to want to rebuild the index. 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 Sitecore.ContentSearch.Maintenance dll, we will find a class called IndexCustodian. Inside that class we need to find a method to help us manually rebuild the index. In this case we are going to rebuild the index based on the name given
// 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
<%@ 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> <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>