Friday, 2 July 2010

Add pre-query search suggestions for SharePoint 2010 with a CSV file and PowerShell

The pre-query search suggestions feature (“find-as-you-type” functionality in the Search Center) automatically populates a list of keywords using previously searched terms by users. Obviously, this may take a while to compile, so it is a good idea to pre-populate a list of keywords from a CSV file (providing you know what the keywords are of course). This PowerShell script does exactly that. The format of the CSV file must have the word “Suggestion” on the top line and then your keyword list – for example:

Suggestion
SharePoint
SharePoint Server 2010
Sharing Files
SharePoint Designer
SharePoint Foundation

The script itself looks like this:

#Set up default variables
$csvfile="C:\Install\SearchSuggestions.csv"
$ssa = Get-SPEnterpriseSearchServiceapplication -Identity "Search Service Application"

#Create Lists from each item in CSV file
$csvData = Import-Csv $csvfile
foreach ($line in $csvData)
{
    New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $ssa -Language en-US -Type QuerySuggestionAlwaysSuggest -Name $line.Suggestion
}

$timerJob = Get-SPTimerJob "Prepare query suggestions"
$timerJob.RunNow()

Note that the we also run the “Prepare query suggestions” Timer job – which normally runs daily – to prepare these suggestions for use in search.

15 comments:

  1. Great job :-)

    Benjamin

    ReplyDelete
  2. Wonderful, A big Thanks

    ReplyDelete
  3. This code is not working, use below code

    get-content "C:\Install\SearchSuggestions.csv" | foreach-object { New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication "Fast Query" -Language en-US -Type QuerySuggestionAlwaysSuggest -Name $_ }

    ReplyDelete
  4. Srikanth - Thanks for the info, but my original published script has worked for me a number of times on different implementations and it seems like it also worked for the other guys who commented above!

    ReplyDelete
  5. # If you have already populated a TermStore with the terms you want to add to the query suggestions use this code

    #function for a TermSet having one level of Terms
    function SetTermToSuggestion($TermStoreName,$TermGroupName,$TermSetName,$theTermStores,$searchapplication)
    {
    foreach($TermStore in $theTermStores)
    {
    if ($TermStore.Name -eq $TermStoreName)
    {
    $TermStoreGroups = $TermStore.Groups
    foreach($TermStoreGroup in $TermStoreGroups)
    {
    #the operator -eq is case insensitive
    if ($TermStoreGroup.Name -eq $TermGroupName)
    {
    $TermSets = $TermStoreGroup.TermSets
    foreach($TermSet in $TermSets)
    {
    if ($TermSet.Name -eq $TermSetName)
    {
    $Terms = $TermSet.Terms
    foreach ($Term in $Terms)
    {
    #this works when there is only one level of terms
    #get the default label of the Term object
    $TermNameToAdd = $Term.Name
    #then run the command to add the term to the suggestion list
    New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapplication -Language en-us -Type QuerySuggestionAlwaysSuggest -Name $TermNameToAdd
    }
    }
    }
    }
    }
    }
    }
    }

    #function for a TermSet having more than one level of Terms
    function SetSubTermSuggestion($TermStoreName,$TermGroupName,$TermSetName,$theTermStores,$searchapplication)
    {
    foreach($TermStore in $theTermStores)
    {
    #the operator -eq is case insensitive
    if ($TermStore.Name -eq $TermStoreName)
    {
    $TermStoreGroups = $TermStore.Groups
    foreach($TermStoreGroup in $TermStoreGroups)
    {
    if ($TermStoreGroup.Name -eq $TermGroupName)
    {
    $TermSets = $TermStoreGroup.TermSets
    foreach($TermSet in $TermSets)
    {
    if ($TermSet.Name -eq $TermSetName)
    {
    #first level of terms
    $Terms = $TermSet.Terms
    foreach ($Term in $Terms)
    {
    #second level of terms
    $subTerms = $Term.Terms
    foreach ($subTerm in $subTerms)
    {
    #third level of terms : the one to be added to the query suggestions
    $subSubTerms = $subTerm.Terms
    foreach ($subSubTerm in $subSubTerms)
    {
    #get the default label of the Term object
    $TermNameToAdd = $subSubTerm.Name
    #then run the command to add the term to the suggestion list
    New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $searchapplication -Language en-us -Type QuerySuggestionAlwaysSuggest -Name $TermNameToAdd
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }

    #Main

    # Set the root web site URL

    $websiteurl = ""

    #get all the TermStores

    $TaxSession = Get-SPTaxonomySession -Site $websiteurl
    $TermStores = $TaxSession.TermStores

    #get the search application

    $searchapp = Get-SPEnterpriseSearchServiceApplication -Identity ""

    #loop to add all the Terms of the TermSet taken from the TermStore to the query suggestion

    SetTermSuggestion "" "" "" $TermStores $searchapp

    # or if you're trying to add sub terms from a sub level of a TermSet (here the sample code get the third level only)

    #loop to add all the subTerms of the TermSet taken from the TermStore to the query suggestion

    SetSubTermSuggestion "" "" "" $TermStores $searchapp

    #launch the default timer job to add the query suggestions

    Start-SPTimerJob -Identity "prepare query suggestions"

    # End of Main, enjoy !

    ReplyDelete
  6. obviously you will have to put values in between the ""for the root URL, the TermSet Name, the TermGroup Name and the TermStore Name

    an example :

    $websiteurl = "http://myrootwebsite"
    SetTermSuggestion "myTermStoreName" "myTermGroupName" "myTermSetName" $TermStores $searchapp

    ReplyDelete