add or remove search component in sharpoint 2013


Recently I faced an issue where one of the search index partition failed to become active and was in Degraded state for long time.  after lot of effort on trying to fix it, I decided to remove the bad partition and add it again. 

I have to clone the existing search topology and then remove the bad component and then add it again.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea 0 
#set Search Service Application
$ssa = Get-SPEnterpriseSearchServiceApplication
$ssa.pause()

# Check the status of the service application in a new Powershell windoe.
$ssa.IsPaused() -ne 0

#recreate search topology with new index component
#reset the index if the bad component is the index

#grab component name of faulty topology component and it's respective server name
Get-SPEnterpriseSearchStatus -SearchApplication $ssa -Text

#put server name where [SERVER] is.. remove the []
$hosta = get-spenterprisesearchserviceinstance -identity "USALSECS076"

#get the existing topology
$active = Get-SPEnterpriseSearchTopology -SearchApplication $ssa -Active

#copy the existing topology
$clone = New-SPEnterpriseSearchTopology -SearchApplication $ssa -Clone -SearchTopology $active

Get-SPEnterpriseSearchComponent -SearchTopology $clone

#remove the bad component, placing the name where [BAD COMPONENT NAME] is... remove the []
$ComponentID = (Get-SPEnterpriseSearchComponent -SearchTopology $clone -Identity "IndexComponent4").componentID
Remove-SPEnterpriseSearchComponent -Identity $ComponentID.GUID -SearchTopology $clone -confirm:$false

#create the new component, the example below is for the index specifically - create a new folder for the index so it's empty!
New-SPEnterpriseSearchIndexComponent -SearchTopology $clone -SearchServiceInstance $hostA -IndexPartition 0

#Set this copy as the active topology
Set-SPEnterpriseSearchTopology -identity $clone

$active 
#Note the ID of the Topology and then Remove it.
#Remove-SPEnterpriseSearchTopology -Identity 9c1a5c17-b470-436f-bb8c-dc73ec3eaeda -SearchApplication $ssa

$ssa.resume()
#or
$ssa.ResumeAfterIndexRepartitioning()