> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create or update rules

> Create or update multiple rules.

**Required ACL:** `editSettings`

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](https://support.algolia.com/hc/articles/4406975251089-Is-there-a-rate-limit-for-indexing-on-Algolia).

## Usage

<CodeGroup>
  ```cs C# theme={"system"}
  // Initialize the client
  var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

  // Call the API
  var response = await client.SaveRulesAsync(
    "<YOUR_INDEX_NAME>",
    new List<Rule>
    {
      new Rule
      {
        ObjectID = "a-rule-id",
        Conditions = new List<Condition>
        {
          new Condition { Pattern = "smartphone", Anchoring = Enum.Parse<Anchoring>("Contains") },
        },
        Consequence = new Consequence
        {
          Params = new ConsequenceParams { Filters = "brand:apple" },
        },
      },
      new Rule
      {
        ObjectID = "a-second-rule-id",
        Conditions = new List<Condition>
        {
          new Condition { Pattern = "apple", Anchoring = Enum.Parse<Anchoring>("Contains") },
        },
        Consequence = new Consequence
        {
          Params = new ConsequenceParams { Filters = "brand:samsung" },
        },
      },
    },
    false,
    true
  );

  // print the response
  Console.WriteLine(response);
  ```

  ```dart Dart theme={"system"}
  // Initialize the client
  final client =
      SearchClient(appId: 'ALGOLIA_APPLICATION_ID', apiKey: 'ALGOLIA_API_KEY');

  // Call the API
  final response = await client.saveRules(
    indexName: "<YOUR_INDEX_NAME>",
    rules: [
      Rule(
        objectID: "a-rule-id",
        conditions: [
          Condition(
            pattern: "smartphone",
            anchoring: Anchoring.fromJson("contains"),
          ),
        ],
        consequence: Consequence(
          params: ConsequenceParams(
            filters: "brand:apple",
          ),
        ),
      ),
      Rule(
        objectID: "a-second-rule-id",
        conditions: [
          Condition(
            pattern: "apple",
            anchoring: Anchoring.fromJson("contains"),
          ),
        ],
        consequence: Consequence(
          params: ConsequenceParams(
            filters: "brand:samsung",
          ),
        ),
      ),
    ],
    forwardToReplicas: false,
    clearExistingRules: true,
  );

  // print the response
  print(response);
  ```

  ```go Go theme={"system"}
  // Initialize the client
  client, err := search.NewClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
  if err != nil {
    // The client can fail to initialize if you pass an invalid parameter.
    panic(err)
  }

  // Call the API
  response, err := client.SaveRules(client.NewApiSaveRulesRequest(
    "<YOUR_INDEX_NAME>",
    []search.Rule{*search.NewEmptyRule().SetObjectID("a-rule-id").SetConditions(
      []search.Condition{*search.NewEmptyCondition().SetPattern("smartphone").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
      search.NewEmptyConsequence().SetParams(
        search.NewEmptyConsequenceParams().SetFilters("brand:apple"))), *search.NewEmptyRule().SetObjectID("a-second-rule-id").SetConditions(
      []search.Condition{*search.NewEmptyCondition().SetPattern("apple").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
      search.NewEmptyConsequence().SetParams(
        search.NewEmptyConsequenceParams().SetFilters("brand:samsung")))}).WithForwardToReplicas(false).WithClearExistingRules(true))
  if err != nil {
    // handle the eventual error
    panic(err)
  }


  // print the response
  print(response)
  ```

  ```java Java theme={"system"}
  // Initialize the client
  SearchClient client = new SearchClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");

  // Call the API
  UpdatedAtResponse response = client.saveRules(
    "<YOUR_INDEX_NAME>",
    Arrays.asList(
      new Rule()
        .setObjectID("a-rule-id")
        .setConditions(Arrays.asList(new Condition().setPattern("smartphone").setAnchoring(Anchoring.CONTAINS)))
        .setConsequence(new Consequence().setParams(new ConsequenceParams().setFilters("brand:apple"))),
      new Rule()
        .setObjectID("a-second-rule-id")
        .setConditions(Arrays.asList(new Condition().setPattern("apple").setAnchoring(Anchoring.CONTAINS)))
        .setConsequence(new Consequence().setParams(new ConsequenceParams().setFilters("brand:samsung")))
    ),
    false,
    true
  );

  // print the response
  System.out.println(response);
  ```

  ```js JavaScript theme={"system"}
  // Initialize the client
  const client = algoliasearch('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY');

  // Call the API
  const response = await client.saveRules({
    indexName: '<YOUR_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' } },
      },
    ],
    forwardToReplicas: false,
    clearExistingRules: true,
  });


  // print the response
  console.log(response);
  ```

  ```kotlin Kotlin theme={"system"}
  // Initialize the client
  val client = SearchClient(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY")

  // Call the API
  var response =
    client.saveRules(
      indexName = "<YOUR_INDEX_NAME>",
      rules =
        listOf(
          Rule(
            objectID = "a-rule-id",
            conditions =
              listOf(
                Condition(
                  pattern = "smartphone",
                  anchoring = Anchoring.entries.first { it.value == "contains" },
                )
              ),
            consequence = Consequence(params = ConsequenceParams(filters = "brand:apple")),
          ),
          Rule(
            objectID = "a-second-rule-id",
            conditions =
              listOf(
                Condition(
                  pattern = "apple",
                  anchoring = Anchoring.entries.first { it.value == "contains" },
                )
              ),
            consequence = Consequence(params = ConsequenceParams(filters = "brand:samsung")),
          ),
        ),
      forwardToReplicas = false,
      clearExistingRules = true,
    )


  // print the response
  println(response)
  ```

  ```php PHP theme={"system"}
  // Initialize the client
  $client = SearchClient::create('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY');

  // Call the API
  $response = $client->saveRules(
      '<YOUR_INDEX_NAME>',
      [
          ['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',
              ],
              ],
          ],
      ],
      false,
      true,
  );


  // print the response
  var_dump($response);
  ```

  ```python Python theme={"system"}
  # Initialize the client
  # In an asynchronous context, you can use SearchClient instead, which exposes the exact same methods.
  client = SearchClientSync("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")

  # Call the API
  response = client.save_rules(
      index_name="<YOUR_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,
  )


  # print the response
  print(response)
  ```

  ```ruby Ruby theme={"system"}
  # Initialize the client
  client = Algolia::SearchClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")

  # Call the API
  response = client.save_rules(
    "<YOUR_INDEX_NAME>",
    [
      Algolia::Search::Rule.new(
        algolia_object_id: "a-rule-id",
        conditions: [Algolia::Search::Condition.new(pattern: "smartphone", anchoring: "contains")],
        consequence: Algolia::Search::Consequence.new(
          params: Algolia::Search::ConsequenceParams.new(filters: "brand:apple")
        )
      ),
      Algolia::Search::Rule.new(
        algolia_object_id: "a-second-rule-id",
        conditions: [Algolia::Search::Condition.new(pattern: "apple", anchoring: "contains")],
        consequence: Algolia::Search::Consequence.new(
          params: Algolia::Search::ConsequenceParams.new(filters: "brand:samsung")
        )
      )
    ],
    false,
    true
  )


  # print the response
  puts(response)
  ```

  ```scala Scala theme={"system"}
  // Initialize the client
  val client = SearchClient(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY")

  // Call the API
  val response = Await.result(
    client.saveRules(
      indexName = "<YOUR_INDEX_NAME>",
      rules = Seq(
        Rule(
          objectID = "a-rule-id",
          conditions = Some(
            Seq(
              Condition(
                pattern = Some("smartphone"),
                anchoring = Some(Anchoring.withName("contains"))
              )
            )
          ),
          consequence = Consequence(
            params = Some(
              ConsequenceParams(
                filters = Some("brand:apple")
              )
            )
          )
        ),
        Rule(
          objectID = "a-second-rule-id",
          conditions = Some(
            Seq(
              Condition(
                pattern = Some("apple"),
                anchoring = Some(Anchoring.withName("contains"))
              )
            )
          ),
          consequence = Consequence(
            params = Some(
              ConsequenceParams(
                filters = Some("brand:samsung")
              )
            )
          )
        )
      ),
      forwardToReplicas = Some(false),
      clearExistingRules = Some(true)
    ),
    Duration(100, "sec")
  )

  // print the response
  println(response)
  ```

  ```swift Swift theme={"system"}
  // Initialize the client
  let client = try SearchClient(appID: "ALGOLIA_APPLICATION_ID", apiKey: "ALGOLIA_API_KEY")

  // Call the API
  let response = try await client.saveRules(
      indexName: "<YOUR_INDEX_NAME>",
      rules: [
          Rule(objectID: "a-rule-id", conditions: [SearchCondition(
              pattern: "smartphone",
              anchoring: SearchAnchoring.contains
          )], consequence: SearchConsequence(params: SearchConsequenceParams(filters: "brand:apple"))),
          Rule(
              objectID: "a-second-rule-id",
              conditions: [SearchCondition(pattern: "apple", anchoring: SearchAnchoring.contains)],
              consequence: SearchConsequence(params: SearchConsequenceParams(filters: "brand:samsung"))
          ),
      ],
      forwardToReplicas: false,
      clearExistingRules: true
  )

  // print the response
  print(response)
  ```
</CodeGroup>

<Card icon="folder-code" horizontal="true" title="See the full API reference" arrow="true" href="/doc/rest-api/search/save-rules">
  For more details about input parameters
  and response fields.
</Card>
