API Reference / API Methods / Rules / Save rule
Required API Key: any key with the editSettings ACL
Method signature
index.save_rule(
  Hash Rule,
  Hash opts = {
    Boolean forwardToReplicas: false,
  }
)

About this method # A

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

Create or update a single Rule.

You can batch import and export Rules from the Dashboard with a configuration file.

Examples # A

Save a Rule#

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
28
29
30
31
rule = {
  objectID: 'a-rule-id',
  conditions: [{
    pattern: 'smartphone',
    anchoring: 'contains'
  }],
  consequence: {
    params: {
      filters: 'category = 1'
    }
  }
}

# Optionally, to disable the rule
rule['enabled'] = false

# Optionally, to add validity time ranges
# (with `require 'date'`)
rule['validity'] = [
  {
    from: Time.now.to_i,
    until: (DateTime.now + 10).to_time.to_i,
  }
]

# Save the Rule.
index.save_rule(rule)
# Save the Rule and wait the end of indexing.
index.save_rule!(rule)
# Save the Rule, and forward it to all replicas of the index.
response = index.save_rule(rule, { forwardToReplicas: true })

Save a Rule with alternatives enabled#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
rule = {
  objectID: 'a-rule-id',
  conditions: [{
    pattern: 'smartphone',
    anchoring: 'contains',
    alternatives: true
  }],
  consequence: {
    params: {
      filters: 'category = 1'
    }
  }
}
index.save_rule(rule)

Save a Rule with a context based condition#

1
2
3
4
5
6
7
8
9
10
11
12
13
rule = {
  objectID: 'a-rule-id',
  conditions: [{
    context: 'mobile'
  }],
  consequence: {
    params: {
      filters: 'release_date >= 1568498400'
    }
  }
}

index.save_rule(rule)

Parameters # A

objectID #
type: string
Required

Unique identifier for the Rule (format: [A-Za-z0-9_-]+).

Note that for some languages, the objectID is duplicated in the Rule.

Rule #
type: rule
Required

The rule object, its conditions and consequences.

{
  objectID: objectID,
  conditions: conditions,
  consequence: consequence
}
forwardToReplicas #
type: boolean
default: false
Optional

By default, this method applies only to the specified index. By making this true, the method will also send the Rule to all replicas.

Rule ➔ rule #

objectID #
type: string
Required

Must contain the same value as the preceding objectID.

condition #
type: condition
Optional

This parameter is deprecated in favor of conditions.

Condition of the Rule, expressed using the following variables: pattern, anchoring, alternatives, context.

When the condition isn’t provided or is empty, the Rule is active for all queries to the relevant index.

{
  "pattern": pattern,
  "anchoring": anchoring,
  "alternatives": alternatives,
  "context": context
}
conditions #
type: list of condition
Optional

A list of conditions that should apply to activate a Rule. You can use up to 25 conditions per Rule.

Filters can also be used as conditions.

[
  {
    "pattern": pattern,
    "anchoring": anchoring,
    "alternatives": alternatives,
    "context": context
  }
]
consequence #
type: consequence
Required

Consequence of the Rule. At least one of the following object must be used:

description #
type: string
Optional

This field is intended for Rule management purposes, in particular to ease searching for Rules and presenting them to human readers. It’s not interpreted by the API.

enabled #
type: boolean
default: true
Optional

Whether the Rule is enabled. Disabled Rules remain in the index, but aren’t applied at query time.

validity #
type: list of timeRange
Optional

By default, Rules are permanently valid. When validity periods are specified, the Rule applies only during those periods; it’s ignored the rest of the time. The list must not be empty.

Rule ➔ rule ➔ condition #

pattern #
type: string
Optional

Query pattern syntax

Query patterns are expressed as a string with a specific syntax. A pattern is a sequence of tokens, which can be either:

  • Facet value placeholder: {facet:$facet_name}. Example: {facet:brand}.
  • Literal: the world itself. Example: Algolia.

Special characters (*, {, }, : and \) must be escaped by preceding them with a backslash (\) if they are to be treated as literals.

This parameter goes hand in hand with the anchoring parameter. If you’re creating a Rule that depends on a specific query, you must specify the pattern and anchoring. The empty "" pattern is only allowed when anchoring is set to is.

Otherwise, you can omit both.

anchoring #
type: string, enum
Optional

{ is | startsWith | endsWith | contains }: Whether the pattern parameter must match the beginning or the end of the query string, or both, or none.

This parameter goes hand in hand with the pattern parameter. If you’re creating a Rule that depends on a specific query, you must specify the pattern and anchoring.

Otherwise, you can omit both.

alternatives #
type: boolean
default: false
Optional

Whether the pattern matches on plurals, synonyms, and typos.

This parameter goes hand in hand with the pattern parameter. If the pattern is “shoe” and alternatives is true, the pattern matches on “shoes”, as well as synonyms and typos of “shoe”.

