1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Threading . Tasks ;
4+ using Sitecore . Search ;
5+ using Sitecore . ContentSearch ;
6+ using Sitecore . Diagnostics ;
7+ using Sitecore . SecurityModel ;
8+
9+ namespace Sitecore . Azure . Startup
10+ {
11+ /// <summary>
12+ /// Provides a way to rebuild all Search and ContentSearch indexes in Sitecore application for the Azure platform.
13+ /// </summary>
14+ public class SearchIndexRebuilder
15+ {
16+ /// <summary>
17+ /// Rebuilds all Search and ContentSearch asynchronous.
18+ /// </summary>
19+ public static async void RebuildAllIndexesAsync ( )
20+ {
21+ List < Task > tasksList = new List < Task > ( ) ;
22+
23+ RebuildSearchIndexesAsync ( SearchManager . Indexes , tasksList ) ;
24+ RebuildContentSearchIndexesAsync ( ContentSearchManager . Indexes , tasksList ) ;
25+
26+ await Task . WhenAll ( tasksList ) ;
27+ }
28+
29+ /// <summary>
30+ /// Rebuilds all Search and ContentSearch indexes.
31+ /// </summary>
32+ public static void RebuildAllIndexes ( )
33+ {
34+ RebuildSearchIndexes ( SearchManager . Indexes ) ;
35+ RebuildContentSearchIndexes ( ContentSearchManager . Indexes ) ;
36+ }
37+
38+ #region Private Methods
39+
40+ /// <summary>
41+ /// Rebuilds the Search indexes asynchronous.
42+ /// </summary>
43+ /// <param name="searchIndexList">The search index list.</param>
44+ /// <param name="taskLists">The task lists.</param>
45+ private static void RebuildSearchIndexesAsync ( IEnumerable < Index > searchIndexList , IList < Task > taskLists )
46+ {
47+ foreach ( var searchIndex in searchIndexList )
48+ {
49+ try
50+ {
51+ var index = searchIndex ;
52+
53+ Task rebuildIndexTask = Task . Run ( ( ) =>
54+ {
55+ using ( new SecurityDisabler ( ) )
56+ {
57+ index . Rebuild ( ) ;
58+ }
59+ } ) ;
60+
61+ taskLists . Add ( rebuildIndexTask ) ;
62+ }
63+ catch ( Exception exception )
64+ {
65+ var message = string . Format ( "Exception occurred during rebuilding the '{0}' Search index." , searchIndex . Name ) ;
66+ Log . Error ( message , exception , typeof ( SearchIndexRebuilder ) ) ;
67+ }
68+ }
69+ }
70+
71+ /// <summary>
72+ /// Rebuilds the ContentSearch indexes asynchronous.
73+ /// </summary>
74+ /// <param name="searchIndexList">The search index list.</param>
75+ /// <param name="taskLists">The task lists.</param>
76+ private static void RebuildContentSearchIndexesAsync ( IEnumerable < ISearchIndex > searchIndexList , IList < Task > taskLists )
77+ {
78+ foreach ( var searchIndex in searchIndexList )
79+ {
80+ try
81+ {
82+ var index = searchIndex ;
83+
84+ Task rebuildIndexTask = Task . Run ( ( ) =>
85+ {
86+ using ( new SecurityDisabler ( ) )
87+ {
88+ index . Rebuild ( ) ;
89+ }
90+ } ) ;
91+
92+ taskLists . Add ( rebuildIndexTask ) ;
93+ }
94+ catch ( Exception exception )
95+ {
96+ var message = string . Format ( "Exception occurred during rebuilding the '{0}' ContentSearch index." , searchIndex . Name ) ;
97+ Log . Error ( message , exception , typeof ( SearchIndexRebuilder ) ) ;
98+ }
99+ }
100+ }
101+
102+ /// <summary>
103+ /// Rebuilds the Search indexes.
104+ /// </summary>
105+ /// <param name="indexList">The index list.</param>
106+ private static void RebuildSearchIndexes ( IEnumerable < Index > indexList )
107+ {
108+ foreach ( var index in indexList )
109+ {
110+ using ( new SecurityDisabler ( ) )
111+ {
112+ try
113+ {
114+ index . Rebuild ( ) ;
115+ }
116+ catch ( Exception exception )
117+ {
118+ var message = string . Format ( "Exception occurred during rebuilding the '{0}' Search index." , index . Name ) ;
119+ Log . Error ( message , exception , typeof ( SearchIndexRebuilder ) ) ;
120+ }
121+ }
122+ }
123+ }
124+
125+ /// <summary>
126+ /// Rebuilds the ContentSearch indexes.
127+ /// </summary>
128+ /// <param name="indexList">The index list.</param>
129+ private static void RebuildContentSearchIndexes ( IEnumerable < ISearchIndex > indexList )
130+ {
131+ foreach ( var index in indexList )
132+ {
133+ using ( new SecurityDisabler ( ) )
134+ {
135+ try
136+ {
137+ index . Rebuild ( ) ;
138+ }
139+ catch ( Exception exception )
140+ {
141+ var message = string . Format ( "Exception occurred during rebuilding the '{0}' ContentSearch index." , index . Name ) ;
142+ Log . Error ( message , exception , typeof ( SearchIndexRebuilder ) ) ;
143+ }
144+ }
145+ }
146+ }
147+
148+ #endregion
149+ }
150+ }
0 commit comments