You can create Rules to add default parameters to every search made to the associated index. This is most useful when applied with a validity period. If you want to add always active parameters for an indefinite period of time, you should change your index’s settings.
This guide shows you how to create a Rule that sets the:
aroundRadius
search parameter, for all queries, to 1,000 meters,
- and validity period of this Rule to one month.
This Rule is an example of a conditionless Rule.
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
17
| $rule = array(
'objectID' => 'a-rule-id',
'consequence' => array(
'params' => array(
'aroundRadius' => 1000,
)
)
);
// set validity to 30 days
$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
18
19
| rule = {
objectID: 'a-rule-id',
consequence: {
params: {
aroundRadius: 1000
}
}
}
# set validity to 30 days
# (with `require 'date'`)
rule['validity'] = [
{
from: Time.now.to_i,
until: (DateTime.now + 30).to_time.to_i,
}
]
index.save_rule(rule)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| const rule = {
objectID: 'a-rule-id',
consequence: {
params: {
aroundRadius: 1000
}
}
};
// set valitidy to 30 days
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
27
| rule = {
'objectID': 'a-rule-id',
'consequence': {
'params': {
'aroundRadius': 1000
}
}
}
# set validity to 30 days
# 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
13
14
| let rule = Rule(objectID: "a-rule-id")
.set(\.consequence, to: Rule.Consequence()
.set(\.query, to: Query()
.set(\.aroundRadius, to: .meters(1000))
)
)
// set validity 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
21
| Rule ruleToSave = new Rule
{
ObjectID = "a-rule-id",
Consequence = new Consequence
{
Params = new ConsequenceParams
{
AroundRadius = 1000
}
},
{
new TimeRange // set validity to 30 days
{
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
21
| Consequence consequence =
new Consequence()
.setParams(
new ConsequenceParams()
.setAroundRadius(1000));
// set validity to 30 days
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
17
18
| rule := search.Rule{
ObjectID: "a-rule-id",
Consequence: search.RuleConsequence{
Params: &search.RuleParams{
QueryParams: search.QueryParams{
AroundRadius: opt.AroundRadius(1000),
},
},
},
// set validity to 30 days
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
| val rule = Rule(
objectID = ObjectID("a-rule-id"),
consequence = Consequence(
query = Query(aroundRadius = 1000)
),
// set validity to 30 days
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.
- 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, click on Add consequence and select Add Query Parameter.
- In the input field that appears, enter the JSON search parameter you want to add followed by a colon and the value you want to add. For example:
1
2
3
| {
"aroundRadius": 1000
}
|
- In the Additional Settings, under the Timeframe in UTC header, select a time range of one month.
- 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.
In JSON, you must write string values inside quotation marks, and number values and booleans without quotations. To get further details on JSON syntax, checkout the record structure guidance.