Update Sitecore Index
I am working on Sitecore 7.2 and using Lucene search, I have created some templates and pages and the search is working fine, now I want to exclude several templates from the index, I have a custom crawler and it removes templates from the indexes, but the index is not updated, I use following code to update the index
foreach (Cache cache in CacheManager.GetAllCaches())
{
//WriteLog(string.Concat(" Clearing Cache, name = ", cache.Name));
cache.Clear();
}
//WriteLog("Clearing caching finished");
var index = ContentSearchManager.GetIndex("sitecore_global_index");
index.RebuildAsync(IndexingOptions.ForcedIndexing, new System.Threading.CancellationToken(false));
index.Reset();
//index.Refresh();
source to share
The simplest way to programmatically rebuild an index is:
Sitecore.ContentSearch.ContentSearchManager.GetIndex("Your_Index_Name").Rebuild();
This GetIndex () method has been refactored in the ContentSearchManager, previously contained in this class:
Sitecore.Search.SearchManager
.. this has created problems for some people, so it's good to keep in mind.
If you want to know the names of the indexes (programs) (say you want to iterate over them), extract them from:
ContentSearchManager.Indexes
Hope it helps, let us know if you have any further questions.
Hooray!
source to share
Since you are trying to use an async method, you are using it incorrectly. Use the prefix "await".
**await** index.RebuildAsync(IndexingOptions.ForcedIndexing, new System.Threading.CancellationToken(false));
It works for me.
protected async void RebuildIndexAsync()
{
foreach (Cache cache in CacheManager.GetAllCaches())
{
//WriteLog(string.Concat(" Clearing Cache, name = ", cache.Name));
cache.Clear();
}
//WriteLog("Clearing caching finished");
var index = ContentSearchManager.GetIndex("sitecore_global_index");
await index.RebuildAsync(IndexingOptions.ForcedIndexing, new System.Threading.CancellationToken(false));
index.Reset();
//index.Refresh();
}
source to share