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,
$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
construle={objectID:'a-rule-id',consequence:{params:{aroundRadius:1000}}};// set valitidy to 30 daysrule['validity']=[{'from':Math.floor(Date.now()/1000),'until':Math.floor(Date.now()/1000)+30*24*60*60,}];index.saveRule(rule).then(()=>{// done});
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
letrule=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){resultinifcase.success(letresponse)=result{print("Response: \(response)")}}
1
2
3
4
5
6
7
8
9
valrule=Rule(objectID=ObjectID("a-rule-id"),consequence=Consequence(query=Query(aroundRadius=1000)),// set validity to 30 daysvalidity=listOf(TimeRange(0,30)))index.saveRule(rule)
RuleruleToSave=newRule{ObjectID="a-rule-id",Consequence=newConsequence{Params=newConsequenceParams{AroundRadius=1000}},{newTimeRange// set validity to 30 days{From=newDateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),Until=newDateTimeOffset(DateTime.UtcNow.AddDays(30)).ToUnixTimeSeconds()}}};index.SaveRule(rule);// Asynchronousawaitindex.SaveRuleAsync(rule);
Consequenceconsequence=newConsequence().setParams(newConsequenceParams().setAroundRadius(1000));// set validity to 30 daysList<TimeRange>validity=Collections.singletonList(newTimeRange(OffsetDateTime.now(ZoneOffset.UTC),OffsetDateTime.now(ZoneOffset.UTC).plusDays(30));Rulerule=newRule().setObjectID("a-unique-identifier").setConsequence(consequence);.setValidity(validity);index.saveRule(rule);// Asynchronousindex.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 daysValidity:[]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.
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 drop-down menu, select 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 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: { "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.
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.