Recreate Search Service Application Components

Tested on: SharePoint Server 2016, SharePoint Server 2019

We had a strange issue with our Search Service Application, everything in the Central Administration was green but crawls were running forever without finding any items or throwing any error in the Crawl Log or the ULS log. We wanted to avoid rebuilding the whole Search Service application because there was another third party application that used the current SSA and it would have to be reconfigured as well. Also we did not want to lose the search index.

Articles I found on the internet advised me to create a clone of the active topology, remove the component(s) from the clone and add them back again to the clone and then activate it.

It did not work and did not really make much sense to me to remove components from an inactive cloned topology and then immediately add them back without activating it first. That way the components are not really removed from the servers where search service instances are running .

Nevertheless, this advice helped me to find my own solution. Here are some of the articles that helped me:

We had a simple topology with the same components running on two servers.

Click for the full size.

Some topologies might be more complex but the idea is the same for any topology – remove all the components from all the servers and then add them back again. Of course you cannot remove them all at once because it is not possible to apply an empty topology, so you basically have to shuffle the components between the servers and create multiple topologies. At the very end you apply the old topology again.

Topologies and components can only be managed via powershell. Here is a simple example of how you can fix the Search Application by re-creating the components.

Let’s define the constant variables first.

PowerShell
$Server1 = "YourServerName1"
$Server2 = "YourServerName1"
$SSAName = "Search Service Application"

Get the active Search Service Application and the active topology.

PowerShell
$SSA = Get-SPEnterpriseSearchServiceApplication -Identity $SSAName
$OldTopology = Get-SPEnterpriseSearchTopology -SearchApplication $SSA -Active

Creating two topologies by cloning the old one and saving the search components of each topology in variables.

PowerShell
$NewTopology1 = New-SPEnterpriseSearchTopology -SearchApplication $SSA -Clone -SearchTopology $OldTopology
$NewTopology2 = New-SPEnterpriseSearchTopology -SearchApplication $SSA -Clone -SearchTopology $OldTopology

$NewSearchComponents1 = Get-SPEnterpriseSearchComponent -SearchTopology $NewTopology1
$NewSearchComponents2 = Get-SPEnterpriseSearchComponent -SearchTopology $NewTopology2

Creating two lists of components which will be later removed from the corresponding topologies. The first list contains only components for Server1 and the second contains only components for Server2.

PowerShell
### Make lists of comopnents that will be removed from the topologies in the next steps
### Server2 is removed from the list, only Server1 is left.
$ComponentsForRemoval1 = $NewSearchComponents1 | where {$_.servername -ne $Server2}
### Server1 is removed from the list, only Server2 is left.
$ComponentsForRemoval2 = $NewSearchComponents2 | where {$_.servername -ne $Server1}

Deleting Server1 components in NewTopology1 so it will contain only Server2 components and then deleting Server2 Components in NewTopology2 so it will only contain components on Server1.

PowerShell
#Remove Server1 components from Topology1
foreach ($SearchCompontent in $ComponentsForRemoval1) {
    Remove-SPEnterpriseSearchComponent -Identity $SearchCompontent.ComponentId.Guid -SearchTopology $NewTopology1 -Confirm:$false
}

#Remove Server2 components from Topology2
foreach ($SearchCompontent in $ComponentsForRemoval2 ) {
    Remove-SPEnterpriseSearchComponent -Identity $SearchCompontent.ComponentId.Guid -SearchTopology $NewTopology2 -Confirm:$false
}

Now we have one active and two new inactive topologies.


Here comes the part when we start activating the topologies. Creating and deleting topologies is not affecting anything, activation is the important part !

I activated NewTopology1 and then I tried to activate NewTopology2 but the activation of the second topology failed. So I applied the old topology again and then activated NewTopology2. That way it worked.

PowerShell
#Activate the NewTopology1 - This will remove all the components from the Server1
Set-SPEnterpriseSearchTopology -Identity $NewTopology1

#Apply the old topology. It will add the components back to the Server1. If you skip this step you will probably not be able to apply $NewTopology2
Set-SPEnterpriseSearchTopology -Identity $OldTopology

This is what it looks like when a new topology is being applied. It can easily take more than 10 minutes to remove all the components.

Click for the full size.

Now do the same thing with the second topology.

PowerShell
#Activate the NewTopology2 - This will remove all the components from the Server2
Set-SPEnterpriseSearchTopology -Identity $NewTopology2

