There are cases when you may want to hide records from users based on what they’re searching for. Rules can help you achieve that, either for a single item or for several.
Hiding a single item
We can use Rules to hide a specific record based on its unique objectID
.
Using the API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $rule = [
'objectID' => 'hide-12345',
'conditions' => array(array(
'pattern' => 'query',
'anchoring' => 'contains'
)),
'consequence' => [
'hide' => [
'objectID' => 'to-hide-12345'
]
]
];
$index->saveRule($rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| rule = {
objectID: 'hide-12345',
conditions: [{
pattern: 'query',
anchoring: 'contains'
}],
consequence: {
hide: {
objectID: 'to-hide-12345'
}
}
}
index.save_rule(rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| const rule = {
objectID: 'hide-12345',
conditions: [{
pattern: 'query',
anchoring: 'contains'
}],
consequence: {
hide: {
objectID: 'to-hide-12345'
}
}
};
index.saveRule(rule).then(() => {
// done
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| rule = {
'objectID': 'hide-12345',
'conditions': [{
'pattern': 'query',
'anchoring': 'contains'
}],
'consequence': {
'hide': {
'objectID': 'to-hide-12345'
}
}
}
response = index.save_rule(rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| let rule = Rule(objectID: "hide-12345")
.set(\.conditions, to: [
Rule.Condition()
.set(\.anchoring, to: .contains)
.set(\.pattern, to: .literal("query"))
])
.set(\.consequence, to: Rule.Consequence()
.set(\.hide, to: ["to-hide-12345"])
)
index.saveRule(rule) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
| // Creating Rules isn't currently possible with this client.
// Please use the dashboard instead.
|
1
2
3
4
5
6
7
8
9
| var rule = new Rule
{
ObjectID = "hide-12345",
Conditions = new List<Condition> { new Condition { Anchoring = "contains", Pattern = "query" } },
Consequence = new Consequence
{
Hide = new List<Hide> { new Hide { ObjectID = "to-hide-12345" } }
}
};
|
1
2
3
4
5
6
7
8
9
10
| Condition condition = new Condition().setPattern("query").setAnchoring("contains");
Consequence consequence = new Consequence().setHide(Collections.singletonList(new Hide("to-hide-12345")));
Rule rule =
new Rule()
.setObjectID("hide-12345")
.setConditions(Collections.singletonList(condition))
.setConsequence(consequence);
index.saveRule(rule);
|
1
2
3
4
5
6
7
8
9
10
11
| rule := search.Rule{
ObjectID: "hide-12345",
Conditions: []search.RuleCondition{{Anchoring: search.Contains, Pattern: "query"}},
Consequence: search.RuleConsequence{
Hide: []search.HiddenObject{
{"to-hide-12345"},
},
},
}
res, err := index.SaveRule(rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| val rule = Rule(
objectID = "hide-12345",
conditions = Some(Seq(Condition(
anchoring = "contains",
pattern = "query",
))),
consequence = Consequence(
hide = Some(Seq(ConsequenceHide("to-hide-12345"))),
)
)
client.execute {
save rule rule inIndex "indexName"
}
|
1
2
3
4
5
6
7
8
9
| val rules = rules {
rule(
objectID = "hide-12345",
conditions = listOf(Condition(Contains, Literal("harry potter"))),
consequence = Consequence(hide = objectIDs { +"to-hide-12345" })
)
}
index.saveRules(rules)
|
Using the dashboard
Visual 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 dropdown, click on the Visual Editor option.
- In the It all starts here section:
- Click Set search query button.
- In the sidepanel, type your query in the input field and click
Save
.
- In the What do you want to do? section:
- Click the Hide item button.
- In the sidepanel, find the product you want to hide in the input field and press
Save
.
- Don’t forget to 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 dropdown, click on the Manual Editor option.
- In the Condition(s) section, keep Query toggled on, select Contains in the dropdown, and type your query in the input field.
- In the Consequence(s) section:
- Click the Add consequence button and select Hide a result.
- Find the product you want to hide in the input field and press
Enter
.
- Don’t forget to save your changes.
Hiding a set of items
Hiding single items can be helpful, but most of the time, you’ll want to hide several based on a distinctive attribute.
A good example is when users add key phrases such as “gluten-free”, which you can interpret as a cue to filter out any dish that has “gluten” in its list of allergens.