Search Suggestion is one of the cool features available in the SharePoint 2010 and SharePoint 2013. The search query will automatically appear in the search box drop down, when the user clicks the search result at-least 6 times in the search result page.
In this post, we will see how to add or import the search suggestion terms manually using the Powershell cmdlets.
Requirements:
- Search Service Application up and running
- Set the “Local SharePoint Results” as a default Search Result source on both SiteCollection and Site level.
Initialize and get an instance of the Search Service Application
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" } $SearchServiceApplicationName = "Search Application" $SearchServiceApplicationProxyName = "Search Service Application Proxy 1" $SiteUrl = "http://win-2j3idcdeuh6/" $RootSiteCollection = Get-SPSite $SiteUrl -ErrorAction SilentlyContinue #----------------------------------------------------- # Get the Search Service application #----------------------------------------------------- $SearchServiceApplicationProxy = Get-SPEnterpriseSearchServiceApplicationProxy -Identity $SearchServiceApplicationProxyName $SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue $FederationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($SearchServiceApplicationProxy) #-------------------------------------------------------------------------- # The below line creates a Search Object owner at the site collection level # and this can be changed to Search Application or Site level by passing # different SearchObjectLevel argument. #-------------------------------------------------------------------------- $RootSiteCollection = Get-SPSite $SiteUrl -ErrorAction SilentlyContinue $SearchOwner = Get-SPEnterpriseSearchOwner -Level SPWeb -SPWeb $RootSiteCollection.RootWeb
Set the default Search Result source
$ResultSourceName ="Local SharePoint Results" $ResultSource = $FederationManager.GetSourceByName($ResultSourceName,$SearchOwner) if($ResultSource) { #----------------------------------------------------------- # Update the new Search source as a default one... #----------------------------------------------------------- $ResultSource = $FederationManager.UpdateDefaultSource($ResultSource.Id,$SearchOwner) } else { Write-Host "Result Source : $ResultSourceName does not exist". }
Add a new set of Search Phrase
# Get the existing phrase list... $PhraseList = Get-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $SearchServiceApplication -Owner $SearchOwner -SourceId $ResultSource.Id # Remove all the existing phrase for the give search result resource ... $PhraseList | Remove-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $SearchServiceApplication -Type QuerySuggestionAlwaysSuggest -Language "en-us" -Owner $SearchOwner -SourceId $ResultSource.Id # Add the new phrase... New-SPEnterpriseSearchLanguageResourcePhrase -Name "SharePoint Community" -SearchApplication $SearchServiceApplication -Type QuerySuggestionAlwaysSuggest -Language "en-us" -Owner $SearchOwner -SourceId $ResultSource.Id New-SPEnterpriseSearchLanguageResourcePhrase -Name "SharePoint Search" -SearchApplication $SearchServiceApplication -Type QuerySuggestionAlwaysSuggest -Language "en-us" -Owner $SearchOwner -SourceId $ResultSource.Id New-SPEnterpriseSearchLanguageResourcePhrase -Name "SharePoint Search suggestion" -SearchApplication $SearchServiceApplication -Type QuerySuggestionAlwaysSuggest -Language "en-us" -Owner $SearchOwner -SourceId $ResultSource.Id
Import a new set of Search Phrase
# Import the popular queries from the given txt file... Import-SPEnterpriseSearchPopularQueries -SearchApplicationProxy $SearchServiceApplicationProxy -Filename "E:\work\SharePoint\Search\SP2013 - QuerySuggestion.txt" -ResultSource $ResultSource -Web $RootSiteCollection.RootWeb -Verbose
Note: The import file contains Query terms, Query count, Click count and the LCID. The display order in the suggestion list is determined by the “Query count” and the “Click count”.
example : “SharePoint Community – Txt File,1100,1180,1033”
Run the “PrepareQuerySuggestionsJobDefinition” timer job
$TimerJob = Get-SPTimerJob -type "Microsoft.Office.Server.Search.Administration.PrepareQuerySuggestionsJobDefinition" $TimerJob.RunNow()
Search Suggestion UI
Search Suggestion Client / Database
The Search control makes a ajax call (/_vti_bin/client.svc/ProcessQuery) which in turn calls the “proc_MSS_GetQuerySuggestions” stored procedure (“MSSQLogSearchCounts” table – stores the terms and Query and Click Count) to get the Search suggestion.
The full search suggestion configuration script and the text file can be downloaded from here.