Share via


POC - Part 2b: PowerShell: Configuring the connector/Indexing Content

The POC - Part 2 post was quite underwhelming. The VHD has all the software installed for us and stealing using content off the web was easy enough (well, maybe not that easy. I ended up writing a program to download files for me as I got tired of right-clicking and selecting Save Target As… ). As you noticed if you walked the process with the rest of us setting up a basic file crawl is pretty straightforward .

I hear you ask: but how would I do that in PowerShell? What if my GUI stops working and I just have to get my Fast Search Center created, and content source created and configured?

I am so glad you asked. Leo Souza wasn't that happy as I asked him and he basically gave me the information I am about to transcribe. Aren't I lucky to have such a good friend?

What we did in Part 2 breaks down into 3 parts:

  1. Install the Advanced Filter Pack. Since we did that in PowerShell we have already crossed that line.
  2. Add the SharePoint PowerShell cmdlets
  3. Create the Fast Search Center
  4. Create and configure a Content Source
  5. Start the crawl
  6. Execute a search

Remember to open up a PowerShell window from the Start menu:

Start --> Fast Search Server 2010 for SharePoint (right click --> Run as Administrator)

Now let's do each of the above steps in turn.

  1. Install the Advanced Filter Pack

    1. From the PowerShell window:

    > cd ..\installer\scripts

    > .\AdvancedFilterPack.ps1 -enable

    [answer y to the only question it asks]

    There is no need to restart the machine or any of the services.

  2. Add the SharePoint PowerShell cmdlets

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

  3. Create the Fast Search Center

    $template = Get-SPWebTemplate "SRCHCENTERFAST#0" # Want to know more? Go to https://technet.microsoft.com/en-us/library/ff678213.aspx

    New-SPWeb -Url "https://intranet.contoso.com/fast-search" -Name "TechNet VM Fast Search Page"-Template $template

  4. Create and configure a Content Source

    $contentSSA = "FASTContent"

    $startaddress = "\\VBOXSVR\neurobiology-files"

    $contentsourcename = "File system crawl"

       

    $contentsource = New-SPEnterpriseSearchCrawlContentSource -SearchApplication $contentSSA -Type file -name $contentsourcename -StartAddresses $startaddress

  5. Start the crawl

    $contentsource.StartFullCrawl()

    $contentsource.CrawlStatus

    1. Execute $contentsource.CrawlStatus again.
    2. Wait. Wait. Wait. Wait.
    3. Execute $contentsource.CrawlStatus again.
    4. Wait. Wait. Wait. Wait.
    5. Keep executing $contentsource.CrawlStatus until the status changes to CrawlCompleting and then Idle
  6. Execute a search

    $searchSite = Get-SPWeb https://intranet.contoso.com/fast-search

    $keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery $searchSite

    $keywordQuery.ResultTypes = [Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults

     

    $keywordQuery.EnableFQL = 1

    $keywordQuery.RowLimit = 10

       

    $keywordQuery.QueryText = "#"

       

    $results = $keywordQuery.Execute()

       

    Write-Host Total hits: $results[1].TotalRows

    $resultsTable = $results.Item([Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults)

       

    $resultsDataTable = $resultsTable.Table

    $resultsDataTable | Format-Table -AutoSize -Property title, url

 

So by rights you should create two versions of the script:

  1. One that executes the code generically with things like the Fast Search Center URL, file location and content source name as incoming arguments
  2. A script that calls the generic script from step 1 with the proper values for the URL, file location and content source name.

The separation gives you the flexibility to use the first script in lots of situations and the second one to execute a particular task without all typing. Get to it!

If something doesn't work please let me know. You'd be surprised where I am at any given time writing this blog (and this time I am at TechReady14 sitting in a Starbucks making sure this goes out in one piece.).

References and Thanks

Create a new FAST Search Center Site Collection using PowerShell by Brent Groom (thanks, Brent!)

FAST Search for SharePoint 2010–Using PowerShell to execute search by Satish TK (Thanks, Satish!)

Testing Custom Query Features (FAST Search Server 2010 for SharePoint) (oh, author of unknown origin: thanks!)

Scripted deployment reference (SharePoint Foundation 2010) 

The code

[Not sure how to run these scripts? Check out Running Windows PowerShell Scriptsat TechNet. You want to make them more useful? Slice and dice them even more. I won't be doing that...]

CreateContosoFastSearchCenter.ps1

CreateAFastSearchCenter.ps1 "https://intranet.contoso.com/fast-search" "TechNet VM Fast Search Page" "CONTOSO\Administrator"

CreateAFastSearchCenter.ps1

# CreateAFastSearchCenter.ps1 <Site URL> <Page Title>

function usage($cmd) {
write-host ""
write-host "Create a FAST Search Center"
write-host "Create a FAST Search Center using the incoming URL and name"
write-host ""
write-host "usage: $cmd <Site URL> <Page Title> <Owner Alias>"
write-host " <Site URL> - Specifies the new FULL URL for the site (for example, https://intranet.contoso.com/fast-search)"

   write-host " <Page Title> - The title to give this search center"

   write-host " <Owner Alias> - DOMAIN\alias of the site owner"

   exit
}

# argument checks
switch ($args.count) {
3 { $siteurl = $args[0]
$pageTitle = $args[1] }

   default { usage $myinvocation.mycommand.definition }
}

 

# Put this here or execute it before you run the script

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

  

$template = Get-SPWebTemplate "SRCHCENTERFAST#0"

   

write-host "Creating the Fast Search Center at" $siteurl

New-SPWeb -Url $siteurl -Name $pageTitle-Template $template

write-host "Finished!"

   

ConfigureContosoFileFASTContentSourceAndStartAFullCrawl.ps1

ConfigureAFileFASTContentSourceAndStartAFullCrawl.ps1 "File system crawl" "\\VBOXSVR\neurobiology-files"

   

ConfigureAFileFASTContentSourceAndStartAFullCrawl.ps1

# ConfigureAFileFASTContentSourceAndStartAFullCrawl.ps1 <ContentSourceName> <FileSystemStartAddress>

   

function usage($cmd) {

   write-host ""

   write-host "Configure A File System Content Source And Start A Full Crawl"

   write-host ""

   write-host "usage: $cmd <ContentSourceName> <FileSystemStartAddress>"

   write-host " <ContentSourceName> - Name to give this content source"

   write-host " <FileSystemStartAddress> - Starting point for the file system crawl"

   

   exit

}

# argument checks

switch ($args.count) {

   2 { $contentSourceName = $args[0]

       $startAddress = $args[1] }

   default { usage $myinvocation.mycommand.definition }

}

   

# Put this here or execute it before you run the script

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

   

$contentSource = New-SPEnterpriseSearchCrawlContentSource -SearchApplication "FASTContent" -Type file -name $contentSourceName -StartAddresses $startAddress

   

$contentsource.StartFullCrawl()

$contentsource.CrawlStatus

$sleepTime = 30

while ($true)

{

   Write-Host "Current status: " $contentsource.CrawlStatus

   if ($contentsource.CrawlStatus -eq "Idle")

   {

     break

   }

 

   write-host "Will check status again in" $sleepTime "seconds..."

   Start-Sleep -seconds $sleepTime

}

   

write-host "Finished!"

   

ExecuteContosoSimpleFQLQuery.ps1

ExecuteASimpleFQLQuery.ps1 "https://intranet.contoso.com/fast-search" $args[0]

[To execute the "# AND isDocument=true" query use "# and isDocument:true" as the script assumes the use of FQL not the SharePoint default of KQL.]

   

ExecuteASimpleFQLQuery.ps1

# ExecuteASimpleFQLQuery.ps1 <URL> <Query>

   

function usage($cmd) {
write-host ""
write-host "Execute a simple FQL keyword query"
write-host ""
write-host "usage: $cmd <URL> <Query>"
write-host " <URL> - URL to the Fast Search Center"

   write-host " <Query> - Simple FQL to execute"

   exit
}

# argument checks
switch ($args.count) {
2 { $url = $args[0]

       $query = $args[1] }
default { usage $myinvocation.mycommand.definition }
}

   

# Put this here or execute it before you run the script

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# argument checks

$searchSite = Get-SPWeb $url

$keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery $searchSite

$keywordQuery.ResultTypes = [Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults

   

$keywordQuery.EnableFQL = 1

$keywordQuery.RowLimit = 10

   

$keywordQuery.QueryText = $query

   

$results = $keywordQuery.Execute()

   

Write-Host Total hits: $results[1].TotalRows

$resultsTable = $results.Item([Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults)

   

$resultsDataTable = $resultsTable.Table

$resultsDataTable | Format-Table -AutoSize -Property title, url