Skip to content

Instantly share code, notes, and snippets.

@dperish
Created September 24, 2020 19:14
Show Gist options
  • Save dperish/87bd2d954beacd62e3df8f3264a04946 to your computer and use it in GitHub Desktop.
Save dperish/87bd2d954beacd62e3df8f3264a04946 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
- _ ___ _ ___ _ _
- /_\ ___ / __| ___ __ _ _ _ __| |_ | _ \___(_)_ _ __| |_____ _____ _ _
- / _ \ |_ / \__ \/ -_) _` | '_/ _| ' \ | / -_) | ' \/ _` / -_) \ / -_) '_|
- /_/ \_\/__| |___/\___\__,_|_| \__|_||_| |_|_\___|_|_||_\__,_\___/_\_\___|_|
.DESCRIPTION
Given an Azure search resource, this script will discover and reindex all
search indexes. This is necessary as non-soft-deletes do not remove data
from the indexes.
#>
param (
$ApiUri,
$ApiKey,
$ApiVersion = "2020-06-30"
)
$Headers = @{ "api-key" = $ApiKey }
#region Common
$StarLine = "*" * 80
function GetIndexNames {
$GetUri = "$ApiUri/indexes?api-version=$ApiVersion" + '&$select=name'
$Response = Invoke-WebRequest $GetUri `
-Method GET `
-ContentType "application/json" `
-Headers $Headers
$Json = $Response.content | ConvertFrom-Json
return $Json.value | foreach-object { ($_.name) }
}
function GetIndexerNames {
$GetUri = "$ApiUri/indexers?api-version=$ApiVersion" + '&$select=name'
$Response = Invoke-WebRequest $GetUri `
-Method GET `
-ContentType "application/json" `
-Headers $Headers
$Json = $Response.content | ConvertFrom-Json
return $Json.value | foreach-object { ($_.name) }
}
function RecreateSearchIndex {
param ($IndexName)
Write-Output $StarLine
Write-Output " RecreateSearchIndex: $IndexName"
Write-Output $StarLine
#region Get Index
$GetUri = $ApiUri + "/indexes/" + $IndexName + "?api-version=" + $ApiVersion
$Response = Invoke-WebRequest $GetUri `
-Method GET `
-ContentType "application/json" `
-Headers $Headers
Write-Output $Response
$StatusCode = $Response.StatusCode
$IndexJson = $Response.Content
if ($StatusCode -ne 200) {
return
}
#endregion
#region Delete Index
$delete_uri = $ApiUri + "/indexes/" + $IndexName + "?api-version=" + $ApiVersion
$Response = Invoke-WebRequest $delete_uri `
-Method DELETE `
-ContentType "application/json" `
-Headers $Headers
Write-Output $Response
#endregion
#region Create Index
$create_url = "$ApiUri/indexes?api-version=" + $ApiVersion
$Response = Invoke-WebRequest $create_url `
-Method POST `
-ContentType "application/json" `
-Headers $Headers `
-Body $IndexJson
Write-Output $Response
#endregion
}
function ResetAndRunIndexer {
param ($IndexerName)
Write-Output $StarLine
Write-Output " ResetAndRunIndexer: $IndexerName"
Write-Output $StarLine
$ResetUri = "$ApiUri/indexers/" + $IndexerName + "/reset?api-version=" + $ApiVersion
$RunUri = "$ApiUri/indexers/" + $IndexerName + "/run?api-version=" + $ApiVersion
$Response = Invoke-WebRequest $ResetUri `
-Method POST `
-ContentType "application/json" `
-Headers $Headers
Write-Output $Response
$Response = Invoke-WebRequest $RunUri `
-Method POST `
-ContentType "application/json" `
-Headers $Headers
Write-Output $Response
}
#endregion
foreach ($IndexName in GetIndexNames) {
RecreateSearchIndex $IndexName
}
foreach ($IndexerName in GetIndexerNames) {
ResetAndRunIndexer $IndexerName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment