NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

4

SharePoint 2013 - Manage Search Query Rule using Powershell
by Balamurugan Kailasam
Apr 24, 2013
Following the Search Application Provisioning and Search Result Source configuration post, this post outlines how to configure Search Query rules in the SharePoint 2013.
Summary:
Initialize and get an instance of the Search Service Application
Update the existing Search Result Source (Site collection level)
Create a new Search Query rule
Set the rule condition and bind it to the Search Result source
Set the Query condition action to sort by "FileName" in descending order.
Search Results
Assumption:
Search Service Application up and running
Publishing Site with some test pages (testpage1.aspx and testpage2.aspx) and a Search Result Source.
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"
$SiteUrl = "http://win-2j3idcdeuh6/"
$RootSiteCollection = Get-SPSite $SiteUrl -ErrorAction SilentlyContinue
#-----------------------------------------------------
# Get the Search Service application
#-----------------------------------------------------
$SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue
$FederationManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($SearchServiceApplication)


Update the existing Search Result Source
Note: The below script sets the existing Result Source to a default one and without any filters.
#--------------------------------------------------------------------------
# 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.
#--------------------------------------------------------------------------
$SearchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner –ArgumentList @([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::SPSite,$RootSiteCollection.RootWeb)
$ResultSourceName ="Test Result Source"
$Query = "{searchTerms} Title:*"
$ResultSource = $FederationManager.GetSourceByName($ResultSourceName,$SearchOwner)
if($ResultSource)
{
#-----------------------------------------------------------
# Update the existing query property.
#-----------------------------------------------------------
$ResultSource.CreateQueryTransform($queryProperties, $query)
$ResultSource.Activate()
$ResultSource.Commit()
#-----------------------------------------------------------
# Update the new Search source as a default one...
#-----------------------------------------------------------
$FederationManager.UpdateDefaultSource($ResultSource.Id,$SearchOwner)
}
else
{
Write-Host "Result Source : $ResultSourceName does not exist".
}


Create a new Search Query rule
Note: The below script creates a new query rule for the search term "test"
#-------------------------------------------------------------------
# Configure a Query Rule to sort by Filename
#-------------------------------------------------------------------
$QueryRuleName = "Test - Sort by File name"
$QueryRuleConditionTerm = "test"
$QueryRuleManager = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryRuleManager($SearchServiceApplication)
# Create a search object filter using a $SearchOwner object (Site collection level - in this case)
$SearchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($SearchOwner)
$QueryRules = $QueryRuleManager.GetQueryRules($SearchObjectFilter)
ForEach($Rule in $QueryRules)
{
if($Rule.DisplayName -eq $QueryRuleName)
{
Write-Host "Query Rule : $QueryRuleName already exist. Deleting..."
$QueryRules.RemoveQueryRule($Rule)
break
}
}
$QueryRules = $QueryRuleManager.GetQueryRules($SearchObjectFilter)
# Create a new rule as a active one.
$QueryRule = $QueryRules.CreateQueryRule($QueryRuleName,$null,$null,$true)

Set the rule condition and bind it to the Search Result source
# Set the Query Rule condition...
[string[]] $QueryRuleTerms = @($QueryRuleConditionTerm)
$QueryRuleConditions = $QueryRule.QueryConditions
$QueryRuleCondition = $QueryRuleConditions.CreateKeywordCondition($QueryRuleTerms,$true)
#Bind it to the Result Source...
$QuerySourceContextCondition = $QueryRule.CreateSourceContextCondition($ResultSource)
Set the Query condition to sort by "FileName" in descending order.
# Set the Query Condition action to change ranked results...
$QueryRuleAction = $QueryRule.CreateQueryAction([Microsoft.Office.Server.Search.Query.Rules.QueryActionType]::ChangeQuery)
$QueryRuleAction.QueryTransform.OverrideProperties = new-object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties
$QueryRuleAction.QueryTransform.SourceId = $ResultSource.Id

# define a custom sorting - Order by FileName
$QueryRuleSortCollection = New-Object Microsoft.Office.Server.Search.Query.SortCollection
$QueryRuleSortCollection.Add("FileName", [Microsoft.Office.Server.Search.Query.SortDirection]::Descending)

$QueryRule.ChangeQueryAction.QueryTransform.OverrideProperties["SortList"] = [Microsoft.Office.Server.Search.Query.SortCollection]$QueryRuleSortCollection
$QueryRule.ChangeQueryAction.QueryTransform.QueryTemplate = "{searchTerms}"

$QueryRule.Update()
Search Results
Step1 : Go to the site (http://win-2j3idcdeuh6/) and search for "test". The search result brings all the test pages (testpage2.aspx and testpage1.aspx) in a descending order.

Step 2: Deactivate the rule and search for "test" in the search screen

Step 3: Search for "test" again and displays the results in the default order.

The full script can be downloaded from here SP2013%20-%20Manage%20Query%20rule.ps1
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.