> ## 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 replace a rule

> If a rule with the specified object ID doesn't exist, it's created. Otherwise, the existing rule is replaced.

**Required ACL:** `editSettings`

To create or update more than one rule, use the [`batch` operation](https://www.algolia.com/doc/rest-api/search/save-rules).

## 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.SaveRuleAsync(
    "<YOUR_INDEX_NAME>",
    "id1",
    new Rule
    {
      ObjectID = "id1",
      Conditions = new List<Condition>
      {
        new Condition { Pattern = "apple", Anchoring = Enum.Parse<Anchoring>("Contains") },
      },
      Consequence = new Consequence
      {
        Params = new ConsequenceParams { Filters = "brand:xiaomi" },
      },
    }
  );

  // 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.saveRule(
    indexName: "<YOUR_INDEX_NAME>",
    objectID: "id1",
    rule: Rule(
      objectID: "id1",
      conditions: [
        Condition(
          pattern: "apple",
          anchoring: Anchoring.fromJson("contains"),
        ),
      ],
      consequence: Consequence(
        params: ConsequenceParams(
          filters: "brand:xiaomi",
        ),
      ),
    ),
  );

  // 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.SaveRule(client.NewApiSaveRuleRequest(
    "<YOUR_INDEX_NAME>", "id1",
    search.NewEmptyRule().SetObjectID("id1").SetConditions(
      []search.Condition{*search.NewEmptyCondition().SetPattern("apple").SetAnchoring(search.Anchoring("contains"))}).SetConsequence(
      search.NewEmptyConsequence().SetParams(
        search.NewEmptyConsequenceParams().SetFilters("brand:xiaomi")))))
  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.saveRule(
    "<YOUR_INDEX_NAME>",
    "id1",
    new Rule()
      .setObjectID("id1")
      .setConditions(Arrays.asList(new Condition().setPattern("apple").setAnchoring(Anchoring.CONTAINS)))
      .setConsequence(new Consequence().setParams(new ConsequenceParams().setFilters("brand:xiaomi")))
  );

  // 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.saveRule({
    indexName: 'indexName',
    objectID: 'id1',
    rule: {
      objectID: 'id1',
      conditions: [{ pattern: 'apple', anchoring: 'contains' }],
      consequence: { params: { filters: 'brand:xiaomi' } },
    },
  });


  // 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.saveRule(
      indexName = "<YOUR_INDEX_NAME>",
      objectID = "id1",
      rule =
        Rule(
          objectID = "id1",
          conditions =
            listOf(
              Condition(
                pattern = "apple",
                anchoring = Anchoring.entries.first { it.value == "contains" },
              )
            ),
          consequence = Consequence(params = ConsequenceParams(filters = "brand:xiaomi")),
        ),
    )


  // 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->saveRule(
      '<YOUR_INDEX_NAME>',
      'id1',
      ['objectID' => 'id1',
          'conditions' => [
              ['pattern' => 'apple',
                  'anchoring' => 'contains',
              ],
          ],
          'consequence' => ['params' => ['filters' => 'brand:xiaomi',
          ],
          ],
      ],
  );


  // 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_rule(
      index_name="<YOUR_INDEX_NAME>",
      object_id="id1",
      rule={
          "objectID": "id1",
          "conditions": [
              {
                  "pattern": "apple",
                  "anchoring": "contains",
              },
          ],
          "consequence": {
              "params": {
                  "filters": "brand:xiaomi",
              },
          },
      },
  )


  # 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_rule(
    "<YOUR_INDEX_NAME>",
    "id1",
    Algolia::Search::Rule.new(
      algolia_object_id: "id1",
      conditions: [Algolia::Search::Condition.new(pattern: "apple", anchoring: "contains")],
      consequence: Algolia::Search::Consequence.new(
        params: Algolia::Search::ConsequenceParams.new(filters: "brand:xiaomi")
      )
    )
  )


  # 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.saveRule(
      indexName = "<YOUR_INDEX_NAME>",
      objectID = "id1",
      rule = Rule(
        objectID = "id1",
        conditions = Some(
          Seq(
            Condition(
              pattern = Some("apple"),
              anchoring = Some(Anchoring.withName("contains"))
            )
          )
        ),
        consequence = Consequence(
          params = Some(
            ConsequenceParams(
              filters = Some("brand:xiaomi")
            )
          )
        )
      )
    ),
    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.saveRule(
      indexName: "<YOUR_INDEX_NAME>",
      objectID: "id1",
      rule: Rule(
          objectID: "id1",
          conditions: [SearchCondition(pattern: "apple", anchoring: SearchAnchoring.contains)],
          consequence: SearchConsequence(params: SearchConsequenceParams(filters: "brand:xiaomi"))
      )
  )

  // 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-rule">
  For more details about input parameters
  and response fields.
</Card>
