Different ways to rebuild and refresh Solr Indexes
Hello Everyone, I am back with another blog. This time we will discuss how we can rebuild our Solr indexes programmatically. What is the difference between Solr force index rebuild and Solr index refresh. How we can achieve Solr index refresh programmatically.
So let's start with the same without wasting any time.
We all are aware that we can rebuild our indexes from Sitecore control panel. Sitecore also provides the option to rebuild a tree instead of rebuilding whole index from ribbon. But if we want to rebuild our indexes programmatically the how we can do it. Also if we don't want to rebuild the whole index, is there any way programmatically we can refresh the index for specific path or folder.
Let's understand first, difference between the two first and see the code accordingly.
1. Sitecore Index Rebuild:
Whenever we do a rebuild index, Solr deletes the indexed files from its data folder and again recreates it from scratch.
2. Sitecore Index Refresh:
Whenever we do index refresh, instead of deleting indexed files from data folder, it just refreshes the changed data for the Sitecore items.
Let's now go for the code which can help us for the same.
Full Index rebuild
//Code to rebuild master And Web index rebuild manually
Sitecore.Diagnostics.Log.Info("Build Started for sitecore web and master Index", "");
Sitecore.ContentSearch.ISearchIndex index_web = Sitecore.ContentSearch.ContentSearchManager.GetIndex("web");
if (index_web != null)
{
index_web.Rebuild(Sitecore.ContentSearch.IndexingOptions.ForcedIndexing);
}
Sitecore.ContentSearch.ISearchIndex index_master = Sitecore.ContentSearch.ContentSearchManager.GetIndex("master");
if (index_master != null)
{
index_master.Rebuild(Sitecore.ContentSearch.IndexingOptions.ForcedIndexing);
}
Index refresh
Item item = "{19528344-1587-4BBC-8493-7A9928ED891F}"
//Code to refresh master And Web index rebuild manually
Sitecore.Diagnostics.Log.Info("Build Started for default master and Web index", "");
Sitecore.ContentSearch.ISearchIndex index_web = Sitecore.ContentSearch.ContentSearchManager.GetIndex("web");
if (index_web != null)
{
string itemid = item.ID.ToString();
index_web.Refresh(
new Sitecore.ContentSearch.SitecoreIndexableItem(Sitecore.Data.Database.GetDatabase("web").GetItem(new Sitecore.Data.ID(itemid))));
}
Sitecore.ContentSearch.ISearchIndex index_master = Sitecore.ContentSearch.ContentSearchManager.GetIndex("master");
if (index_master != null)
{
string itemid = item.ID.ToString();
index_master.Refresh(new Sitecore.ContentSearch.SitecoreIndexableItem(Sitecore.Data.Database.GetDatabase("master").GetItem(new Sitecore.Data.ID(itemid))));
}
If you note for index refresh code, the syntax is different from index rebuild. It take item id so that it can refresh the index for that item only.
Index refresh can be use for a scenario where we have some computed field which should be updated periodically. So we can create a job to refresh the index for the items, so that we can get updated values. We can update the indexes data for whole folder also like Articles,Blogs etc. We can give the folder path in job and by looping we can refresh the index of the required items.
Thanks for reading. Happy learning.
Comments
Post a Comment