Skip to main content
Required ACL: editSettings To create or update more than one rule, use the batch operation.

Usage

// 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);
// 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);
// 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)
// 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);
// 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);
// 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)
// 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);
# 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)
# 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)
// 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)
// 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)

See the full API reference

For more details about input parameters and response fields.
Last modified on March 25, 2026