Replace All Rules and Synonyms
In Algolia, you can replace all existing rules and synonyms by leveraging the appropriate methods. This guide will show you how to use saveRules
and saveSynonyms
methods to achieve this.
Replace All Rules
To replace all existing rules in Algolia, you can use the saveRules
method with the clearExistingRules
parameter set to true
.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Ruby
- Scala
- Swift
using Algolia.Search.Clients;
using Algolia.Search.Http;
var client = new SearchClient(new SearchConfig("YOUR_APP_ID", "YOUR_API_KEY"));
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"), }
},
},
new Rule
{
ObjectID = "a-second-rule-id",
Conditions = new List<Condition>
{
new Condition { Pattern = "apple", Anchoring = Enum.Parse<Anchoring>("Contains"), }
},
}
},
false,
true
);
await client.WaitForTaskAsync("<YOUR_INDEX_NAME>", response.TaskID);
import 'package:algolia_client_search/algolia_client_search.dart';
final client = SearchClient(appId: 'YOUR_APP_ID', apiKey: 'YOUR_API_KEY');
final response = await client.saveRules(
indexName: "<YOUR_INDEX_NAME>",
rules: [
Rule(
objectID: "a-rule-id",
conditions: [
Condition(
pattern: "smartphone",
anchoring: Anchoring.fromJson("contains"),
),
],
),
Rule(
objectID: "a-second-rule-id",
conditions: [
Condition(
pattern: "apple",
anchoring: Anchoring.fromJson("contains"),
),
],
),
],
forwardToReplicas: false,
clearExistingRules: true,
);
await client.waitTask('<YOUR_INDEX_NAME>', response.taskID);
import "github.com/algolia/algoliasearch-client-go/v4/algolia/search"
client, err := search.NewClient("YOUR_APP_ID", "YOUR_API_KEY")
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"))}), *search.NewEmptyRule().SetObjectID("a-second-rule-id").SetConditions(
[]search.Condition{*search.NewEmptyCondition().SetPattern("apple").SetAnchoring(search.Anchoring("contains"))})},
).WithForwardToReplicas(false).WithClearExistingRules(true))
if err != nil {
// handle the eventual error
panic(err)
}
// use the model directly
print(response)
taskResponse, err := searchClient.WaitForTask("<YOUR_INDEX_NAME>", response.TaskID, nil, nil, nil)
if err != nil {
panic(err)
}
import com.algolia.api.SearchClient;
import com.algolia.model.search.*;
SearchClient client = new SearchClient("YOUR_APP_ID", "YOUR_API_KEY");
client.saveRules(
"<YOUR_INDEX_NAME>",
List.of(
new Rule()
.setObjectID("a-rule-id")
.setConditions(List.of(new Condition().setPattern("smartphone").setAnchoring(Anchoring.CONTAINS))),
new Rule()
.setObjectID("a-second-rule-id")
.setConditions(List.of(new Condition().setPattern("apple").setAnchoring(Anchoring.CONTAINS)))
),
false,
true
);
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
import { searchClient } from '@algolia/client-search';
const client = searchClient('YOUR_APP_ID', 'YOUR_API_KEY');
const response = await client.saveRules({
indexName: '<YOUR_INDEX_NAME>',
rules: [
{
objectID: 'a-rule-id',
conditions: [{ pattern: 'smartphone', anchoring: 'contains' }],
},
{
objectID: 'a-second-rule-id',
conditions: [{ pattern: 'apple', anchoring: 'contains' }],
},
],
forwardToReplicas: false,
clearExistingRules: true,
});
// use typed response
console.log(response);
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID: response.taskID });
import com.algolia.client.api.SearchClient
val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY")
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" },
),
),
),
Rule(
objectID = "a-second-rule-id",
conditions = listOf(
Condition(
pattern = "apple",
anchoring = Anchoring.entries.first { it.value == "contains" },
),
),
),
),
forwardToReplicas = false,
clearExistingRules = true,
)
// Use the response
println(response)
client.waitTask("<YOUR_INDEX_NAME>", response.taskID)
use Algolia\AlgoliaSearch\Api\SearchClient;
$client = SearchClient::create('<YOUR_APP_ID>', '<YOUR_API_KEY>');
$response = $client->saveRules(
'<YOUR_INDEX_NAME>',
[
['objectID' => 'a-rule-id',
'conditions' => [
['pattern' => 'smartphone',
'anchoring' => 'contains',
],
],
],
['objectID' => 'a-second-rule-id',
'conditions' => [
['pattern' => 'apple',
'anchoring' => 'contains',
],
],
],
],
false,
true,
);
// play with the response
var_dump($response);
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);
from algoliasearch.search.client import SearchClient
_client = SearchClient("YOUR_APP_ID", "YOUR_API_KEY")
response = await _client.save_rules(
index_name="<YOUR_INDEX_NAME>",
rules=[
{
"objectID": "a-rule-id",
"conditions": [
{
"pattern": "smartphone",
"anchoring": "contains",
},
],
},
{
"objectID": "a-second-rule-id",
"conditions": [
{
"pattern": "apple",
"anchoring": "contains",
},
],
},
],
forward_to_replicas=False,
clear_existing_rules=True,
)
# use the class directly
print(response)
# print the JSON response
print(response.to_json())
await client.wait_for_task(index_name="<YOUR_INDEX_NAME>", task_id=response.task_id)
require 'algolia'
client = Algolia::SearchClient.create('YOUR_APP_ID', 'YOUR_API_KEY')
response = client.save_rules(
"<YOUR_INDEX_NAME>",
[
Rule.new(
object_id: "a-rule-id",
conditions: [Condition.new(
pattern: "smartphone",
anchoring: 'contains'
)]
),
Rule.new(
object_id: "a-second-rule-id",
conditions: [Condition.new(pattern: "apple", anchoring: 'contains')]
)
],
false,
true
)
# use the class directly
puts response
# print the JSON response
puts response.to_json
client.wait_for_task("<YOUR_INDEX_NAME>", response.task_id)
import algoliasearch.api.SearchClient
val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY")
val response = 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"))
)
)
)
),
Rule(
objectID = "a-second-rule-id",
conditions = Some(
Seq(
Condition(
pattern = Some("apple"),
anchoring = Some(Anchoring.withName("contains"))
)
)
)
)
),
forwardToReplicas = Some(false),
clearExistingRules = Some(true)
)
// Use the response
val value = Await.result(response, Duration(100, "sec"))
client.waitTask("<YOUR_INDEX_NAME>", response.getTaskID())
import Search
let client = try SearchClient(appID: "YOUR_APP_ID", apiKey: "YOUR_API_KEY")
let response = try await client.saveRules(
indexName: "<YOUR_INDEX_NAME>",
rules: [
Rule(
objectID: "a-rule-id",
conditions: [SearchCondition(pattern: "smartphone", anchoring: SearchAnchoring.contains)]
),
Rule(
objectID: "a-second-rule-id",
conditions: [SearchCondition(pattern: "apple", anchoring: SearchAnchoring.contains)]
),
],
forwardToReplicas: false,
clearExistingRules: true
)
try await client.waitForTask(with: response.taskID, in: "<YOUR_INDEX_NAME>")
The rules parameter should contain the array of rules you want to save. By executing the above code, all existing rules will be removed, and the new rules provided will be saved to your Algolia index.
Replace All Synonyms
To replace all existing synonyms in Algolia, you can use the saveSynonyms
method with the replaceExistingSynonyms
parameter set to true
.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Ruby
- Scala
- Swift
var response = await client.SaveSynonymsAsync(
"<YOUR_INDEX_NAME>",
new List<SynonymHit>
{
new SynonymHit
{
ObjectID = "id1",
Type = Enum.Parse<SynonymType>("Synonym"),
Synonyms = new List<string> { "car", "vehicule", "auto" },
},
new SynonymHit
{
ObjectID = "id2",
Type = Enum.Parse<SynonymType>("Onewaysynonym"),
Input = "iphone",
Synonyms = new List<string> { "ephone", "aphone", "yphone" },
}
},
true,
true
);
await client.WaitForTaskAsync("<YOUR_INDEX_NAME>", response.TaskID);
final response = await client.saveSynonyms(
indexName: "<YOUR_INDEX_NAME>",
synonymHit: [
SynonymHit(
objectID: "id1",
type: SynonymType.fromJson("synonym"),
synonyms: [
"car",
"vehicule",
"auto",
],
),
SynonymHit(
objectID: "id2",
type: SynonymType.fromJson("onewaysynonym"),
input: "iphone",
synonyms: [
"ephone",
"aphone",
"yphone",
],
),
],
forwardToReplicas: true,
replaceExistingSynonyms: true,
);
await client.waitTask('<YOUR_INDEX_NAME>', response.taskID);
response, err := client.SaveSynonyms(client.NewApiSaveSynonymsRequest(
"<YOUR_INDEX_NAME>",
[]search.SynonymHit{*search.NewEmptySynonymHit().SetObjectID("id1").SetType(search.SynonymType("synonym")).SetSynonyms(
[]string{"car", "vehicule", "auto"}), *search.NewEmptySynonymHit().SetObjectID("id2").SetType(search.SynonymType("onewaysynonym")).SetInput("iphone").SetSynonyms(
[]string{"ephone", "aphone", "yphone"})},
).WithForwardToReplicas(true).WithReplaceExistingSynonyms(true))
if err != nil {
// handle the eventual error
panic(err)
}
// use the model directly
print(response)
taskResponse, err := searchClient.WaitForTask("<YOUR_INDEX_NAME>", response.TaskID, nil, nil, nil)
if err != nil {
panic(err)
}
client.saveSynonyms(
"<YOUR_INDEX_NAME>",
List.of(
new SynonymHit().setObjectID("id1").setType(SynonymType.SYNONYM).setSynonyms(List.of("car", "vehicule", "auto")),
new SynonymHit()
.setObjectID("id2")
.setType(SynonymType.ONEWAYSYNONYM)
.setInput("iphone")
.setSynonyms(List.of("ephone", "aphone", "yphone"))
),
true,
true
);
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
const response = await client.saveSynonyms({
indexName: '<YOUR_INDEX_NAME>',
synonymHit: [
{
objectID: 'id1',
type: 'synonym',
synonyms: ['car', 'vehicule', 'auto'],
},
{
objectID: 'id2',
type: 'onewaysynonym',
input: 'iphone',
synonyms: ['ephone', 'aphone', 'yphone'],
},
],
forwardToReplicas: true,
replaceExistingSynonyms: true,
});
// use typed response
console.log(response);
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID: response.taskID });
var response = client.saveSynonyms(
indexName = "<YOUR_INDEX_NAME>",
synonymHit = listOf(
SynonymHit(
objectID = "id1",
type = SynonymType.entries.first { it.value == "synonym" },
synonyms = listOf("car", "vehicule", "auto"),
),
SynonymHit(
objectID = "id2",
type = SynonymType.entries.first { it.value == "onewaysynonym" },
input = "iphone",
synonyms = listOf("ephone", "aphone", "yphone"),
),
),
forwardToReplicas = true,
replaceExistingSynonyms = true,
)
// Use the response
println(response)
client.waitTask("<YOUR_INDEX_NAME>", response.taskID)
$response = $client->saveSynonyms(
'<YOUR_INDEX_NAME>',
[
['objectID' => 'id1',
'type' => 'synonym',
'synonyms' => [
'car',
'vehicule',
'auto',
],
],
['objectID' => 'id2',
'type' => 'onewaysynonym',
'input' => 'iphone',
'synonyms' => [
'ephone',
'aphone',
'yphone',
],
],
],
true,
true,
);
// play with the response
var_dump($response);
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);
response = await _client.save_synonyms(
index_name="<YOUR_INDEX_NAME>",
synonym_hit=[
{
"objectID": "id1",
"type": "synonym",
"synonyms": [
"car",
"vehicule",
"auto",
],
},
{
"objectID": "id2",
"type": "onewaysynonym",
"input": "iphone",
"synonyms": [
"ephone",
"aphone",
"yphone",
],
},
],
forward_to_replicas=True,
replace_existing_synonyms=True,
)
# use the class directly
print(response)
# print the JSON response
print(response.to_json())
await client.wait_for_task(index_name="<YOUR_INDEX_NAME>", task_id=response.task_id)
response = client.save_synonyms(
"<YOUR_INDEX_NAME>",
[SynonymHit.new(object_id: "id1", type: 'synonym', synonyms: ["car", "vehicule", "auto"]),
SynonymHit.new(
object_id: "id2",
type: 'onewaysynonym',
input: "iphone",
synonyms: ["ephone", "aphone", "yphone"]
)],
true,
true
)
# use the class directly
puts response
# print the JSON response
puts response.to_json
client.wait_for_task("<YOUR_INDEX_NAME>", response.task_id)
val response = client.saveSynonyms(
indexName = "<YOUR_INDEX_NAME>",
synonymHit = Seq(
SynonymHit(
objectID = "id1",
`type` = SynonymType.withName("synonym"),
synonyms = Some(Seq("car", "vehicule", "auto"))
),
SynonymHit(
objectID = "id2",
`type` = SynonymType.withName("onewaysynonym"),
input = Some("iphone"),
synonyms = Some(Seq("ephone", "aphone", "yphone"))
)
),
forwardToReplicas = Some(true),
replaceExistingSynonyms = Some(true)
)
// Use the response
val value = Await.result(response, Duration(100, "sec"))
client.waitTask("<YOUR_INDEX_NAME>", response.getTaskID())
let response = try await client.saveSynonyms(
indexName: "<YOUR_INDEX_NAME>",
synonymHit: [
SynonymHit(objectID: "id1", type: SynonymType.synonym, synonyms: ["car", "vehicule", "auto"]),
SynonymHit(
objectID: "id2",
type: SynonymType.onewaysynonym,
synonyms: ["ephone", "aphone", "yphone"],
input: "iphone"
),
],
forwardToReplicas: true,
replaceExistingSynonyms: true
)
try await client.waitForTask(with: response.taskID, in: "<YOUR_INDEX_NAME>")
The synonymHit
parameter should contain the synonyms you want to save.
By executing the above code, all existing synonyms will be replaced with the new synonyms provided.
That’s it! You have learned how to replace all existing rules and synonyms in Algolia using the saveRules
and saveSynonyms
methods respectively.