Home » AWS » AWS CloudSearch – clear domain using PowerShell

AWS CloudSearch – clear domain using PowerShell

Amazon Web Services CloudSearch is a search engine and it’s offered in a SaaS model (as well as Elasticsearch what I’ve already explained here). I’m currently writing an article about it so I’ll skip the way how to set it up and how it works. Unfortunately, CloudSearch isn’t free. It doesn’t cost much if you take a look at everything what you get. For more information on pricing check here.

There’s few issues with CloudSearch. One of them is the fact that there’s no easy way to clear search domain. AWS CLI has no such feature. At StackOverflow, I managed to find scripts for Java, NodeJS, PHP or bash.

Clear search domain with PowerShell

On daily basis for scripting purposes I use PowerShell. That’s why I wanted to have a script which would quickly clear CloudSearch domain. However, the use of PowerShell with AWS requires AWS Tools for Windows PowerShell. You may get it here.

Assuming, you’ve got access to modify and query your domain, next step is to obtain Search Url and Document Url from AWS console and replace it in the script below.

$searchUrl = '[SEARCH_URL]'
$documentUrl = '[DOCUMENT_URL]'
$parser = 'Lucene'
$querySize = 500

function Get-DomainHits()
{
    (Search-CSDDocuments -ServiceUrl $searchUrl -Query "*:*" -QueryParser $parser -Size $querySize).Hits;
}

function Get-TotalDocuments()
{
    (Get-DomainHits).Found
}

function Delete-Documents()
{
    (Get-DomainHits).Hit | ForEach-Object -begin { $batch = '[' } -process { $batch += '{"type":"delete","id":' + $_.id + '},'} -end { $batch = $batch.Remove($batch.Length - 1, 1); $batch += ']' }
       
    Try
    {
        Invoke-WebRequest -Uri $documentUrl -Method POST -Body $batch -ContentType 'application/json'
    }
    Catch
    {
        $_.Exception
        $_.Exception.Message
    }
}

$total = Get-TotalDocuments
while($total -ne 0)
{
    Delete-Documents

    $total = Get-TotalDocuments

    Write-Host 'Documents left:'$total
    # Sleep for 1 second to give CS time to delete documents
    sleep 1
}

 

Then just run the script. Results on the screen (not yet finished):

AWS CloudSearch clear domain

You may find this script on GitHub.

Published by

Mateusz Pustelak

Technical Lead at Iglu.com, Software Developer with several years of commercial experience, TDD practitioner, DDD/CQRS fan.

Leave a Reply

Your email address will not be published. Required fields are marked *


*