Hace unos meses os contaba en este post como añadir sugerencias de consulta al motor de búsquedas de SharePoint 2013 a través de las opciones disponibles en la administración de las mismas. En este nuevo post sobre este tema, os voy a comentar como automatizar la importación de estas sugerencias por medio de PowerShell. Empecemos:
-
En primer lugar, preparamos un archivo de texto con las sugerencias a importar. Fijaros que en mi caso estoy especificando QuerySuggestion como cabecera para facilitar el procesado de las sugerencias que se definen a continuación.
-
Una vez que tenemos preparadas las sugerencias, lo siguiente que haremos es definir un pequeño script PowerShell para importarlas. Este Script, que os podéis descargar en How to import search query suggestions to the SharePoint Search, utiliza el comando New-SPEnterpriseSearchLanguageResourcePhrase para añadir las nuevas sugerencias de consultas en el ámbito de aplicación web (Nota: Se pueden añadir Sugerencias de Consultas en los ámbitos de Colección de Sitios y Sitio si se necesita). Cuando las sugerencias están importadas, es necesario forzar la ejecución del Timer Job “Prepare query suggestions” de manera que estén disponibles para que el usuario final pueda buscar con mayor facilidad información.
1: ############################################################################################################################################
2: # Script that allows to import query suggestions to the SharePoint Search.
3: # Required Parameters:
4: # ->$sInputfile: Query suggestions file.
5: # ->$sLanguage: Language for the Query Suggestions.
6: ############################################################################################################################################
7:
8: If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
9: { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
10:
11: $host.Runspace.ThreadOptions = "ReuseThread"
12:
13: #Definition of the function that imports new query suggestions to SharePoint
14: function Import-QuerySuggestions
15: {
16: param ($sInpuntFile,$sLanguage)
17: try
18: {
19: Write-Host "Importing Query Suggestions.." -ForegroundColor Green
20: #Checking if the query suggestions file exists
21: $bFileExists = (Test-Path $sInputFile -PathType Leaf)
22: if ($bFileExists) {
23: "Loading $sInputFile for processing..."
24: $tblData = Import-CSV $sInputFile
25: } else {
26: Write-Host "File $sInputFile not found. Stopping the Import Process!" -foregroundcolor Red
27: exit
28: }
29:
30: $ssaSearchApp = Get-SPEnterpriseSearchServiceApplication -Identity “Search Service App"
31: $spSearchOwner = Get-SPEnterpriseSearchOwner -Level SSA
32:
33: #Processing the file data
34: foreach ($row in $tblData){
35: $sQuerySuggestion=$row.QuerySuggestion.ToString()
36: Write-Host "Adding $sQuerySuggestion as a Query Suggestion"
37: New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $ssaSearchApp -Language $sLanguage -Type QuerySuggestionAlwaysSuggest -Name $sQuerySuggestion -Owner $spSearchOwner
38: }
39:
40: #Starting the Timer Job that makes available new query suggestions
41: $qsTimerJob = Get-SPTimerJob -type "Microsoft.Office.Server.Search.Administration.PrepareQuerySuggestionsJobDefinition"
42: Write-Host "Starting " $qsTimerJob.Name " Timber Job" -ForegroundColor Green
43: $qsTimerJob.RunNow()
44: Write-Host "Query Suggestions successfully imported!!" -ForegroundColor Green
45:
46: }
47: catch [System.Exception]
48: {
49: write-host -f red $_.Exception.ToString()
50: }
51: }
52:
53: Start-SPAssignment –Global
54: #Archivo con los Usuarios
55: $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
56: $sInputFile=$ScriptDir+ "\QuerySuggestions_Madrid.txt"
57: $sLanguage="ES-es"
58: Import-QuerySuggestions -sInpuntFile $sInputFile -sLanguage $sLanguage
59: Stop-SPAssignment –Global
60:
61: Remove-PSSnapin Microsoft.SharePoint.PowerShell
-
Una vez ejecutado el script anterior, podemos verificar de nuevo con PowerShell que las nuevas sugerencias estén disponibles. De nuevo, podéis descargaros el script correspondiente en How to get all query suggestions defined in a SharePoint Farm! Como veis, para obtener las sugerencias de consulta disponibles simplemente ejecutamos el comando Get-SPEnterpriseSearchQuerySuggestionsCandidates.
1: ############################################################################################################################################
2: # Script that allows to get all the query suggestions already defined in a SharePoint farm.
3: ############################################################################################################################################
4:
5: If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
6: { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
7:
8: $host.Runspace.ThreadOptions = "ReuseThread"
9:
10: #Definition of the function that gets current query suggestions in the SharePoint farm
11: function Get-QuerySuggestions
12: {
13: try
14: {
15: Write-Host "Getting all the query suggestions..." -ForegroundColor Green
16: $ssaSearchApp = Get-SPEnterpriseSearchServiceApplication -Identity “Search Service App"
17: $spSearchOwner = Get-SPEnterpriseSearchOwner -Level SSA
18: Get-SPEnterpriseSearchQuerySuggestionCandidates -SearchApplication $ssaSearchApp -Owner $spSearchOwner
19: }
20: catch [System.Exception]
21: {
22: write-host -f red $_.Exception.ToString()
23: }
24: }
25:
26: Start-SPAssignment –Global
27: Get-QuerySuggestions
28: Stop-SPAssignment –Global
29:
30: Remove-PSSnapin Microsoft.SharePoint.PowerShell
![]() |
![]() |
-
Finalmente, comprobamos en un sitio de búsqueda de SharePoint que las nuevas sugerencias están listas y disponibles para los usuarios.