one step closer to dev-ops

As a developer in 2017 being a great coder is no longer enough, we also need to be dev-ops that can handle the environments we are working with. On my current project we are using Azure for all services so when we selected search service, Azure Search, was the most natural choice. Creating indices in the azure portal is as easy as creating any other resource but in order to be able to clear an environment for e.g. testing we need to be able to clear any indices we have created so we can start testing with a fresh version of the system.

You can clear and create your search indices in several ways:

  • in the portal
  • in code
  • or PowerShell script

However only viable solution is to script this in order for it to be easy to run and repeatable. All our other services such as blob and table storage is cleared using PowerShell script so I was tasked with updating the scripts with clearing our indices.

This is the part of our script that handles the indices (modified slightly). To run this, stick it in a PowerShell .ps file.

note some of these commands require extending PowerShell. See Install and configure Azure PowerShell

function Get-SearchResource(){

Get-AzureRmResource `
    -ResourceType "Microsoft.Search/searchServices" `
    -ResourceGroupName $searchResourceGroup `
    -ResourceName $searchResourceName `
    -ApiVersion 2015-08-19

function Get-ApiKey(){

$searchResource = Get-SearchResource

(Invoke-AzureRmResourceAction `
    -Action listAdminKeys `
    -ResourceId $searchresource.ResourceId `
    -ApiVersion 2015-08-19).PrimaryKey

$searchResourceGroup = "ourresourcegroupname"
$searchResourceName = "oursearchname"

Write-Host "Delete search index..."

$primaryKey = Get-ApiKey

Write-Host "Got api key: " $primaryKey

$headers = @{ "api-key" = $primaryKey}

$url = "https://ourservicename-" + $environment + ""
& Invoke-WebRequest  -Method DELETE -Uri $url -ContentType "application/json" -Headers $headers

Write-Host "Create search index..."

$body = @'
         "retrievable": 'true'},
        {"name":'name',"type":'Edm.String',"filterable":'true',"sortable":'true',"facetable":'false', "retrievable": 'true'},
        {"name":'type',"type":'Edm.String',"filterable":'true',"sortable":'true',"facetable":'false', "retrievable": 'true'}        

& Invoke-WebRequest  -Method Put -Uri $url -Body $body -ContentType "application/json" -Headers $headers

The header must include your admin key for the search service otherwise you will receive a forbidden response.

$headers = @{ "api-key" = $primaryKey}

the api version must also be the same as the version in the portal.


Note that I am using the Put verb when creating the index, when using put Azure will create the index if it does not exist otherwise it will update it.

& Invoke-WebRequest  -Method Put -Uri $url -Body $body -ContentType "application/json" -Headers $headers

If you want to use Post, the url should be like this

$url = "https://ourservicename-" + $environment + ""

I.e you should not include the index name part.

Bonus snippet

If you have multiple environments then it would be nice to be able to tell which environment we are targeting. Simply put this at the top of your script, then it will prompt for an environment and a variable will be set with the current environment.

$requestedEnvironment = Read-Host -Prompt 'Input the instance you want to clear. Valid values DEV,TEST,UAT'

if ($requestedEnvironment -eq "DEV") {
  $environment = "dev"
elseif ($requestedEnvironment -eq "TEST") {
  $environment = "test"
elseif ($requestedEnvironment -eq "UAT") {
  $environment = "uat"
else {
  Write-Host "Invalid environment parameter:" $requestedEnvironment

Pin It on Pinterest

Share This

Share This

Share this post with your friends!