Enable continuous crawls with PowerShell

Continuous crawling is a new feature with SharePoint 2013. It is a way to ensure search results are fresher than they otherwise would be with incremental and full crawls on their own. With incremental and full crawls, a new crawl will not start until a previous crawl has finished. With continuous crawls a running crawl will not stop a new crawl from starting. This is common if there were a lot of updates and the previous crawl is still running. The new crawl will pick up changes that the old crawl has not yet processed and you'll see these items in your results sooner than if you had waited for the old crawl to finish.

Microsoft has a guide, Manage continuous crawls in SharePoint Server 2013 that details turning on continuous crawls however the only method they show is with Central Administration.

How to set it up with PowerShell

A continuous crawl works only on content sources that crawl SharePoint content. For this example, we’ll use the out of the box “Local SharePoint sites” content source as unsurprisingly it indexes only SharePoint content. We’ll also need a functioning search service application. In this example we assume there is a single search service application running in the farm.

To enable the crawl, use the Set-SPEnterpriseSearchCrawlContentSource cmdlet with the EnableContinuousCrawls parameter, like this:

$ssa = Get-SPEnterpriseSearchServiceApplication
$cs = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa -Identity "Local SharePoint sites"
Set-SPEnterpriseSearchCrawlContentSource -Identity $cs -EnableContinuousCrawls $True

That’s it. Now it's turned on.

You can also change how often a continuous crawl runs. By default continuous crawls run every 15 minutes. This setting is per search service application so if you have multiple SharePoint content sources using continuous crawl, they all will share this interval.

To make the crawl more frequent, let's say every 10 minutes, use the Search Service Application's SetProperty() method to set the ContinuousCrawlProperty to 10:

$ssa.SetProperty("ContinuousCrawlInterval", 10)
$ssa.Update()

Notice I used the Update() method on the Search Service Application. The official documentation doesn’t say you need to do this. From previous experience, whenever I see an object with an Update() method there's usually a good bet using it after making a change will make the change permanent. I don't know for sure if it's the case for continuous crawl but it doesn't hurt.

But it gets better, combine this with my previous post, Setting crawl schedules with PowerShell and now you can set all three crawls: Continuous, Full, and Incremental:

$ssa = Get-SPEnterpriseSearchServiceApplication
$cs = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa -Identity "Local SharePoint sites"
Set-SPEnterpriseSearchCrawlContentSource -Identity $cs -EnableContinuousCrawls $True
$ssa.SetProperty("ContinuousCrawlInterval", 10)
$ssa.Update()

Set-SPEnterpriseSearchCrawlContentSource -Identity $cs -ScheduleType Full -WeeklyCrawlSchedule -CrawlScheduleRunEveryInterval 1 -CrawlScheduleDaysOfWeek "Friday" -CrawlScheduleStartDateTime "10:30 PM"

Set-SPEnterpriseSearchCrawlContentSource -Identity $cs -ScheduleType Incremental -DailyCrawlSchedule -CrawlScheduleRunEveryInterval 1 -CrawlScheduleRepeatInterval 30 -CrawlScheduleRepeatDuration 1440 -Confirm:$false

Now we have continuous crawls running every 10 minutes, incremental crawls running every 30 minutes, and full crawls running every Friday at 10:30pm.

Bug?

There’s an issue with SetProperty(). SetProperty() takes a property name and a value. The name is obviously a String, but according to the MSDN SetProperty page the value can be either an Int32 or a String. When I was testing this out I was using a value from an XML file which made the variable a String. When you pass SetProperty() a string for the interval value you get the exception:

Exception calling "SetProperty" with "2" argument(s): "Service is offline"

At first I thought this was because continuous crawl hadn’t yet run or wasn't fully set up but once I figured out it would accept hard coded values and integer variables it started to make sense. For setting the continuous crawl interval, SetProperty() will only accept integer values.

For example, this will not work:

$interval = "5"
$ssa.SetProperty("ContinuousCrawlInterval", $interval)

You get the exception. Obviously, you wouldn’t create a variable that is a string when you want an integer, this is just an example. Remember in my case my value came from an XML file so it had to be a string. If you cast your string to an integer it will work:

$interval = "5"
$ssa.SetProperty("ContinuousCrawlInterval", [int]$interval)

Happy crawling!

Stories say it best.

Are you ready to make your workplace awesome? We're keen to hear what you have in mind.

Interested in learning more about the work we do?

Explore our culture and transformation services.