#Apply the old topology again. It will add the components back to the server2.
Set-SPEnterpriseSearchTopology -Identity $OldTopology

Make sure to check in Central Administration that a the old topology is applied again! Then you can remove the unused topologies.

PowerShell
#Remove the old inactive Topologies
$InactiveTopologies = Get-SPEnterpriseSearchTopology -SearchApplication $SSA | where {$_.State -eq "Inactive"}
$InactiveTopologies | Remove-SPEnterpriseSearchTopology -Confirm:$false

Once the components have been removed you might have to stop all crawls and start a full crawl again. Ideally you stop them before you start applying new topologies. Do not suspend the SSA. Applying a new topology is not possible if the search application is suspended.

Click the copy button to get the whole code here. I recommend to run it in sections via ISE.

PowerShell
Add-PSSnapin Microsoft.Sharepoint.Powershell

### Define the constant variables according to your environment
$Server1 = "BC-SPQA-APP-1"
$Server2 = "BC-SPQA-APP-2"
$SSAName = "Search Service Application"

### Get the search service application
$SSA = Get-SPEnterpriseSearchServiceApplication -Identity $SSAName
### Get the active topology
$OldTopology = Get-SPEnterpriseSearchTopology -SearchApplication $SSA -Active

### Create two new topologies
$NewTopology1 = New-SPEnterpriseSearchTopology -SearchApplication $SSA -Clone -SearchTopology $OldTopology
$NewTopology2 = New-SPEnterpriseSearchTopology -SearchApplication $SSA -Clone -SearchTopology $OldTopology

### Get the list of all search components for $NewTopology1
$NewSearchComponents1 = Get-SPEnterpriseSearchComponent -SearchTopology $NewTopology1
### Get the list of all search components for $NewTopology2
$NewSearchComponents2 = Get-SPEnterpriseSearchComponent -SearchTopology $NewTopology2

### Make lists of comopnents that will be removed from the topologies in the next steps
### Server2 is removed from the list, only Server1 is left.
$ComponentsForRemoval1 = $NewSearchComponents1 | where {$_.servername -ne $Server2}
### Server1 is removed from the list, only Server2 is left.
$ComponentsForRemoval2 = $NewSearchComponents2 | where {$_.servername -ne $Server1}

### Remove Server1 components from Topology1
foreach ($SearchCompontent in $ComponentsForRemoval1) {
    Remove-SPEnterpriseSearchComponent -Identity $SearchCompontent.ComponentId.Guid -SearchTopology $NewTopology1 -Confirm:$false
}

### Remove Server2 components from Topology2
foreach ($SearchCompontent in $ComponentsForRemoval2 ) {
    Remove-SPEnterpriseSearchComponent -Identity $SearchCompontent.ComponentId.Guid -SearchTopology $NewTopology2 -Confirm:$false
}

### Display all the Topologies Again
Get-SPEnterpriseSearchTopology -SearchApplication $SSA

### Display the components in the new new topologies
Write-Host -ForegroundColor Yellow "Components in NewTopology1"
Get-SPEnterpriseSearchComponent -SearchTopology $NewTopology1 | select name,componentid,servername |ft

Write-Host -ForegroundColor Yellow "Components in NewTopology2"
Get-SPEnterpriseSearchComponent -SearchTopology $NewTopology2 | select name,componentid,servername |ft

### Activation ###!!!

### Activate the NewTopology1 - This will remove all the components from the Server1
Set-SPEnterpriseSearchTopology -Identity $NewTopology1

### Apply the old topology. It will add the components back to the Server1. If you skip this step you will probably not be able to apply $NewTopology2
Set-SPEnterpriseSearchTopology -Identity $OldTopology

### Activate the NewTopology2 - This will remove all the components from the Server2
Set-SPEnterpriseSearchTopology -Identity $NewTopology2

### Apply the old topology again. It will add the components back to the server2.
Set-SPEnterpriseSearchTopology -Identity $OldTopology

### Remove the old inactive Topologies
$InactiveTopologies = Get-SPEnterpriseSearchTopology -SearchApplication $SSA | where {$_.State -eq "Inactive"}
$InactiveTopologies | Remove-SPEnterpriseSearchTopology -Confirm:$false

Potential Issues

This method helped me several times to fix different issues without having to rebuild the SSA or the Search Index. The index stays intact. I was applying topologies left and right until I got this right and it did not break the SSA more than it already was so it seems to be a safe thing to try. Of course, I recommend to do it after business hours on the production systems.

Leave a Reply

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