Algolia DevCon
Oct. 2–3 2024, virtual.
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
);

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

Copy an index, including its records, synonyms, rules, and settings.

You can copy the entire index (records, settings, synonyms, and rules) or one or more of the following scopes:

  • Settings
  • Synonyms
  • Rules

This method does not copy the enableReRanking and mode settings.

Rate limiting

Copying an index is rate-limited:

  • If you have more than 100 pending requests, your requests are throttled.
  • If you have more than 5,000 pending requests, further requests are ignored.

Source indices

Copying a source index that doesn’t exist creates a new index with 0 records. If the source index has replicas, the replicas won’t be copied.

Destination indices

When copying indices within the same Algolia application, the destination index is replaced if it exists.

You can’t copy an index between two Algolia applications, if the destination index exists.

Everything apart from the analytics data is replaced. You can’t copy to a destination index that already has replicas.

Analytics

Copying an index has no impact on Analytics. You can’t copy an index’s analytics data.

Scopes

To copy parts of your source index, use the scope parameter. If you omit the scope parameter, everything is copied.

For example, to copy an index’s settings and synonyms, but not records and rules, set the scope parameter to: ["settings", "synonyms"].

  • The scope is replaced completely. Different items belonging to the same scope are not merged. For example, with scope: "settings", all settings of the destination index are replaced with the settings of the source index.

  • Items in different scopes are preserved.
  • If you set the scope parameter, records aren’t copied.

Examples

Read the Algolia CLI documentation for more information.

Copy an index

1
2
3
4
5
6
7
8
9
10
11
<?php
require_once __DIR__."/vendor/autoload.php";
use Algolia\AlgoliaSearch\SearchClient;

// Use an API key with `addObject` ACL
$client = SearchClient::create(
  'YourApplicationID', 'YourAPIKey'
);

// Copy `indexNameSrc` to `indexNameDest`
$client->copyIndex('indexNameSrc', 'indexNameDest');

Copy parts of an index with scopes

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
require_once __DIR__."/vendor/autoload.php";
use Algolia\AlgoliaSearch\SearchClient;

// Use an API key with `addObject` ACL
$client = SearchClient::create(
  'YourApplicationID', 'YourAPIKey'
);

// Copy only settings and synonyms from `indexNameSrc` to `indexNameDest`
$client->copyIndex('indexNameSrc', 'indexNameDest', [
  'scope' => ['settings', 'synonyms']
]);

Copy index between apps

1
2
3
4
5
6
7
8
9
use \Algolia\AlgoliaSearch\SearchClient;
use \Algolia\AlgoliaSearch\AccountClient;

// Copy `indexNameSrc` from app `SOURCE_APP_ID` to app `TARGET_APP_ID`
$sourceIndex = SearchClient::create('SOURCE_APP_ID', 'SOURCE_API_KEY')->initIndex('indexNameSrc');
$targetIndex = SearchClient::create('TARGET_APP_ID', 'TARGET_API_KEY')->initIndex('indexNameDest');

// This method returns an error if `$targetIndex` exists
AccountClient::copyIndex($sourceIndex, $targetIndex);

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

An array containing any combination of the following strings:

  • settings
  • synonyms
  • rules

See more on how this parameter works.

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": "2017-12-18T21:22:40.761Z",
  "taskID": 19541511530
}
Field Description
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?