Skip to main content

Reusing Sitecore Index Definitions

This post describes a scenario where I needed to reuse the configuration of a Sitecore index. I'm not sure how common the requirement is, but maybe you can repurpose it for your own needs.

A Sitecore implementation I work on has 2 publishing targets. One is for the 'live' site using the web database. The other is for a staging site using a database named 'staging'. There are numerous custom indexes in the system, and each of those has a web and staging version. For example, we have 'products_index_staging' and 'products_index_web'.

These are almost identical indexes. The only differences are the index name and the database that the crawlers and update strategies point at. We've ended up with a lot of duplicate index configuration, which inevitably has a tendency to get out of sync. I wanted a more elegant and robust solution, and here's what I came up with.

So whats going on here?
  • I'm defining the index once (in 'contentSearch/customIndexes').
  • Im referencing that defintion for each index variant (using the ref attribute). 
  • I'm passing in the name and database as parameters (param1 and param2).

Notice that the param1 and param2 attributes correspond with the tokens $(1) and $(2) in the index defintion. These are used throughout Sitecore's own configuration, but I don't think I've ever seen them documented anywhere.

Going through this process has got me rethinking my approach to Sitecore configuration. If you have any other examples of reuse like this, I'd like to see them.