Sitecore 7 Indexing with Code
Several years ago, the Advanced Database Crawler and Searcher made it much easier to work with Sitecore's Lucene indexes. It made a similified way to create custom indexes and query those indexes. It included a helpful tool to rebuild those indexes on content delivery instances if needed. The Sitecore Support Toolbox additionally had a tool to rebuild indexes.
Otherwise an index needed to be rebuilt programmatically, a developer could simply call the following:
Sitecore.Search.SearchManager.GetIndex("custom_index").Rebuild();
Of course, rebuilding was relatively pretty simple with Sitecore's API for version 6.x.
Since then, Sitecore 7 brought more robust featues for managing Lucene indexes. and changed the way a developer interacts with these indexes.
Sitecore 7 API Changes
To access an index in Sitecore 6.x, a developer would do the following:
var index = Sitecore.Search.SearchManager.GetIndex("custom_index");
In Sitecore 7.x, the Sitecore API has introduced a Sitecore.ContentSearch
namespace and within that is defined a manager called ContentSearchManager
. Getting access to the index is still as easy as it was in Sitecore 6.x:
var index = Sitecore.ContentSearch.ContentSearchManager.GetIndex("custom_index");
Rebuild an Index
The above example is with a custom index if one was defined. With Sitecore 7, it may not be necessary to define a custom index as Sitecore already maintains three indexes:
- index_master
- index_web
- index_core
To access one of these indexes, a developer could do the following:
var masterIndex = Sitecore.ContentSearch.ContentSearchManager.GetIndex("index_master");
If the index needed to be rebuilt, it could be done with:
masterIndex.Rebuild();
Though, this procedure takes a long time so the client may give up before it receives a confirmation that the index has been rebuilt. The recommendation would be to wrap this call with a Sitecore Job.
Sitecore Job
To setup a job for Sitecore, we'll create a service class that will be responsible for setting up the job and starting. It may be benefical to add some logging as an index may take some time to complete. A developer could go as far as creating a view into what jobs are running or queued with Sitecore if necessary. But the logs should be sufficient.
First step is to create the interface and class:
using Sitecore.ContentSearch;
using Sitecore.Jobs;
public interface IJobService()
{
JobStatus Start();
void Rebuild(ISearchIndex index);
}
We will add two methods. One to start to setup and start the job and the other to start the rebuild of the index.
using Sitecore.ContentSearch;
using Sitecore.Jobs;
public class JobService : IJobService
{
public JobStatus Start()
{
...
}
public void Rebuild(ISearchIndex index)
{
...
}
}
The second step is to complete the logic to setup and start the job:
public JobStatus Start()
{
var indexWeb = Sitecore.ContentSearch.ContentSearchManager.GetIndex("index_web");
var options = JobOptions("Rebuild Web Index",
"Indexing",
Sitecore.Context.Site.Name,
this,
"Rebuild",
new object[] { indexWeb });
var job = JobManager.Start(options);
return job.Status;
}
The JobOptions
object requires the constructor to provide these parameters:
- Job Name
- Category
- Site Name
- Instance of object (that contains the method to call)
- Method name (that will execute for the job)
- Any passing parameters (in this case we are passing the index to be rebuilt)
Finally, we need to complete the Rebuild
method:
public void Rebuild(ISearchIndex index)
{
var job = JobManager.GetJob("Rebuild Web Index");
job.Status.State = JobState.Running;
index.Rebuild();
job.Status.Processed += 1L;
job.Status.State = JobState.Finished;
}
Job status is updated so any job viewer can see its status and Sitecore knows when it is done.
Much of what is needed to rebuild an index in a job is ready. What is probably left is handling pre-conditions and/or post-conditions with these methods and allow this logic to be called from a ASP.NET Web Form, MVC route, subscribe to a Sitecore event, or create a Sitecore scheduled task.
However you wish to use it, enjoy!