Query Suggestions in FAST Search for SharePoint 2010 (FS4SP)
Adding query suggestions to your search solution is a feature that can contribute to a better search experience for end-users. First of all, it makes us save time as we don’t have to type in the entire query. Secondly, it can help the user avoid potential spelling errors, which in turn can reduce the quality of the results, and avoid that the user has to spend more time finding the information he/she is looking for. Also, it can aid the user to do the first search more specific, by adding information that may be pertinent to the search.
With FS4SP, these query suggestions are automatically created over a period of time based on how often the users search for a specific query within a certain timeframe. Sometimes, it can however make sense to modify the query suggestions manually – and to help us do that, we can always lean on PowerShell.
The script I have implemented, has the following parameters:
1 |
.\QuerySuggestions.ps1 -file [fileName] -action [add|delete] -queryssa [querySSAName] |
where file is the file with the query suggestion phrases (one per line), action decides if the query suggestions should be added or deleted, queryssa is the name of the Query SSA. And now for the actual code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#Get input param ( [string]$file, [string]$action, [string]$queryssa ) function LoadQueryCompletion() { # Read input file with stuff that you should add to the Query suggestion list if (test-path $file) { log VERBOSE "Reading file: $file" $input = Get-Content $file } else { log ERROR "File noe found. Exiting script." exit 1 } # Get SSA search application $searchapp = Get-SPEnterpriseSearchServiceApplication -Identity $queryssa # Populating a dictionary with the query suggestions that have already been added before $query_suggestions = Get-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest $already_dict = @{} foreach ($term in $query_suggestions) { if (!$term){ continue } $already_dict.Add($term.Phrase,$term.Phrase) } if ($action -eq "add") { foreach ($entry in $input) { if ($already_dict.ContainsKey($entry)) { log WARNING "Key already added: $entry" } else { try { log VERBOSE "Adding entry ($entry) with language En-US" $nooutput = new-spenterprisesearchlanguageresourcephrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Name $entry $already_dict.Add($entry,$entry) }catch { log ERROR "Could not add entry ($entry). Exiting script." exit 1 } } } } elseif ($action -eq "delete") { foreach ($entry in $input) { try { if ($already_dict.ContainsKey($entry)) { log VERBOSE "Removing entry ($entry) with language En-US" $nooutput = remove-spenterprisesearchlanguageresourcephrase -SearchApplication $searchapp -Language En-Us -Type QuerySuggestionAlwaysSuggest -Identity $entry -Confirm:$false $already_dict.Remove($entry) }else{ log WARNING "Entry ($entry) has not been added as a query suggestion" } }catch { log ERROR "Could not remove entry ($entry)." } } } else { log ERROR "-action parameter contains an invalid value. Exiting script." } #Run timerjob manually to upload entries from DB Start-SPTimerJob -Identity "prepare query suggestions" log VERBOSE "Finished loading query completion phrases." } #Main $spInstalled = Get-PSSnapin | Select-String Sharepoint if (!$spInstalled) { Add-PSSnapin Microsoft.Sharepoint.PowerShell } LoadQueryCompletion |
If you want to include the logging output , add this to the top of the PowerShell script.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Log level to color mapping $lc = @{ VERBOSE="green"; WARNING="yellow"; ERROR="red"; MESSAGE="blue"; } function log([string] $level, [string] $message) { $date = get-date -uformat "%G-%m-%d %T" write-host "[$date] " -nonewline write-host -foregroundcolor $lc[$level] $level.padright(7) -nonewline write-host " $message" } |
If you’re really picky (like me), you should also consider having some kind of validation on the input parameters. For instance verifying that the file exists, that the action parameter contains a valid value, and that the Query SSA exists. You can check the last-mentioned by adding the following lines of code to the script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ssaExists = Get-SPEnterpriseSearchServiceApplication | Select-String $queryssa if ($ssaExists -eq $null) { log ERROR "-queryssa is invalid. Search Service Application does not exist. Exiting script." $queryssas = Get-SPEnterpriseSearchServiceApplication | Where-Object {$_.DefaultSearchProvider -eq "FASTSearch"} foreach ($queryssa in $queryssas) { if ($queryssa) { $name = $queryssa.Name log VERBOSE "Found the following Query SSA: $name" } } } |
After doing that, you should be ready to run the script on the SharePoint Admin server.
Afterwards, go to your FAST Search Center, and start typing one of the terms you have added. The result can look something like in the screenshot below.