|
5 | 5 | using Microsoft.SharePoint.Client.Search.Query;
|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.Linq;
|
| 8 | +using System.Threading; |
8 | 9 |
|
9 | 10 | namespace PnP.PowerShell.Commands.Search
|
10 | 11 | {
|
@@ -85,6 +86,9 @@ public class SubmitSearchQuery : PnPWebCmdlet
|
85 | 86 | [Parameter(Mandatory = false, ParameterSetName = ParameterAttribute.AllParameterSets)]
|
86 | 87 | public SwitchParameter RelevantResults;
|
87 | 88 |
|
| 89 | + [Parameter(Mandatory = false, ParameterSetName = ParameterAttribute.AllParameterSets)] |
| 90 | + public int RetryCount = 0; |
| 91 | + |
88 | 92 | internal IEnumerable<object> Run()
|
89 | 93 | {
|
90 | 94 | int startRow = StartRow;
|
@@ -121,53 +125,79 @@ internal IEnumerable<object> Run()
|
121 | 125 | keywordQuery.QueryText += " IndexDocId>" + lastDocId;
|
122 | 126 | }
|
123 | 127 |
|
124 |
| - var searchExec = new SearchExecutor(ClientContext); |
125 |
| - var results = searchExec.ExecuteQuery(keywordQuery); |
126 |
| - ClientContext.ExecuteQueryRetry(); |
127 |
| - |
128 |
| - if (results.Value != null) |
| 128 | + // We'll always try at least once, even if RetryCount is 0 (default) |
| 129 | + for (var iterator = 0; iterator <= RetryCount; iterator++) |
129 | 130 | {
|
130 |
| - if (finalResults == null) |
| 131 | + try |
131 | 132 | {
|
132 |
| - finalResults = (PnPResultTableCollection)results.Value; |
133 |
| - foreach (ResultTable resultTable in results.Value) |
| 133 | + var searchExec = new SearchExecutor(ClientContext); |
| 134 | + var results = searchExec.ExecuteQuery(keywordQuery); |
| 135 | + ClientContext.ExecuteQueryRetry(); |
| 136 | + |
| 137 | + if (results.Value != null) |
134 | 138 | {
|
135 |
| - if (resultTable.TableType == "RelevantResults") |
| 139 | + if (finalResults == null) |
136 | 140 | {
|
137 |
| - currentCount = resultTable.RowCount; |
138 |
| - if (currentCount > 0) |
| 141 | + finalResults = (PnPResultTableCollection)results.Value; |
| 142 | + foreach (ResultTable resultTable in results.Value) |
139 | 143 | {
|
140 |
| - lastDocId = resultTable.ResultRows.Last()["DocId"].ToString(); |
| 144 | + if (resultTable.TableType == "RelevantResults") |
| 145 | + { |
| 146 | + currentCount = resultTable.RowCount; |
| 147 | + if (currentCount > 0) |
| 148 | + { |
| 149 | + lastDocId = resultTable.ResultRows.Last()["DocId"].ToString(); |
| 150 | + } |
| 151 | + } |
141 | 152 | }
|
142 | 153 | }
|
143 |
| - } |
144 |
| - } |
145 |
| - else |
146 |
| - { |
147 |
| - // we're in paging mode |
148 |
| - foreach (ResultTable resultTable in results.Value) |
149 |
| - { |
150 |
| - PnPResultTable pnpResultTable = (PnPResultTable)resultTable; |
151 |
| - var existingTable = finalResults.SingleOrDefault(t => t.TableType == resultTable.TableType); |
152 |
| - if (existingTable != null) |
153 |
| - { |
154 |
| - existingTable.ResultRows.AddRange(pnpResultTable.ResultRows); |
155 |
| - } |
156 | 154 | else
|
157 | 155 | {
|
158 |
| - finalResults.Add(pnpResultTable); |
159 |
| - } |
160 |
| - if (pnpResultTable.TableType == "RelevantResults") |
161 |
| - { |
162 |
| - currentCount = resultTable.RowCount; |
163 |
| - if (currentCount > 0) |
| 156 | + // we're in paging mode |
| 157 | + foreach (ResultTable resultTable in results.Value) |
164 | 158 | {
|
165 |
| - lastDocId = resultTable.ResultRows.Last()["DocId"].ToString(); |
| 159 | + PnPResultTable pnpResultTable = (PnPResultTable)resultTable; |
| 160 | + var existingTable = finalResults.SingleOrDefault(t => t.TableType == resultTable.TableType); |
| 161 | + if (existingTable != null) |
| 162 | + { |
| 163 | + existingTable.ResultRows.AddRange(pnpResultTable.ResultRows); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + finalResults.Add(pnpResultTable); |
| 168 | + } |
| 169 | + if (pnpResultTable.TableType == "RelevantResults") |
| 170 | + { |
| 171 | + currentCount = resultTable.RowCount; |
| 172 | + if (currentCount > 0) |
| 173 | + { |
| 174 | + lastDocId = resultTable.ResultRows.Last()["DocId"].ToString(); |
| 175 | + } |
| 176 | + } |
166 | 177 | }
|
167 | 178 | }
|
168 | 179 | }
|
| 180 | + |
| 181 | + // If we were successful (and didn't end in the catch block), we don't want to retry -> break out of retry loop |
| 182 | + break; |
169 | 183 | }
|
| 184 | + // If we're not retrying, or if we're on the last retry, don't catch the exception |
| 185 | + catch (Exception ex) |
| 186 | + { |
| 187 | + if (RetryCount > 0 && iterator < RetryCount) |
| 188 | + { |
| 189 | + var waitTime = 5 * (iterator + 1); |
170 | 190 |
|
| 191 | + WriteVerbose($"Search operation failed with exception {ex.Message}. Attempt {iterator + 1} out of {RetryCount}. Retrying in {waitTime} seconds."); |
| 192 | + |
| 193 | + Thread.Sleep(TimeSpan.FromSeconds(waitTime)); |
| 194 | + continue; |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + throw; |
| 199 | + } |
| 200 | + } |
171 | 201 | }
|
172 | 202 | startRow += rowLimit;
|
173 | 203 | } while (currentCount == rowLimit && All.IsPresent);
|
|
0 commit comments