I was digging through some new PowerShell cmdlets for one of my talks coming up at SharePoint Conference 2012 (SPC12), and I stumbled upon the new cmdlet Import-SPEnterpriseSearchPopularQueries. I found this cmdlet to be interesting since it can be used to easily seed what shows up in the search suggestions on a given site. I took the example there and did some tweaking and thought I would share my results.
We first, need to get a reference to the Search Service Application Proxy using Get-SPEnterpriseSearchServiceApplicationProxy. Sorry, the cmdlets for search a bit wordy so that is what the Tab key is for. :)
$ssap = Get-SPEnterpriseSearchServiceApplicationProxy
Search suggestions are actually specific to the result source and site that hosts the search box. This means you could have different suggestions depending on which part of the site you are on. To achieve that, we need to get a reference to the web hosting the search center and ultimately a result source object. Let’s get the web first. Change the URL to match your server’s search center.
$searchWeb = Get-SPWeb -Identity http://myserver/search
Now, we will use the Federation Manager to get a result source. However, we need a Search Object Owner first. The example on TechNet requires a lot more typing. I luckily found the cmdlet Get-SPSearchObjectOwner. It takes a scope (Level) and an SPWeb object.
$owner = Get-SPEnterpriseSearchOwner -Level SPWeb -SPWeb $searchWeb
Unfortunately, I couldn’t find a command to directly get a result source so that means we actually have to go through the API to get a Federation Manager first.
$mgr = new-object Microsoft.Office.Server.Search.Administration.Query.FederationManager -ArgumentList $ssap
Once we have the manager object, we use the GetSourceByName method passing it the name of the result source and $owner. In this case, we use “Local SharePoint Results" as the result source name.
$source = $mgr.GetSourceByName("Local SharePoint Results", $owner)
As far as the query suggestions themselves, we will need to create a CSV file with the data. The format is simple: Query Text,Query Count,Click Count,LCID. For example:
Setting these values allows you to control the order in which items appear in the query suggestions. For suggestions to appear in the search box, the click count must be at least 5. 1033 is the LCID value for English. Here is what my file looks like:
Now, we run the Import-SPEnterpriseSearchPopularQueries cmdlet. It needs the SearchApplicationProxy, Filename, and ResultSource parameters which we retrieved with the previous lines of script.
Import-SPEnterpriseSearchPopularQueries -SearchApplicationProxy $ssap -Filename .\suggestions.csv -ResultSource $source -Web $searchWeb
If all commands execute successfully, you should not see anything returned at the command prompt. Now, you can begin using your suggestions right away. You do not need to recrawl. Go to the search center that you specified in your $searchWeb object. Start typing some of the values from your suggestions and you will see them beneath the search box.
This new PowerShell cmdlet makes it so easy to seed your search suggestions. I think I will start recommending this on new deployments to really improve the immediate value of search. Try it out today.