Create or update rules

Create or update multiple rules.

If a rule with the specified object ID doesn’t exist, Algolia creates a new one. Otherwise, existing rules are replaced.

This operation is subject to indexing rate limits.

Usage

Required ACL: editSettings
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
32
33
34
35
36
37
38
39
40
from algoliasearch.search.client import SearchClientSync

# In an asynchronous context, you can use SearchClient instead, which exposes the exact same methods.
client = SearchClientSync("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")

response = client.save_rules(
    index_name="ALGOLIA_INDEX_NAME",
    rules=[
        {
            "objectID": "a-rule-id",
            "conditions": [
                {
                    "pattern": "smartphone",
                    "anchoring": "contains",
                },
            ],
            "consequence": {
                "params": {
                    "filters": "brand:apple",
                },
            },
        },
        {
            "objectID": "a-second-rule-id",
            "conditions": [
                {
                    "pattern": "apple",
                    "anchoring": "contains",
                },
            ],
            "consequence": {
                "params": {
                    "filters": "brand:samsung",
                },
            },
        },
    ],
    forward_to_replicas=False,
    clear_existing_rules=True,
)
Did you find this page helpful?
Python API clients v4