Surendra Sharma

Surendra Sharma

Search This Blog

Friday, June 10, 2016

How to rebuild index programmatically in Sitecore



We have DR(Disaster Recovery) environment and everyday SQL job taking Production WEB database backup and restoring it on next day on our DR SQL server.

We are using SOLR search in our project.

Due to this database restoration, index rebuild is required for SOLR search. Initially we are doing indexing manually on daily basis.

But later on I make this rebuild process automatically.

Solution:
The easiest way to rebuild an index automatically is to create a scheduled agent that will call and run the below code.

There are two questions before do it automatically.
·         Do you want to rebuild particular items and its children?
·         Do you want to rebuild full indexing?

Rebuild Particular Item and its children

You can use Refresh() method that will take few minutes for execution:
/// <summary>
/// Rebuild Index
/// </summary>
/// <returns></returns>
public string RebuildIndex()
{
    string result = string.Empty;

    try
    {
        result = "Custom Index Rebuild Process Start : " + System.DateTime.Now.ToString() + System.Environment.NewLine;

        foreach (Sitecore.Caching.Cache cache in Sitecore.Caching.CacheManager.GetAllCaches())
        {
            cache.Clear();
        }

        result += "Cache completed : " + System.DateTime.Now.ToString() + System.Environment.NewLine;

        Sitecore.ContentSearch.ISearchIndex index = Sitecore.ContentSearch.ContentSearchManager.GetIndex("sitecore_web_index");
        if (index != null)
        {
            result += "Home Indexing : " + System.DateTime.Now.ToString() + System.Environment.NewLine;

            string homeID = "{97FF4F41-A5FD-4BC3-838F-6A01E0E26B18}";   //Home
            index.Refresh(new Sitecore.ContentSearch.SitecoreIndexableItem(Sitecore.Data.Database.GetDatabase("web").GetItem(new Sitecore.Data.ID(homeID))));

        }

        result += "Indexing completed : " + System.DateTime.Now.ToString() + System.Environment.NewLine;
    }
    catch (System.Exception ex)
    {
        result += ex.ToString() + System.Environment.NewLine;
    }

    Sitecore.Diagnostics.Log.Error(result, this);

    return result;
}
               
Rebuild Full Indexing

You can use Rebuild() method that will also take few minutes for execution.

Sitecore.ContentSearch.ISearchIndex index = Sitecore.ContentSearch.ContentSearchManager.GetIndex("sitecore_web_index");
if (index != null)
{
index.Rebuild(Sitecore.ContentSearch.IndexingOptions.ForcedIndexing);
}

What’s the difference between Rebuild() and Refresh() method?

The Refresh method allows you to update the full index or a part of it. The FullRebuild method deletes the index completely and builds it starting from the root.

The Refresh method also seems to be correct method to update the index, but if you need to rebuild the index completely, try Rebuild(Sitecore.ContentSearch.IndexingOptions.ForcedIndexing) - you should see the changes after as it forces the index rebuild.

Please leave your comments or share this trick if it’s useful for you.

No comments:

Post a Comment