Ok, so – Cardinal Rules Queries has recently had a beta release, you can find it here. It allows you to leverage the power of the rules engine in Sitecore to build queries based on Lucene.Net, and if you choose – other search tools. Please see my initial post Sitecore Rules Queries Pt I for a bit more information.
Following on from this, I thought I would write a quick article on how you actually use it in code. Please note this is subject to change, but is correct at the time of posting.
At its core – its really simple, the engine leverages a QueryManager, this object is responsible for translating between Sitecore’s Rules structures and a useful result. I am in the process of allowing POCO returns, but I am using Sitecore Items in this example
// realistically - DI this, but for clarity 😉 IQueryManager queryManager = new QueryManager(new LucinqSearchRepository(new ContentDatabaseHelper()), new ItemManager(new ItemRepository(), new SitecoreLog()), new SitecoreHelper()); ValueRuleContext context = new ValueRuleContext(null, new Dictionary()); RuleList rulesList = RuleFactory.GetRules(item, fieldName); // item is the query item you are using - it could easily be a datasource 😉 // not entirely sure this is needed - am still experimenting. rulesList.Run(context); IRulesSearchResult result = queryManager.GetRulesSearchResult(rulesList, new SearchOptions(0, 20)); // do something with you results
So go ahead, create rules, drive code, set your criteria / actions using the rules engine 😀
- Rules Engine Queries Pt I – Introduction
- Rules Engine Queries Pt III – What can we use rules queries for?