How To Import Search Query Suggestions using PowerShell!

Undoubtedly, SharePoint search is one of the most powerful features available in the platform not only for the out of the box features but also for the multiple extensibility points provided. In this post we will see how we can improve the search user experience through search query suggestions imported in SharePoint using the power provided by PowerShell.

  • Before going ahead with all the PowerShell staff, we need to define a file with all the query suggestions we want to add to the SharePoint Search. In my case, I have created a simple text file with all these suggestions. As you can see in the file structure, I’m using the QuerySuggestion term as a header in order to simplify the data processing required to extract each suggestion from the file.

image

  • Once we have ready the query suggestions, we need to define the PowerShell code that will import them in the SharePoint Search. This script, which is ready from download in How to import search query suggestions to the SharePoint Search, uses the cmdlet New-SPEnterpriseSearchLanguageResourcePhrase to add each Query Suggestion in a specific Scope (Search Service Application, Site Collection or Site). Finally, in order to provide the query suggestions to final users it’s required to execute the “Prepare query suggestions” Timer Job.

#################################################### # Script that allows to import query suggestions to the SharePoint Search. # Required Parameters: # ->$sInputfile: Query suggestions file. # ->$sLanguage: Language for the Query Suggestions. #################################################### If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } $host.Runspace.ThreadOptions = “ReuseThread” #Definition of the function that imports new query suggestions to SharePoint function Import-QuerySuggestions { param ($sInpuntFile,$sLanguage) try { Write-Host “Importing Query Suggestions..” -ForegroundColor Green #Checking if the query suggestions file exists $bFileExists = (Test-Path $sInputFile -PathType Leaf) if ($bFileExists) { “Loading $sInputFile for processing…” $tblData = Import-CSV $sInputFile } else { Write-Host “File $sInputFile not found. Stopping the Import Process!” -foregroundcolor Red exit } $ssaSearchApp = Get-SPEnterpriseSearchServiceApplication -Identity “Search Service App” $spSearchOwner = Get-SPEnterpriseSearchOwner -Level SSA #Processing the file data foreach ($row in $tblData){ $sQuerySuggestion=$row.QuerySuggestion.ToString() Write-Host “Adding $sQuerySuggestion as a Query Suggestion” New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $ssaSearchApp -Language $sLanguage -Type QuerySuggestionAlwaysSuggest -Name $sQuerySuggestion -Owner $spSearchOwner } #Starting the Timer Job that makes available new query suggestions $qsTimerJob = Get-SPTimerJob -type “Microsoft.Office.Server.Search.Administration.PrepareQuerySuggestionsJobDefinition” Write-Host “Starting ” $qsTimerJob.Name ” Timber Job” -ForegroundColor Green $qsTimerJob.RunNow() Write-Host “Query Suggestions successfully imported!!” -ForegroundColor Green } catch [System.Exception] { write-host -f red $_.Exception.ToString() } } Start-SPAssignment –Global #Archivo con los Usuarios $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path $sInputFile=$ScriptDir+ “\QuerySuggestions_Madrid.txt” $sLanguage=”ES-es” Import-QuerySuggestions -sInpuntFile $sInputFile -sLanguage $sLanguage Stop-SPAssignment –Global Remove-PSSnapin Microsoft.SharePoint.PowerShell image

  • Of course, you can verify that all the query suggestions has been imported using again the magic and power of Windows PowerShell. In this case, I have prepared a small script that you can get again in the TechNet Script Gallery ( How to get all query suggestions defined in a SharePoint Farm!. As you can see in the script, you only need to properly call the Get-SPEnterpriseSearchQueryCantidates in order to get all query suggestions available in the SharePoint farm.

################################################### # Script that allows to get all the query suggestions already defined in a SharePoint farm. #################################################### If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } $host.Runspace.ThreadOptions = “ReuseThread” #Definition of the function that gets current query suggestions in the SharePoint farm function Get-QuerySuggestions { try { Write-Host “Getting all the query suggestions…” -ForegroundColor Green $ssaSearchApp = Get-SPEnterpriseSearchServiceApplication -Identity “Search Service App” $spSearchOwner = Get-SPEnterpriseSearchOwner -Level SSA Get-SPEnterpriseSearchQuerySuggestionCandidates -SearchApplication $ssaSearchApp -Owner $spSearchOwner } catch [System.Exception] { write-host -f red $_.Exception.ToString() } } Start-SPAssignment –Global Get-QuerySuggestions Stop-SPAssignment –Global Remove-PSSnapin

 

Microsoft.SharePoint.PowerShell image

 

  • Finally, just verify your query suggestions are ready to use in a SharePoint Search Center.

image

And that’s all about how to import Search Query Suggestions to SharePoint using PowerShell. Happy CloudSharing!