rule = { "objectID": "a-rule-id", "conditions": [{"pattern": "smartphone", "anchoring": "contains"}], "consequence": {"params": {"filters": "category = 1"}},}# Optional: turn the rule offrule["enabled"] = False# Optional: add valid time rangesrule["validity"] = [ { "from": int((datetime.datetime.utcnow().timestamp())), "until": int((datetime.datetime.utcnow() + timedelta(days=10)).timestamp()), }]# Save the ruleresponse = index.save_rule(rule)# Save the rule and forward it to all replicas of the index.response = index.save_rule(rule, {"forwardToReplicas": True})
rule = { objectID: "a-rule-id", conditions: [ { pattern: "smartphone", anchoring: "contains" } ], consequence: { params: { filters: "category = 1" } }}# Optional: turn the rule offrule["enabled"] = false# Optional: add valid time ranges# (with `require 'date'`)rule["validity"] = [ { from: Time.now.to_i, until: (DateTime.now + 10).to_time.to_i }]# Save the ruleindex.save_rule(rule)# Save the rule and wait until it's saved before continuingindex.save_rule!(rule)# Save the rule and forward it to all replicas of the index.response = index.save_rule(rule, {forwardToReplicas: true})
val from = ZonedDateTime.now(ZoneId.of("UTC").normalized());val until = from.plusDays(10);val ruleToSave = Rule( objectID = "a-rule-id", enabled = Some(false), // Optional: turn the rule off validity = Some(Seq(TimeRange(from, until))), // Optional: add valid time ranges conditions = Some(Seq(Condition( pattern = "smartphone", anchoring = "contains", ))), consequence = Consequence( params = Some(Map("filters" -> "category = 1")), ),)client.execute { save rule ruleToSave inIndex "index_name"}
let rule = Rule(objectID: "a-rule-id") .set(\.isEnabled, to: false) .set(\.conditions, to: [ Rule.Condition() .set(\.anchoring, to: .contains) .set(\.pattern, to: .literal("smartphone")) ]) .set(\.consequence, to: Rule.Consequence() .set(\.query, to: Query() .set(\.filters, to: "category = 1") ) ) .set(\.validity, to: [ TimeRange(from: Date(), until: Date().addingTimeInterval(10 * 24 * 60 * 60)) // 10 days ] )index.saveRule(rule, forwardToReplicas: true) { result in if case .success(let response) = result { print("Response: \(response)") }}
let rule = Rule(objectID: "a-rule-id") .set(\.conditions, to: [ Rule.Condition() .set(\.anchoring, to: .contains) .set(\.pattern, to: .literal("smartphone")) .set(\.alternatives, to: .true) ]) .set(\.consequence, to: Rule.Consequence() .set(\.query, to: Query() .set(\.filters, to: "category = 1") ) )index.saveRule(rule, forwardToReplicas: true) { result in if case .success(let response) = result { print("Response: \(response)") }}
let rule = Rule(objectID: "a-rule-id") .set(\.conditions, to: [ Rule.Condition().set(\.context, to: "mobile") ]) .set(\.consequence, to: Rule.Consequence() .set(\.query, to: Query() .set(\.filters, to: "release_date >= 1568498400") ) )index.saveRule(rule, forwardToReplicas: true) { result in if case .success(let response) = result { print("Response: \(response)") }}
Determines whether promoted records must also match active filters for the consequence to apply.This ensures user-applied filters take priority and irrelevant matches aren’t shown.
For example, if you promote a record with color: red but the user filters for color: blue,
the “red” record won’t be shown.
Object IDs of a group of records to promote.
A group can have up to 100 records.
Either objectID or objectIDs is required.The records are placed as a group at position.
For example, if you want to promote four records to position 0,
they will be the first four search results.
Parameters to apply to this search.
You can use all search parameters,
plus the special fields query, automaticFacetFilters, and automaticOptionalFacetFilters.
Facet filters to apply to this search.
You can use this to respond to search queries that match a facet value.
For example, if users search for “comedy”, which matches a facet value of the “genre” facet,
you can filter the results to show the top-ranked comedy movies.
Facet name.
It must match the placeholder used in the patterns parameter of the rule’s condition.
For example, with pattern: {facet:genre}, automaticFacetFilters.facet must be genre.
Optional filters to apply to this search.
This is the same as automaticFacetFilters,
but with optionalFilters:
records that don’t match the filters will be ranked below matching records.
A JSON object with custom data that will be appended to the userData array in the search response.
This object isn’t interpreted by the API and is limited to 1 kB of minified JSON.
Whether the pattern should match plurals, synonyms, and typos.
For example, if pattern is shoe, and alternatives is true,
the rule is also triggered when the query is shoes, or sheo (typo).For each word in pattern, Algolia checks up to 10 alternatives.
If the total number of alternatives is more than 10,
Algolia may not check some or even any of the words.
An additional restriction that only triggers the rule, when the search has the same value as ruleContexts parameter.
For example, if context: "mobile",
the rule is only triggered when the search request has a matching ruleContexts: "mobile".
A rule context must only contain alphanumeric characters.
Filters that trigger the rule.You can add filters using the syntax facet:value so that the rule is triggered, when the specific filter is selected.
You can use filters on its own or combine it with the pattern parameter.
You can’t combine multiple filters with OR and you can’t use numeric filters.
Query pattern that triggers the rule.You can use either a literal string, or a special pattern {facet:ATTRIBUTE}, where ATTRIBUTE is a facet name.
The rule is triggered if the query matches the literal string or a value of the specified facet.
For example, with pattern: {facet:genre},
the rule is triggered when users search for a genre, such as “comedy”.When specifying a query pattern, you must also specify an anchoring.
This section shows the JSON response returned by the API.
Each API client wraps this response in language-specific objects, so the structure may vary.
To view the response, use the getLogs method.
Don’t rely on the order of properties—JSON objects don’t preserve key order.