API Reference / API Methods / Manage indices / Copy index
Feb. 26, 2019

Copy index

Required API Key: any key with the addObject ACL
Method signature
$client->copyIndex(
  string indexNameSrc,
  string indexNameDest
)
$client->copyIndex(
  string indexNameSrc,
  string indexNameDest,
  [
    'scope' => array
  ]
);

// Copy index in between apps
\Algolia\AlgoliaSearch\AccountClient::copyIndex(
  SearchIndex indexSrc,
  SearchIndex indexDest
);

About this method

Make a copy of an index, including its objects, settings, synonyms, and query rules.

This method enables you to copy the entire index (objects, settings, synonyms, and rules) OR one or more of the following index elements:

  • settings
  • synonyms
  • and rules (query rules)

You can control which of these are copied by using the scope parameter.

The copy command will overwrite the destination index. This means everything will be lost in the destination index except its API keys and Analytics data.

Regarding the API Keys, the source’s API Keys will be merged with the existing keys of the destination index.

Copying an index will have no impact on Analytics data because you cannot copy an index’s Analytics data.

Regarding replicas:

  • Copying an index having replicas is possible, but the replicas will not be copied, only the data. The destination index will not have replicas.
  • Copying to a existing index having replicas is not possible.

Examples

Copy an index

1
2
// Copy indexNameSrc to indexNameDest
$res = $client->copyIndex('indexNameSrc', 'indexNameDest');

Copy resources between indices

1
2
3
4
// Copy settings and synonyms (but not rules, nor records) from the current index to "indexNameDest".
$index->copyTo('indexNameDest', [
  'scope' => ['settings', 'synonyms']
]);

Copy index in between app

1
2
3
4
5
6
$index1 = \Algolia\AlgoliaSearch\SearchClient('APP_ID_1', 'API_KEY_1')
            ->initIndex('index_name_1');
$index2 = \Algolia\AlgoliaSearch\SearchClient('APP_ID_2', 'API_KEY_2')
            ->initIndex('index_name_2');

\Algolia\AlgoliaSearch\AccountClient::copyIndex($index1, $index2);

Parameters

indexNameSrc
type: string
Required

Name of the source index to copy.

indexNameDest
type: string
Required

Name of the destination index.

indexSrc
type: object
Required

Source index object.

indexDest
type: object
Required

Destination index object.

scope
type: list
Optional

The “scope” parameter is an array of strings that refer to the following items:

  • settings
  • synonyms
  • rules (as in Query Rules)

If you omit the scope parameter, then all objects and all scope items are copied.

But if you use the scope parameter, you will no longer be copying records (objects); instead, you will be copying specified scopes.

For example, if you specify {“settings”, “synonyms”}, then only those 2 items will be copied - not the “rules” and not the objects. On the other hand, if you do not specify a scope - that is, you omit the scope parameter - then the copy command will work as by default: copying all objects, settings, synonyms, and rules.

Note: using scope has the following 2 consequences:

  • the copied items fully replace the corresponding items in the destination
  • the destination retains the items that were not copied. In other words, scope merges source and destination, and resolves all conflicts in favor of the source.

Response

In this section we document the JSON response returned by the API. Each language will encapsulate this response inside objects specific to the language and/or the implementation. So the actual type in your language might differ from what is documented.

JSON format

1
2
3
4
{
  "updatedAt": "2017-12-18T21:22:40.761Z",
  "taskID": 19541511530
}
updatedAt
date string

Date at which the job to copy the index has been created.

taskID
integer

This is the taskID which is used with the waitTask method.

Note: You can use either the source or destination index to wait on the resulting taskID. In either case, the wait will include the full copy process.

Did you find this page helpful?