The most common use case for Rules is to alter search results depending on a particular condition. However, you can also create Rules that are active for all searches in the Rule’s associated index.
This guide shows you how to create a conditionless Rule, but doesn’t specify the consequences.
Possible consequences include:
Some consequences aren’t available for conditionless Rules. Examples are:
- Pinning items
- Editing the query
- Filtering or boosting matching attributes
Using the API
To create a Rule with an API client, use the saveRule
method on a Rule object without a condition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $rule = array(
'objectID' => 'a-rule-id',
'consequence' => array(
// Set relevant consequence(s)
)
);
// Set validity (optional)
$rule['validity'] = array(
array(
'from' => time(),
'until' => time() + 30*24*60*60,
)
);
$index->saveRule($rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| rule = {
objectID: 'a-rule-id',
consequence: {
# Set relevant consequence(s)
}
}
# Set validity (optional)
# (with `require 'date'`)
rule['validity'] = [
{
from: Time.now.to_i,
until: (DateTime.now + 30).to_time.to_i,
}
]
index.save_rule('a-rule-id', rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| const rule = {
objectID: 'a-rule-id',
consequence: {
// Set relevant consequence(s)
}
};
// Set validity (optional)
rule['validity'] = [
{
'from': Math.floor(Date.now()/1000),
'until': Math.floor(Date.now()/1000) + 30*24*60*60,
}
];
index.saveRule(rule).then(() => {
// done
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| rule = {
'objectID': 'a-rule-id',
'consequence': {
# Set relevant consequence(s)
}
}
# Set validity (optional)
# Python 2 (with `time` and `datetime.timedelta` imports)
rule['validity'] = [
{
'from': int(time.time()),
'until': int(time.time() + timedelta(days=30).total_seconds())
}
]
# Python 3 (with `datetime.{datetime,timedelta}` imports)
rule['validity'] = [
{
'from': int((datetime.datetime.utcnow().timestamp())),
'until': int(
(datetime.datetime.utcnow() + timedelta(days=30)).timestamp())
}
]
response = index.save_rule(rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
| let rule = Rule(objectID: "a-rule-id")
.set(\.consequence, to: Rule.Consequence()
// Set relevant consequence(s)
)
// Set validity (optional) to 30 days
.set(\.validity, to: [.init(from: Date(), until: Date().addingTimeInterval(30 * 24 * 60 * 60))])
index.saveRule(rule) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| Rule ruleToSave = new Rule
{
ObjectID = "a-rule-id",
Consequence = new Consequence
{
// Set relevant consequence(s)
},
{
new TimeRange // Set validity (optional)
{
From = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
Until = new DateTimeOffset(DateTime.UtcNow.AddDays(30)).ToUnixTimeSeconds()
}
}
};
index.SaveRule(rule);
// Asynchronous
await index.SaveRuleAsync(rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| Consequence consequence =
new Consequence()
// Set relevant consequence(s)
// Set validity (optional)
List<TimeRange> validity =
Collections.singletonList(
new TimeRange(
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC).plusDays(30));
Rule rule = new Rule()
.setObjectID("a-unique-identifier")
.setConsequence(consequence);
.setValidity(validity);
index.saveRule(rule);
// Asynchronous
index.saveRuleAsync(rule);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| rule := search.Rule{
ObjectID: "a-rule-id",
Consequence: search.RuleConsequence{
// Set relevant consequence(s)
},
},
// Set validity (optional)
Validity: []search.TimeRange{
{
From: time.Now(),
Until: time.Now().AddDate(0, 0, 30),
},
},
}
res, err := index.SaveRule(rule)
|
1
2
| // Creating conditionless Rules isn't currently possible with this client.
// Please use the dashboard instead.
|
1
2
3
4
5
6
7
8
9
10
| val rule = Rule(
objectID = ObjectID("a-rule-id"),
consequence = Consequence(
// Set relevant consequence(s)
),
// Set validity (optional)
validity = listOf(TimeRange(0, 30))
)
index.saveRule(rule)
|
Using the dashboard
- Select the Search product icon on your dashboard.
- 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. You can’t create conditionless Rules using the Visual Editor.
- In the Condition(s) section, click the Remove button with the trash can icon at the top right of the condition.
- In the Consequence(s) section, set the relevant consequence(s).
- If this rule is only applicable for a certain period of time, select the time range In the Additional Settings, under the
Timeframe in UTC
header.
- If you want to forward the Rule to replicas or other indices, toggle Copy this rule to other indices, and enter the relevant indices.
- Don’t forget to save your changes.