Testing Custom Query Features (FAST Search Server 2010 for SharePoint)

You can test query related features by using a simple Windows PowerShell script.

This makes it easier to troubleshoot configuration errors, and determine whether the error is in the FAST Search Server 2010 for SharePoint farm configuration or in the Web Part configuration.

Applies to: SharePoint Server 2010

Windows PowerShell Script to Run a Query by Using the KeywordQuery Interface

The following Windows PowerShell script runs a query via the KeywordQuery interface, and returns selected query result data. The script is useful for the following test purposes:

  • Verifying that a managed property contains the expected content

  • Verifying that correct query refinement data is returned with the query result

  • Testing custom relevancy ranking

  • Testing advanced query features when using the FAST Query Language (FQL)

You can save the following example code in a .ps1 file and run it from the SharePoint 2010 Management Shell.

function usage($cmd) {
    write-host ""
    write-host "Simple FQL query tester."
    write-host "Returns first matching items including content for a custom"
    write-host "managed property, and associated query refinement data."
    write-host "Useful for testing ranking and custom refiners."
    write-host ""
    write-host "usage: $cmd <URL> <Hits> <Property> <Query>"
    write-host "  <URL>              - Specifies the URL for the query front-end"
    write-host "                       web server of the SharePoint farm."
    write-host "  <Hits>             - Number of hits to return."
    write-host "  <Property>         - Specifies the name of a managed property."
    write-host "                       Prints the content of the property and"
    write-host "                       associated query refiner data."
    write-host "                       Use lowercase only, even if the property name"
    write-host "                       contains uppercase letters."
    write-host "  <Query>            - The query string in FQL syntax."
    write-host "                       Use single quotes if it contains spaces or"
    write-host "                       double quotes."
    exit
}

# argument checks
switch ($args.count) {
    4 { $siteurl = $args[0]
        $hits = $args[1]
        $managedPropertyName = $args[2]
        $query = $args[3] }
    default { usage $myinvocation.mycommand.definition }
}

# The refiner has the same name as the managed property. 
$refiner = $managedPropertyName
$site = new-Object Microsoft.SharePoint.SPSite $siteUrl

# Use the KeywordQuery interface:
$kq = new-Object Microsoft.Office.Server.Search.Query.KeywordQuery $site
$kq.ResultsProvider = [Microsoft.Office.Server.Search.Query.SearchProvider]::FASTSearch
$kq.QueryText = $query
$kq.EnableFQL = 1

# Return title, url and the new managed property:
$props = $kq.SelectProperties
$cnt = $props.Add($managedPropertyName)
$cnt = $props.Add("title")
$cnt = $props.Add("url")
$cnt = $props.Add("rank")

# Number of hits to return:
$kq.RowLimit = $hits
$kq.ResultTypes = [Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults -bor [Microsoft.Office.Server.Search.Query.ResultType]::RefinementResults
$kq.Refiners = $refiner
$rtc = $kq.Execute()

Write-Host ""
Write-Host -ForegroundColor Yellow "Search Result data"
Write-Host ""
Write-Host "Backend time        :" $rtc.DatabaseTime "ms"
Write-Host "Elapsed time        :" $rtc.ElapsedTime "ms"
Write-Host "Query terms         :" $rtc.QueryTerms

if ($rtc.Exists([Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults))
{
    $crt = $rtc.Item([Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults)
    $cdt = $crt.Table
    Write-Host "Results returned    :" $crt.Table.Rows.Count 
    Write-Host "Results total       :" $crt.TotalRows
    Write-Host ""
    Write-Host -ForegroundColor Yellow "Managed Property Value"
    $cdt.Rows | Format-Table -autosize -property title, url, rank, $managedPropertyName
}

if ($rtc.Exists([Microsoft.Office.Server.Search.Query.ResultType]::RefinementResults))
{
    $rrt = $rtc.Item([Microsoft.Office.Server.Search.Query.ResultType]::RefinementResults)
    $rdt = $rrt.Table
    Write-Host ""
    if($rdt.Rows.Count -gt 0)
    {
        Write-Host -ForegroundColor Yellow "Refinements"
        $rdt.Rows | Format-Table -autosize -property RefinerName, RefinementName, RefinementToken, RefinementCount
    }
    else
    {
        write-Host -ForegroundColor Red "No refinements found for refiner " $refiner
    }
}

Note

The script accepts queries specified by using the FAST Query Language (FQL) Syntax. For basic property filters (property:value) and single term queries, the syntax is similar to the queries you type in any SharePoint Server 2010 query box.

See Also

Concepts

Debugging Custom Item Processing