I was recently working on a project where we had a large set of user generated content that was stored in xDB that we wanted to display on the front end of the site. One of the challenges of this was we needed to personalize what content from xDB was being displayed. In addition to this we had several hundred thousand pieces of content to work with. We decided to serve the content from a search index, however we wanted to be able to allow content authors to be able to personalize the result set returned. We accomplished this by using the rules engine to build predicates on the fly. This approach proved to be extremely powerful.
Arrested Development Quotes in Sitecore
Below I'm going to illustrate how this would be done by creating a repository of Arrested Development quotes in Sitecore. We want to be able to display different Arrested Development quotes to a user with rules, but we don't want to explicitly define which quotes are shown with datasources. Instead, we are going to use rules to describe meta data about the quotes and leverage search to find the results that fit our criteria. If you haven't watched Arrested Development yet, I suggest you stop reading, take a few days off from work, and binge watch it.
Here are some examples of what those rules look like, and what results we should expect them to return:
Michael: (to George Michael) I'm gonna give you a promotion. Welcome aboard, Mr. Manager.
George Michael: Wow! I'm Mr. Manager!
Michael: Well, manager. We just say manager. And you can hire an employee if you need one.
George Michael: Do you think I need one?
Michael: Don't look at me, Mr. Manager.
George Michael: Right. It's up to me now. I'm Mr. Manager.
I hear the jury’s still out on science. -Gob
Custom Rule Context
One of the first things we need is a base search predicate, we also need to be able to pass the predicate between the rule actions that the content author selects. Below you can see a simplified example of this. We are simply using the PredicateBuilder to create an empty True predicate, so that we can AND each of the rule actions together.
A Simple Example
Next we need to create rules for each one of our pieces of search logic.
Below are the classes that we would need to evaluate this rule and create a search predicate based on it. When the rules engine evaluates this it will simply take the parameters that we passed in via our rules, and AND each piece of search logic onto the base predicate that we defined on our custom rule context. At the end of the evaluation we can then pass the resulting expression off to the search provider and generate our result set.
Handling OR logic
The previous example has one major short coming, it doesn't handle OR logic. To handle OR logic, we need to have the ability to except mulitple parameters in a single rule action. Within the rule action, we construct a false predicate and then pass it to our true predicate.
Once your rules have run, you can then access the predicate from your custom rule context and use that to search for appropriate content.