API Reference / API Methods / Synonyms / Save 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
])

About this method # A

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.

Note that each synonym object counts as a single indexing operation.

Examples # A

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" => "a-unique-identifier",
      "type" => "synonym",
      "synonyms" => ["car", "vehicle", "auto"]
    ],
    [
      "objectID" => "another-unique-identifier",
      "type" => "synonym",
      "synonyms" => ["street", "st"]
    ]
  ], [
    'forwardToReplicas' => true,
    'replaceExistingSynonyms' => true
  ]
);

Parameters # A

synonyms #
type: list
Required

Array of synonym.

forwardToReplicas #
type: boolean
default: false
optional

Sends synonyms to all replicas.

Without this parameter, or by setting it to false, the method will apply the change only to the specified index. If you want to forward your synonyms to replicas you will need to set this parameter to true.

replaceExistingSynonyms #
type: boolean
default: false
optional

Forces the engine to replace all synonyms, using an atomic save.

Normally, to replace all synonyms on an index, you would first clear the synonyms, using clearAllSynonyms, and then create a new list. However, between the clear and the add, your index will have no synonyms. This is where replaceExistingSynonyms comes into play.

By adding this parameter, you do not need to use clearAllSynonyms, it’s done for you. This parameter tells the engine to delete all existing synonyms before recreating a new list from the synonyms listed in the current call. This is the only way to avoid having no synonyms, ensuring that your index will always provide a full list of synonyms to your end users.

Response # A

This section shows the JSON response returned by the API. Since each language encapsulates this response inside objects specific to that language and/or implementation, the actual type in your language might differ from what’s written here. You can view the response in the logs (using the getLogs method).

JSON format#

1
2
3
4
{
  "updatedAt":"2013-01-18T15:33:13.556Z",
  "taskID": 678
}
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?
PHP v3