This version of the JavaScript API client has been deprecated in favor of the latest version of the JavaScript API client.
Required API Key: any key with the admin
ACL
About this method
Clears all objects from your index and replaces them with a new set of objects.
Only your objects are replaced: all settings, synonyms, and query rules are untouched.
This method performs an atomic reindex: it replaces all records in an index without any downtime.
This method uses a temporary index. First, it copies your index’s settings, synonyms, and query rules to the temporary index.
Then, it adds the objects you passed to the temporary index. Finally, it replaces your index with the temporary one.
Using this method can significantly increase your indexing operations count. It costs the number of new records + 2 operations (copySettings and moveIndex). For example, replacing all objects of an index with a new set of a million objects costs one million (and two) operations. If you’re on a Free plan, make sure you don’t exceed your record limit. If you’re on a paid plan, be careful of the impact on your operations count.
Behind the scenes, using this method will generate a new, temporary index. If the API key used has restricted index access, the API will return an error when attempting this operation. To fix this, make sure your API key has access to yourIndex
and yourIndex_tmp_*
.
Examples
Replace all objects
1
2
3
4
5
6
7
8
9
| $client = Algolia\AlgoliaSearch\SearchClient::create(
'YourApplicationID',
'YourWriteAPIKey'
);
$objects = /* Fetch your objects */;
$index = $client->initIndex('your_index_name');
$index->replaceAllObjects($objects);
|
1
2
3
4
5
6
7
8
9
| client = Algolia::Client.new({
:application_id => 'YourApplicationID',
:api_key => 'YourWriteAPIKey'
})
objects = [] # Fetch your objects
index = client.init_index('your_index_name')
index.replace_all_objects(objects)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| const client = algoliasearch('YourApplicationID', 'YourWriteAPIKey');
// 1. Initialize the target and temporary indices
const index = client.initIndex('atomic_reindex');
const tmpIndex = client.initIndex('atomic_reindex_tmp');
// 2. Copy the settings, synonyms and rules (but not the records)
client
.copyIndex(index.indexName, tmpIndex.indexName, [
'settings',
'synonyms',
'rules'
])
.then(() => {
// 3. Fetch your data and push it to the temporary index
const objects = [/* Get your data */];
return tmpIndex.addObjects(objects); // for better performance, send your objects in batches
})
.then(() =>
// 4. Move the temporary index to the target index
client.moveIndex(tmpIndex.indexName, index.indexName)
)
.catch(err => {
console.error(err);
});
|
1
2
3
4
5
6
| client = SearchClient.create('YourApplicationID', 'YourWriteAPIKey')
objects = [] # Fetch your objects
index = client.init_index('your_index_name')
index.replace_all_objects(objects)
|
1
2
3
4
5
| var client = new SearchClient("YourApplicationID", "YourWriteAPIKey");
var index = client.InitIndex("your_index_name");
var objects = /* Fetch your objects */;
index.ReplaceAllObjects(objects);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| SearchClient client =
DefaultSearchClient.create("YourApplicationID", "YourWriteAPIKey");
SearchIndex<Contact> index = client.initIndex("your_index_name", Contact.class);
// Fetch your objects
List<Contact> objects = new ArrayList<>();
// Sync version
index.replaceAllObjects(objects);
// Async version
index.replaceAllObjectsAsync(objects);
|
1
2
3
4
5
6
7
8
9
10
11
12
| package main
import "github.com/algolia/algoliasearch-client-go/v3/algolia/search"
func main() {
client := search.NewClient("YourApplicationID", "YourWriteAPIKey")
index := client.InitIndex("your_index_name")
objects := []Object{ /* Fetch your objects */ }
res, err := index.ReplaceAllObjects(objects)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| import algolia.AlgoliaClient
import algolia.AlgoliaDsl._
import algolia.responses._
import scala.concurrent.duration._
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future}
case class MyCaseClass(objectID: String, /* ... */) extends ObjectID
object Main {
def main(args: Array[String]): Unit = {
implicit val ec: ExecutionContextExecutor = ExecutionContext.global
implicit val awaitDuration: FiniteDuration = 10 seconds
val client = new AlgoliaClient("YourApplicationID", "YourWriteAPIKey")
val tmpIndexName = "atomic_reindex_tmp"
val indexName = "atomic_reindex"
// 1. Copy the settings, synonyms and rules (but not the records)
// of the target index into the temporary index
val copyTask = Await.result(
client.execute(copy index indexName to tmpIndexName scope Seq("settings", "synonyms", "rules")),
awaitDuration
)
client.execute(waitFor task copyTask from indexName)
// 2. Fetch your data and push it to the temporary index
val objects: Seq[MyCaseClass] = Seq(/* ... */)
// Here is where you push your data to the temporary index
val batchTasks = Await.result(
Future.sequence(objects.grouped(1000).map(batch => client.execute(index into tmpIndexName objects batch))),
awaitDuration
)
Await.result(
Future.sequence(batchTasks.map(task => client.execute(waitFor task task from tmpIndexName))),
awaitDuration
)
// 3. Move the temporary index to the target index
client.execute(move index tmpIndexName to indexName)
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| // With JsonObject
val json = listOf(
json {
"firstname" to "Jimmie"
"lastname" to "Barninger"
},
json {
"firstname" to "Warren"
"lastname" to "Speach"
}
)
index.replaceAllObjects(json)
// With serializable class
@Serializable
data class Contact(val firstname: String, val lastname: String)
val contacts = listOf(
Contact("Jimmie", "Barninger"),
Contact("Warren", "Speach")
)
index.replaceAllObjects(Contact.serializer(), contacts)
|
Replace all objects and wait for operations
1
2
3
4
5
6
7
8
9
10
11
| $client = Algolia\AlgoliaSearch\SearchClient::create(
'YourApplicationID',
'YourWriteAPIKey'
);
$objects = /* Fetch your objects */;
$index = $client->initIndex('your_index_name');
$index->replaceAllObjects($objects, [
'safe' => true,
]);
|
1
2
3
4
5
6
7
8
9
| client = Algolia::Client.new({
:application_id => 'YourApplicationID',
:api_key => 'YourWriteAPIKey'
})
objects = [] # Fetch your objects
index = client.init_index('your_index_name')
index.replace_all_objects(objects, {'safe' => true})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| const client = algoliasearch('YourApplicationID', 'YourWriteAPIKey');
// 1. Initialize the target and temporary indices
const index = client.initIndex('atomic_reindex');
const tmpIndex = client.initIndex('atomic_reindex_tmp');
// 2. Copy the settings, synonyms and rules (but not the records)
client
.copyIndex(index.indexName, tmpIndex.indexName, [
'settings',
'synonyms',
'rules'
])
.then(({ taskID }) =>
// 3. Wait for the task to finish
tmpIndex.waitTask(taskID)
)
.then(() => {
// 4. Fetch your data and push it to the temporary index
const objects = [/* Get your data */];
return tmpIndex.addObjects(objects); // for better performance, send your objects in batches
})
.then(({ taskID }) =>
// 5. Wait for the task to finish
tmpIndex.waitTask(taskID)
)
.then(() =>
// 6. Move the temporary index to the target index
client.moveIndex(tmpIndex.indexName, index.indexName)
)
.catch(err => {
console.error(err);
});
|
1
2
3
4
5
6
7
8
| client = SearchClient.create('YourApplicationID', 'YourWriteAPIKey')
objects = [] # Fetch your objects
index = client.init_index('your_index_name')
index.replace_all_objects(objects, {
'safe': True
})
|
1
2
3
4
5
| var client = new SearchClient("YourApplicationID", "YourWriteAPIKey");
var index = client.InitIndex("your_index_name");
var objects = /* Fetch your objects */;
index.ReplaceAllObjects(objects, true);
|
1
2
3
4
5
6
7
8
9
| SearchClient client =
DefaultSearchClient.create("YourApplicationID", "YourWriteAPIKey");
SearchIndex<Contact> index = client.initIndex("your_index_name", Contact.class);
// Fetch your objects
List<Contact> objects = new ArrayList<>();
index.replaceAllObjects(objects, true);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package main
import (
"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
)
func main() {
client := search.NewClient("YourApplicationID", "YourWriteAPIKey")
index := client.InitIndex("your_index_name")
objects := []Object{ /* Fetch your objects */ }
res, err := index.ReplaceAllObjects(objects, opt.Safe(true))
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| import algolia.AlgoliaClient
import algolia.AlgoliaDsl._
import algolia.responses._
import scala.concurrent.duration._
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future}
case class MyCaseClass(objectID: String, /* ... */) extends ObjectID
object Main {
def main(args: Array[String]): Unit = {
implicit val ec: ExecutionContextExecutor = ExecutionContext.global
implicit val awaitDuration: FiniteDuration = 10 seconds
val client = new AlgoliaClient("YourApplicationID", "YourWriteAPIKey")
val tmpIndexName = "atomic_reindex_tmp"
val indexName = "atomic_reindex"
// 1. Copy the settings, synonyms and rules (but not the records)
// of the target index into the temporary index
val copyTask = Await.result(
client.execute(copy index indexName to tmpIndexName scope Seq("settings", "synonyms", "rules")),
awaitDuration
)
client.execute(waitFor task copyTask from indexName)
// 2. Fetch your data and push it to the temporary index
val objects: Seq[MyCaseClass] = Seq(/* ... */)
// Here is where you push your data to the temporary index
val batchTasks = Await.result(
Future.sequence(objects.grouped(1000).map(batch => client.execute(index into tmpIndexName objects batch))),
awaitDuration
)
Await.result(
Future.sequence(batchTasks.map(task => client.execute(waitFor task task from tmpIndexName))),
awaitDuration
)
// 3. Move the temporary index to the target index
client.execute(move index tmpIndexName to indexName)
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| client.apply {
val json = listOf(
json {
"firstname" to "Jimmie"
"lastname" to "Barninger"
},
json {
"firstname" to "Warren"
"lastname" to "Speach"
}
)
index.replaceAllObjects(json).waitAll()
}
|
Parameters
objects
|
A schemaless set of key/value pairs representing index attributes.
|
safe
|
type: boolean
default: false
Optional
Whether to wait for indexing operations.
|