Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry logic to Submit-PnPSearchQuery #3528

Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added GCC support for `Get-PnPAzureADUser` , `Add-PnPFlowOwner` , `Remove-PnPFlowOwner`, `Sync-PnPSharePointUserProfilesFromAzureActiveDirectory`, `New-PnPAzureADUserTemporaryAccessPass` and `Get-PnPAvailableSensitivityLabel` cmdlets. [#3484](https://github.com/pnp/powershell/pull/3484)
- Added a devcontainer for easily building a minimal environment necessary to contribute to the project. [#3497](https://github.com/pnp/powershell/pull/3497)
- Added `-RelativeUrl` parameter to `Connect-PnPOnline` cmdlet to allow specifying custom URLs for usage with `-WebLogin` method. [#3530](https://github.com/pnp/powershell/pull/3530)
- Added `-RetryCount` to `Submit-PnPSearchQuery` which allows for specifying the number of retries to perform when an exception occurs [#3528](https://github.com/pnp/powershell/pull/3528)
- Added `-MailNickname` parameter to `Set-PnPMicrosoft365Group` cmdlet to allow changing of this property on a Microsoft 365 Group [#3529](https://github.com/pnp/powershell/pull/3529)

### Fixed
Expand Down Expand Up @@ -85,6 +86,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Contributors

- Antti K. Koskela [koskila]
- Christian Veenhuis [ChVeen]
- Kunj Balkrishna Sangani [kunj-sangani]
- Antti K. Koskela [koskila]
Expand Down
33 changes: 29 additions & 4 deletions documentation/Submit-PnPSearchQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Submit-PnPSearchQuery [-Query] <String> [-StartRow <Int32>] [-MaxResults <Int32>
[-RankingModelId <String>] [-ClientType <String>] [-CollapseSpecification <String>]
[-HiddenConstraints <String>] [-TimeZoneId <Int32>] [-EnablePhonetic <Boolean>] [-EnableStemming <Boolean>]
[-EnableQueryRules <Boolean>] [-SourceId <Guid>] [-ProcessBestBets <Boolean>]
[-ProcessPersonalFavorites <Boolean>] [-RelevantResults] [-Connection <PnPConnection>]
[-ProcessPersonalFavorites <Boolean>] [-RelevantResults] [-Connection <PnPConnection>] [-RetryCount <Int32>] [-Verbose]

```

Expand All @@ -34,7 +34,7 @@ Submit-PnPSearchQuery [-Query] <String> [-All] [-TrimDuplicates <Boolean>] [-Pro
[-CollapseSpecification <String>] [-HiddenConstraints <String>] [-TimeZoneId <Int32>]
[-EnablePhonetic <Boolean>] [-EnableStemming <Boolean>] [-EnableQueryRules <Boolean>] [-SourceId <Guid>]
[-ProcessBestBets <Boolean>] [-ProcessPersonalFavorites <Boolean>] [-RelevantResults]
[-Connection <PnPConnection>]
[-Connection <PnPConnection>] [-RetryCount <Int32>] [-Verbose]
```

## DESCRIPTION
Expand Down Expand Up @@ -353,6 +353,20 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -RetryCount
How many times to retry for a failed query. Default is 0 (no retries). Will wait 5 seconds between each retry.

```yaml
Type: Int32
Parameter Sets: (All)

Required: False
Position: Named
Default value: 0
Accept pipeline input: False
Accept wildcard characters: False
```

### -SelectProperties
The list of properties to return in the search results, separated by a comma. I.e. ComplianceTag,InformationProtectionLabelId.

Expand Down Expand Up @@ -437,9 +451,20 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -Verbose
When provided, additional debug statements will be shown while executing the cmdlet.

```yaml
Type: SwitchParameter
Parameter Sets: (All)

## RELATED LINKS
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
94 changes: 62 additions & 32 deletions src/Commands/Search/SubmitSearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.SharePoint.Client.Search.Query;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace PnP.PowerShell.Commands.Search
{
Expand Down Expand Up @@ -85,6 +86,9 @@ public class SubmitSearchQuery : PnPWebCmdlet
[Parameter(Mandatory = false, ParameterSetName = ParameterAttribute.AllParameterSets)]
public SwitchParameter RelevantResults;

[Parameter(Mandatory = false, ParameterSetName = ParameterAttribute.AllParameterSets)]
public int RetryCount = 0;

internal IEnumerable<object> Run()
{
int startRow = StartRow;
Expand Down Expand Up @@ -121,53 +125,79 @@ internal IEnumerable<object> Run()
keywordQuery.QueryText += " IndexDocId>" + lastDocId;
}

var searchExec = new SearchExecutor(ClientContext);
var results = searchExec.ExecuteQuery(keywordQuery);
ClientContext.ExecuteQueryRetry();

if (results.Value != null)
// We'll always try at least once, even if RetryCount is 0 (default)
for (var iterator = 0; iterator <= RetryCount; iterator++)
{
if (finalResults == null)
try
{
finalResults = (PnPResultTableCollection)results.Value;
foreach (ResultTable resultTable in results.Value)
var searchExec = new SearchExecutor(ClientContext);
var results = searchExec.ExecuteQuery(keywordQuery);
ClientContext.ExecuteQueryRetry();

if (results.Value != null)
{
if (resultTable.TableType == "RelevantResults")
if (finalResults == null)
{
currentCount = resultTable.RowCount;
if (currentCount > 0)
finalResults = (PnPResultTableCollection)results.Value;
foreach (ResultTable resultTable in results.Value)
{
lastDocId = resultTable.ResultRows.Last()["DocId"].ToString();
if (resultTable.TableType == "RelevantResults")
{
currentCount = resultTable.RowCount;
if (currentCount > 0)
{
lastDocId = resultTable.ResultRows.Last()["DocId"].ToString();
}
}
}
}
}
}
else
{
// we're in paging mode
foreach (ResultTable resultTable in results.Value)
{
PnPResultTable pnpResultTable = (PnPResultTable)resultTable;
var existingTable = finalResults.SingleOrDefault(t => t.TableType == resultTable.TableType);
if (existingTable != null)
{
existingTable.ResultRows.AddRange(pnpResultTable.ResultRows);
}
else
{
finalResults.Add(pnpResultTable);
}
if (pnpResultTable.TableType == "RelevantResults")
{
currentCount = resultTable.RowCount;
if (currentCount > 0)
// we're in paging mode
foreach (ResultTable resultTable in results.Value)
{
lastDocId = resultTable.ResultRows.Last()["DocId"].ToString();
PnPResultTable pnpResultTable = (PnPResultTable)resultTable;
var existingTable = finalResults.SingleOrDefault(t => t.TableType == resultTable.TableType);
if (existingTable != null)
{
existingTable.ResultRows.AddRange(pnpResultTable.ResultRows);
}
else
{
finalResults.Add(pnpResultTable);
}
if (pnpResultTable.TableType == "RelevantResults")
{
currentCount = resultTable.RowCount;
if (currentCount > 0)
{
lastDocId = resultTable.ResultRows.Last()["DocId"].ToString();
}
}
}
}
}

// If we were successful (and didn't end in the catch block), we don't want to retry -> break out of retry loop
break;
}
// If we're not retrying, or if we're on the last retry, don't catch the exception
catch (Exception ex)
{
if (RetryCount > 0 && iterator < RetryCount)
{
var waitTime = 5 * (iterator + 1);

WriteVerbose($"Search operation failed with exception {ex.Message}. Attempt {iterator + 1} out of {RetryCount}. Retrying in {waitTime} seconds.");

Thread.Sleep(TimeSpan.FromSeconds(waitTime));
continue;
}
else
{
throw;
}
}
}
startRow += rowLimit;
} while (currentCount == rowLimit && All.IsPresent);
Expand Down