Sonntag, 20. November 2011

Query Search (FAST) with PowerShell

A nice and easy way to send queries to you SharePoint Search Server (in this example FAST Search Server) using PowerShell. Of cause this is not an option getting results to work with if your scope is the content. But it is a very useful way getting statistics, summaries etc, Have a look at some property details or something else.
First we have to do is calling the needed .net class and create an object based on it:
#Calling KeywordQuery class
$searchSite = "http://intranet"
$site = New-Object Microsoft.SharePoint.SPSite $searchSite
$vc =
New-Object Microsoft.Office.Server.Search.Query.KeywordQuery $site
$vc.ResultsProvider = [Microsoft.Office.Server.Search.Query.SearchProvider]::FASTSearch
$vc.ResultTypes = [Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults
In my case I enabled the FQL syntax and set some other parameters:
$vc.EnableFQL = $true # enable FQL
$vc.RowLimit = 2 # sets the limit of results
$vc.StartRow = 0 # 0 is the default

Next step is the query, in my case a simple query searching for the term “backup”

#Query / Result
$vc.QueryText = "backup"
$results = $vc.Execute()
write-host "Ergebiss:"  -F red
$results

Print out the results details:

#result details
$resultTable = $results.Item([Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults)
$rows = $resultTable.Table.Rows

And getting the properties of each result:

#Each result with properties
write-host "Details:"  -F red
$rows

This is just interesting and nice, especially getting all the property from the “HitHighlightedSummary” and the “HitHighlighted” Property fields. But bases on this script, you can also do some other useful things, for example: finding out with filetyps that are in the resultset and how they are distributed

$searchSite = "http://intranet"
$site = New-Object Microsoft.SharePoint.SPSite $searchSite
$vc =
New-Object Microsoft.Office.Server.Search.Query.KeywordQuery $site
$vc.ResultsProvider = [Microsoft.Office.Server.Search.Query.SearchProvider]::FASTSearch
$vc.ResultTypes = [Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults
$vc.RowLimit = 1000
# Query String: the "%%" means to search everything, the "-aspx -bdc3 –file" filters to near only documents
$vc.QueryText = "%% -aspx -bdc3 -file"
$results = $vc.Execute()
$resultTable = $results.Item([Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults)
$rows = $resultTable.Table.Rows
$rows| Group-Object FileExtension| Sort-Object count -descending |Format-Table -AutoSize -Property Name,count,Size
$rows| Measure-Object Size  -Average -Sum -Maximum –Minimum

Output looks like:

Name    Count
----    -----
HTML       53
DOC        42
DOCX       20
XLSX       12
PPTX       10
PPSDC       4
VDW         3
XAP         2
XLS         2
ONETOC2     2
XML         2
VSD         2
TXT         1
            1
ONE         1


Count    : 157
Average  : 201299,133757962
Sum      : 31603964
Maximum  : 9711217
Minimum  : 0
Property : Size

Be careful with the “$vc.RowLimit” parameter, just getting the first 1000 or let’s say 10000 entities gives you a good overview and won’t stress you SearchServer too much…
Have fun playing around with it.

PS: Script based on a post from Harald S. Fianbakken, I corrected some syntax error and add some own ideas etc…

4 Kommentare: