Using hybris rule engine for product recommendations
Introduction
There are the following user cases for the recommendation engines,- Substitute products:
- (1) Based on attribute similarity. It can rely on the properties of the item(s), which are analyzed to determine what else the user may like.
- Example: Apple iphone 6 and Samsung Galaxy S6 are substitutes because they have a lot of similar/same characteristics (product attributes).
- (2) Based on association rules
- Example: Coke and Pepsi are substitutes. Note that it is a manual work to connect them into the group.
- (1) Based on attribute similarity. It can rely on the properties of the item(s), which are analyzed to determine what else the user may like.
- Сomplementary products
- (3) Based on collaborative filtering (frequently bought together)
- Example: the IPA beer and Kettle Jalapeno chips are complementary products because people like to buy them together.
- (4) Based on association rules (for co-purchased products)
- Example: the IPA beer and Lays are complementary products because the merchant wants to sell more Lays than Kettle.
- (3) Based on collaborative filtering (frequently bought together)
Solution
There are two pieces of functionality,- Rule builder for the promotion engine
- Recommendation engine
As you can see in the diagram below, these modules are not connected with each other. Builder produces the rules, and the recommendation engine uses them.
Rules
There are some examples of the rules which are available in the system already:- If you visit the product page with one of Film Camera products, the system must recommend the color and black&white films as complimentary products, if any.
- If the product has “memory stick” in the “Supported memory cards” classification attribute, the system must recommend the Memory Stick products, if any.
- For the Products X,Y,Z the system must recommend the products from categories A and B except the products with the attribute N=<something>
Rule Builder
Rule Builder is initially designed for promotions. It is a brand-new product: SAP added it to hybris in April 2016 (version 6.0) In order to use it for the product recommendation rules, I added custom conditions and actions.Generating recommended products
This module uses hybris Drools engine for evaluating rules against the products (one or more). The result is a SOLR request.Custom Conditions
For the demo I created two custom conditions:- product title condition
- product classification attribute condition.
Custom actions
For the demo I created one custom condition:- Filter all products with specified Indexed Properties.
Post processing
The resulting SOLR request is a merge of the rules output.Video
Architecture
Technical details
Impex
$lang=en INSERT_UPDATE RuleConditionDefinition;id[unique=true] ;name[lang=en] ;priority;breadcrumb[lang=$lang] ;allowsChildren ;translatorId ;translatorParameters;categories(id) ;producttitle ;Product title ;200 ;Product ;false ;simpleProductAttributeConditionTranslator ; ;general ;Example_Compatible_memory_cards ;Compatible memory cards ;200 ;Product ;false ;extProductAttributeConditionTranslator ; ;general INSERT_UPDATE RuleConditionDefinitionParameter;definition(id)[unique=true];id[unique=true];priority;name[lang=$lang];description[lang=$lang];type;value;required[default=true]; #y_cart_total;operator;1100;Operator;Operator to compare the cart total value;Enum(de.hybris.platform.ruledefinitions.AmountOperator);”””GREATER_THAN_OR_EQUAL”””; ;producttitle ;titlestr ;1000 ;Title Substring ;Title Substring ;java.lang.String;; ;Example_Compatible_memory_cards;comp_mc;1001;Example Compatible memory cards (Substring);Example Compatible memory cards (Substring); java.lang.String INSERT_UPDATE RuleConditionDefinitionRuleTypeMapping;definition(id)[unique=true];ruleType(code)[unique=true] ;producttitle;PromotionSourceRule ;Example_Compatible_memory_cards;PromotionSourceRule #ACTIONS $lang=en INSERT_UPDATE RuleActionDefinitionCategory;id[unique=true];name[lang=$lang];priority ;recommendations;recommendations;700 INSERT_UPDATE RuleActionDefinition;id[unique=true];name[lang=$lang];priority;breadcrumb[lang=$lang];translatorId;translatorParameters;categories(id) ;recommend_products;Add products to recommendations;200;Add product to recommendations;ruleExecutableActionTranslator;actionId->ruleAddProductsToRecommendedAction;recommendations INSERT_UPDATE RuleActionDefinitionParameter;definition(id)[unique=true];id[unique=true];priority;name[lang=$lang];description[lang=$lang];type;value;required[default=true] ;recommend_products;solrProperty;100;Solr Property;Solr Property;ItemType(SolrIndexedProperty); ;recommend_products;solrExpression;101;solrExpression;solrExpression;Enum(de.hybris.ruleenginetrail.enums.ActionOperator); ;recommend_products;value;102;Value of the Field;Product Title Substring;java.lang.String;; INSERT_UPDATE RuleActionDefinitionRuleTypeMapping;definition(id)[unique=true];ruleType(code)[default=PromotionSourceRule][unique=true] ;recommend_products;Classes
- Custom ProductRAO populator. Converts0 ProductModel to ProductRAO. By default, only “code” and “supercategories” are populated. I added all the remaining product properties, including classification attributes.
- Custom Product Attribute Condition Translators. This classes are used to convert the values from the condition parameters (see impex) into DroolsRules (more exact, into the RuleIrCondition that used for the drools rules composition). I created two custom translators, as an example, for the product title and for the product classification attribute. It is trivial to convert it into something more comprehensive.
- Custom Rule Executable Action. I use only one type of action, “ProductsToRecommend”. This action is executed each time the rule condition is fulfilled. Internally, all this class do is adding ProductToRecommendRAO with configurable rule-dependent data as a fact.
- ProductToRecommendRAO item type. I used the only custom attribute here, solrCondition (Map type). This structure is used for the messages from actions back to the controller or service. The messages contain the solr attribute name and value.