// Turn JSON into an array$rule=array('objectID'=>'promote-harry-potter-box-set','conditions'=>array(array('pattern'=>'Harry Potter','anchoring'=>'contains')),'consequence'=>array('promote'=>array(array('objectID'=>'HP-12345','position'=>0// objectID 'HP-12345' ==> Harry Potter Box Set))));// Push Rule to index$index->saveRule($rule);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
rule={objectID: 'promote-harry-potter-box-set',conditions: [{pattern: 'Harry Potter',anchoring: 'contains'}],consequence: {promote: [{objectID: 'HP-12345',position: 0# objectID 'HP-12345' ==> Harry Potter Box Set}]}}index.save_rule('promote-harry-potter-box-set',rule)
1
2
3
4
5
6
7
8
9
10
11
12
13
index.saveRule({objectID:'promote-harry-potter-box-set',condition:{pattern:'Harry Potter',anchoring:'contains',},consequence:{promote:[{objectID:'HP-12345',position:0// objectID 'HP-12345' ==> Harry Potter Box Set}]},});
letrule=Rule(objectID:"promote-harry-potter-box-set").set(\.conditions,to:[Rule.Condition().set(\.anchoring,to:.contains).set(\.pattern,to:.literal("Harry Potter"))]).set(\.consequence,to:Rule.Consequence().set(\.promote,to:[// objectID 'HP-12345' ==> Harry Potter Box Set.init(objectID:"HP-12345",position:0)]))index.saveRule(rule){resultinifcase.success(letresponse)=result{print("Response: \(response)")}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
valrules=rules{rule(objectID="Promote Harry Potter Box Set",conditions=conditions{condition(anchoring=Anchoring.Contains,pattern=Pattern.Literal("Harry Potter"))},consequence=consequence(promote=promotions{+ObjectID("HP-12345")(0)}))}index.saveRules(rules)
varrule=newRule{ObjectID="Promote Harry Potter Box Set",Condition=newCondition{Anchoring="contains",Pattern="Harry Potter"},Consequence=newConsequence{Promote=newList<ConsequencePromote>{newConsequencePromote{ObjectID="HP-12345",Position=0}}}};index.SaveRule(rule);// Asynchronousawaitindex.SaveRuleAsync(rule);
1
2
3
4
5
6
7
8
9
10
11
Conditioncondition=newCondition().setPattern("Harry Potter").setAnchoring("contains");ConsequencePromotepromote=newConsequencePromote().setObjectID("HP-12345").setPosition(0);Consequenceconsequence=newConsequence().setPromote(Collections.singletonList(promote));Rulerule=newRule().setObjectID("Promote Harry Potter Box Set").setConditions(Collections.singletonList(condition)).setConsequence(consequence);index.saveRule(rule);
1
2
3
4
5
6
7
8
9
10
11
rule:=search.Rule{ObjectID:"promote-harry-potter-box-set",Condition:search.RuleCondition{Anchoring:search.Contains,Pattern:"Harry Potter"},Consequence:search.RuleConsequence{Promote:[]search.PromotedObject{{"HP-12345",0},// objectID 'HP-12345' ==> Harry Potter Box Set},},}res,err:=index.SaveRule(rule,false)
Select the Search product icon on your dashboard and then select your index.
Select the Rules section from the left sidebar menu in the Algolia dashboard.
Under the heading Rules, select the index you are adding a Rule to.
Select Create your first rule or New rule. In the drop-down menu, click the Visual Editor option.
In the It all starts here section:
Click Set search query button.
In the side panel, type “Harry Potter” in the input field and click Save.
In the What do you want to do? section:
Click the Pin item button.
In the side panel, find the product ‘HP-12345’ you want to pin in the input field and press Save.
Review and publish your changes.
Manual Editor
Select the Rules section from the left sidebar menu in the Algolia dashboard.
Under the heading Rules, select the index you are adding a Rule to.
Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
In the Condition(s) section, keep Query contains and enter “Harry Potter” in the input field.
In the Consequence(s) section:
Click the Add consequence button and select Pin an item.
Find the product ‘HP-12345’ you want to pin the input field and press Enter.
Save your changes.
Promoting the newest release
Use case
You’ve placed “best-selling items” at the top of your search results by using Custom Ranking. But the newest release? Set up a Rule telling the engine that whenever iPhone is searched for, place the newest version at the top, but for the rest of the same brand phones, continue sorting by most-sold.
Rule
If query = iphone then promote newest iPhone release
// Turn JSON into an array$rule=array('objectID'=>'Promote-iPhone-X','conditions'=>array(array('pattern'=>'iPhone','anchoring'=>'contains')),'consequence'=>array('promote'=>array(array('objectID'=>'iPhone-12345','position'=>0// objectID 'iPhone-12345' ==> iPhone X (newest release)))));// Push Rule to index$index->saveRule($rule);
Conditioncondition=newCondition().setAnchoring("contains").setPattern("iPhone");ConsequencePromotepromote=newConsequencePromote().setObjectID("iPhone-12345")// objectID 'iPhone-12345' ==> iPhone X (newest release).setPosition(0);Consequenceconsequence=newConsequence().setPromote(Collections.singletonList(promote));Rulerule=newRule().setObjectID("Promote-iPhone-X").setConditions(Collections.singletonList(condition)).setConsequence(consequence);index.saveRule(rule);// Asynchronousindex.saveRuleAsync(rule);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
rule:=search.Rule{ObjectID:"Promote-iPhone-X",Condition:search.RuleCondition{Anchoring:search.Contains,Pattern:"iPhone"},Consequence:search.RuleConsequence{Promote:[]search.PromotedObject{{ObjectID:"iPhone-12345",Position:0,// objectID 'iPhone-12345' ==> iPhone X (newest release)},},},}res,err:=index.SaveRule(rule)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
valrule=Rule(objectID="Promote-iPhone-X",condition=Some(Condition(pattern="iPhone",anchoring="contains")),consequence=Consequence(promote=Some(Seq(ConsequencePromote("iPhone-12345",0)))// objectID 'iPhone-12345' ==> iPhone X (newest release)))client.execute{saveruleruleinIndex"indexName"}
Using the dashboard
Visual Editor
Select the Search product icon on your dashboard and then select your index.
Select the Rules section from the left sidebar menu in the Algolia dashboard.
Under the heading Rules, select the index you are adding a Rule to.
Select Create your first rule or New rule. In the drop-down menu, click the Visual Editor option.
In the It all starts here section:
Click Set search query button.
In the side panel, type “iPhone” in the input field and click Save.
In the What do you want to do? section:
Click the Pin item button.
In the side panel, find the product “iPhone-12345” you want to pin in the input field and press Save.
Review and publish your changes.
Manual Editor
Select the Rules section from the left sidebar menu in the Algolia dashboard.
Under the heading Rules, select the index you are adding a Rule to.
Select Create your first rule or New rule. In the drop-down menu, click the Manual Editor option.
In the Condition(s) section, keep Query toggled on, select Contains in the drop-down menu, and enter “iPhone” in the input field.
In the Consequence(s) section:
Click the Add consequence button and select Pin an item.
Find the product “iPhone-12345” you want to pin the input field and press Enter.
Save your changes.
Promoting several results
Use case
You’re running a promotion on the newest Apple products. Set up a Rule telling the engine that whenever apple is searched for, place the newest releases at the top.
Rule
If query = apple then promote newest Apple releases
// Turn JSON into an array$rule=array('objectID'=>'Promote-Apple-Newest','conditions'=>array(array('pattern'=>'apple','anchoring'=>'is')),'consequence'=>array('promote'=>array(array('objectIDs'=>array('iPhone-12345','watch-123'),'position'=>0))));// Push Rule to index$index->saveRule($rule);
Select the Search product icon on your dashboard and then select your index.
Select the Rules section from the left sidebar menu in the Algolia dashboard.
Under the heading Rules, select the index you are adding a Rule to.
Select Create your first rule or New rule. In the drop-down menu, click the Visual Editor option.
In the It all starts here section:
Click Set search query button.
In the side panel, type “apple” in the input field and click Save.
In the What do you want to do? section:
Click the Pin item button.
In the side panel, click the Pin multiple items button.
One per line, enter the object IDs you want to pin, “iPhone-12345” and “watch-123”, in the input field and press Pin items.
Click Apply.
Review and publish your changes.
Manual Editor
The Manual Editor only allows for pinning a single item at a time.
Promoting some results over others
Use case
Tomato: A simple search, but one that may never return the fruit (or vegetable) because too many Tomato Soup brands are selling better than cherry tomatoes.
Rule
If query=tomato then put fruits higher than the rest
// Turn JSON into an array$rule=array('objectID'=>'tomato-fruit','conditions'=>array(array('pattern'=>'tomato','anchoring'=>'contains')),'consequence'=>array('params'=>array('optionalFilters'=>'food_group:fruit')));// Push Rule to index$index->saveRule($rule);