context #
type: string
Optional

Rule context (format: [A-Za-z0-9_-]+). When specified, the Rule is only applied when the same context is specified at query time (using the ruleContexts parameter). When absent, the Rule is generic and always applies (provided that its other conditions are met, of course).

Rule ➔ rule ➔ consequence #

params #
type: params
Optional

Additional search parameters. Any valid search parameter is allowed. Specific treatment is applied to these fields: query, automaticFacetFilters, automaticOptionalFacetFilters.

{
  "query": query,
  "automaticFacetFilters": automaticFacetFilters,
  "automaticOptionalFacetFilters": automaticOptionalFacetFilters
}
promote #
type: list
Optional

Objects to promote as hits. Each object must contain the following fields: objectID or objectIDs, position.

By default, you can promote up to 300 items per rule.

{
  "objectID": objectID,
  "objectIDs": objectIDs,
  "position": position
}
filterPromotes #
type: boolean
default: false
Optional

Only use in combination with the promote consequence. When true, promoted results will be restricted to match the filters of the current search. When false, the promoted results will show up regardless of the filters.

{
  "promote": [
    {
      "objectID": objectID,
      "position": position
    }
  ],
  "filterPromotes": true
}
hide #
type: list
Optional

Objects to hide from hits. Each object must contain an objectID field.

By default, you can hide up to 50 items per rule.

{
  "objectID": objectID
}
userData #
type: object
Optional

Custom JSON object that will be appended to the userData array in the response. This object isn’t interpreted by the API. It’s limited to 1kB of minified JSON.

Rule ➔ rule ➔ consequence ➔ params #

query #
type: string or query
Optional

When providing a string, it replaces the entire query string. When providing an object, it describes incremental edits to be made to the query string (but you can’t do both).

{
  "edits": list of edit
}
automaticFacetFilters #
type: list of automaticFacetFilter
Optional

Names of facets to which automatic filtering must be applied; they must match the facet name of a facet value placeholder in the query pattern. Ex. facetName1, facetName2. You can specify a score: facetName1\<score=5\>, facetName2\<score=1\>.

automaticOptionalFacetFilters #
type: object
Optional

Same syntax as automaticFacetFilters, but the engine treats the filters as optional. Behaves like optionalFilters.

Rule ➔ rule ➔ consequence ➔ promote-params #

objectID #
type: string
true (unless using objectIDs)

Unique identifier of the object to promote.

objectIDs #
type: string[]
true (unless using objectID)

Array of unique identifiers of the objects to promote.

position #
type: integer
Required

The position to promote the objects to (zero-based). If you pass objectIDs, the objects are placed at this position as a group. For example, if you pass four objectIDs to position 0, the objects take the first four positions.

Rule ➔ rule ➔ consequence ➔ hide #

objectID #
type: string
Required

Unique identifier of the object to hide.

Rule ➔ rule ➔ validity ➔ timeRange #

from #
type: integer
Required

Lower bound of the time range (Unix timestamp).

until #
type: integer
Required

Upper bound of the time range (Unix timestamp).

Rule ➔ rule ➔ consequence ➔ params ➔ automaticFacetFilter #

facet #
type: string
Required

Attribute to filter on. This must match a facet placeholder in the Rule’s pattern.

score #
type: integer
default: 1
Optional

Score for the filter. Typically used for optional or disjunctive filters.

disjunctive #
type: boolean
default: false
Optional

Whether the filter is disjunctive (true) or conjunctive (false). If the filter applies multiple times, perhaps because the query string contains multiple values of the same facet, the multiple occurrences are combined with an AND operator by default (conjunctive mode). If the filter is specified as disjunctive, however, multiple occurrences are combined with an OR operator.

negative #
type: boolean
default: false
Optional

Whether the match is inverted (true) or not (false).

Rule ➔ rule ➔ consequence ➔ params ➔ query ➔ edits ➔ edit #

type #
type: string
Required

Type of edit. Must be one of:

  • remove: when you want to delete some text and not replace it with anything
  • replace: when you want to delete some text and replace it with something else
delete #
type: string
Required

Text or patterns to remove from the query string.

[
  {
    "type": "remove",
    "delete": "red"
  }
]
insert #
type: string
Optional

Text that should be inserted in place of the removed text inside the query string.

[
  {
    "type": "replace",
    "delete": "red",
    "insert": "blue"
  }
]

Response # A

This section shows the JSON response returned by the API. Since each language encapsulates this response inside objects specific to that language and/or implementation, the actual type in your language might differ from what’s written here. You can view the response in the logs (using the getLogs method).

JSON format#

1
2
3
4
{
  "updatedAt":"2013-01-18T15:33:13.556Z",
  "taskID": 678
}
updatedAt #
string

Date at which the indexing job has been created.

taskID #
integer

The taskID used with the waitTask method.

Did you find this page helpful?
Ruby v2