API client / Methods / Synonyms
Required API Key: any key with the editSettings ACL
Method signature
$index->saveSynonyms(array synonyms)

$index->saveSynonyms(array synonyms), [
  // All the following parameters are optional
  'forwardToReplicas' => boolean,
  'replaceExistingSynonyms' => boolean
])

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

About this method

Create or update multiple synonyms.

This method enables you to create or update one or more synonyms in a single call.

You can also recreate your entire set of synonyms by using the replaceExistingSynonyms parameter.

Each synonym object counts as a single indexing operation.

Examples

Read the Algolia CLI documentation for more information.

Save synonyms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Batch synonyms,
// with replica forwarding and atomic replacement of existing synonyms

$index->saveSynonyms(
  [
    [
      "objectID" => "synonymID1",
      "type" => "synonym",
      "synonyms" => ["car", "vehicle", "auto"]
    ],
    [
      "objectID" => "synonymID2",
      "type" => "synonym",
      "synonyms" => ["street", "st"]
    ]
  ], [
    'forwardToReplicas' => true,
    'replaceExistingSynonyms' => true
  ]
);

Parameters

Parameter Description
synonyms
type: list
Required

Array of synonym.

forwardToReplicas
type: boolean
default: false
optional

Sends synonyms to all replicas. Set this parameter to:

  • false to apply the change only to the specified index.
  • true to forward your synonyms to replicas.
replaceExistingSynonyms
type: boolean
default: false
optional

This parameter forces the engine to replace all synonyms.

You can first clear the existing synonyms, using the clearSynonyms method, and then create a new list. However, between clearing and adding the synonyms, your index will have no synonyms.

The replaceExistingSynonyms parameter tells the engine to delete all existing synonyms before recreating new ones from the list in the current call. You don’t need to use clearAllSynonyms. `replaceExistingSynonyms is the only way to ensure that your index will always provide synonyms to your users.

Response

This section shows the JSON response returned by the API. Each API client encapsulates this response inside objects specific to the programming language, so that the actual response might be different. You can view the response by using the getLogs method. Don’t rely on the order of attributes in the response, as JSON doesn’t guarantee the ordering of keys in objects.

JSON format

1
2
3
4
{
  "updatedAt":"2013-01-18T15:33:13.556Z",
  "taskID": 678
}
Field Description
updatedAt
string

Date at which the indexing job has been created.

taskID
integer

The taskID used with the waitTask method.

Did you find this page helpful?