问题
I am working on Sitecore 7.2 and using Lucene search, I have created few templates and pages and search is working fine, now I want to exclude few templates from the index, I have a custom crawler and that does remove the templates from indexes but index is not refreshed, I am using following code to update 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();
回答1:
The following code worked for me before, It will index the content tree starting from RootItemInTree and below:
index.Refresh(new SitecoreIndexableItem(RootItemInTree));
回答2:
The simplest way to rebuild index programmatically is:
Sitecore.ContentSearch.ContentSearchManager.GetIndex("Your_Index_Name").Rebuild();
This GetIndex() method has been refactored into ContentSearchManager, previously it was kept in this class:
Sitecore.Search.SearchManager
..for some people it has made issues, so good to keep in mind.
In case you want to find out index name(s) programmatically (let's say you'd like to iterate through them), retrieve them from:
ContentSearchManager.Indexes
Hope this helps, please let us know if you got further questions.
Cheers!
回答3:
Since you are trying to use Asynchronous method, you are using it incorrectly. Use "await" prefix.
**await** index.RebuildAsync(IndexingOptions.ForcedIndexing, new System.Threading.CancellationToken(false));
It is working 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();
}
来源:https://stackoverflow.com/questions/31419638/sitecore-refresh-index