addObject
This method lets you replace all records in your index without downtime.
It performs these operations:
- Copy settings, synonyms, and rules from your original index to a temporary index.
- Add your new records to the temporary index.
- Replace your original index with the temporary index.
-
Use the
safeparameter to ensure that these (asynchronous) operations are performed in sequence. - If there’s an error during one of these steps, the temporary index won’t be deleted.
- This operation is rate-limited.
- This method creates a temporary index: your record count is temporarily doubled. Algolia doesn’t count the three days with the highest number of records towards your monthly usage.
-
If you’re on a legacy plan (before July 2020), this method counts two operations towards your usage (in addition to the number of records):
copySettingsandmoveIndex. -
The API key you use for this operation must have access to the index
YourIndexand the temporary indexYourIndex_tmp.
Examples
Replace all records
var client = new SearchClient("YourApplicationID", "YourWriteAPIKey");
var index = client.InitIndex("YourIndexName");
var objects = /* Fetch your objects */;
index.ReplaceAllObjects(objects);
package main
import "github.com/algolia/algoliasearch-client-go/v3/algolia/search"
func main() {
client := search.NewClient("YourApplicationID", "YourWriteAPIKey")
index := client.InitIndex("YourIndexName")
objects := []Object{ /* Fetch your objects */ }
res, err := index.ReplaceAllObjects(objects)
}
SearchClient client =
DefaultSearchClient.create("YourApplicationID", "YourWriteAPIKey");
SearchIndex<Contact> index = client.initIndex("YourIndexName", Contact.class);
// Fetch your objects
List<Contact> objects = new ArrayList<>();
// Sync version
index.replaceAllObjects(objects);
// Async version
index.replaceAllObjectsAsync(objects);
const client = algoliasearch('YourApplicationID', 'YourWriteAPIKey');
const objects = []; // Fetch your objects
const index = client.initIndex('YourIndexName');
index.replaceAllObjects(objects).then(({ objectIDs }) => {
console.log(objectIDs);
});
// 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)
$client = Algolia\AlgoliaSearch\SearchClient::create(
'YourApplicationID',
'YourWriteAPIKey'
);
$objects = /* Fetch your objects */;
$index = $client->initIndex('YourIndexName');
$index->replaceAllObjects($objects);
client = SearchClient.create("YourApplicationID", "YourWriteAPIKey")
objects = [] # Fetch your objects
index = client.init_index("YourIndexName")
index.replace_all_objects(objects)
client = Algolia::Search::Client.create('YourApplicationID', 'YourWriteAPIKey')
objects = [
# your new objects
]
index = client.init_index('YourIndexName')
index.replace_all_objects(objects)
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)
}
}
struct Contact: Encodable {
let objectID: ObjectID
let firstname: String
let lastname: String
}
let client = SearchClient(appID: "YourApplicationID", apiKey: "YourWriteAPIKey")
let index = client.index(withName: "YourIndexName")
let objects: [Contact]! = [/*Fetch your objects*/]
index.replaceAllObjects(with: objects) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
Replace all records and wait for operations
var client = new SearchClient("YourApplicationID", "YourWriteAPIKey");
var index = client.InitIndex("YourIndexName");
var objects = /* Fetch your objects */;
index.ReplaceAllObjects(objects, true);
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("YourIndexName")
objects := []Object{ /* Fetch your objects */ }
res, err := index.ReplaceAllObjects(objects, opt.Safe(true))
}
SearchClient client =
DefaultSearchClient.create("YourApplicationID", "YourWriteAPIKey");
SearchIndex<Contact> index = client.initIndex("YourIndexName", Contact.class);
// Fetch your objects
List<Contact> objects = new ArrayList<>();
index.replaceAllObjects(objects, true);
const client = algoliasearch('YourApplicationID', 'YourWriteAPIKey');
const objects = []; // Fetch your objects
const index = client.initIndex('YourIndexName');
index.replaceAllObjects(objects, { safe: true }).then(({ objectIDs }) => {
console.log(objectIDs);
});
client.apply {
val json = listOf(
json {
"firstname" to "Jimmie"
"lastname" to "Barninger"
},
json {
"firstname" to "Warren"
"lastname" to "Speach"
}
)
index.replaceAllObjects(json).waitAll()
}
$client = Algolia\AlgoliaSearch\SearchClient::create(
'YourApplicationID',
'YourWriteAPIKey'
);
$objects = /* Fetch your objects */;
$index = $client->initIndex('YourIndexName');
$index->replaceAllObjects($objects, [
'safe' => true,
]);
client = SearchClient.create('YourApplicationID', 'YourWriteAPIKey')
objects = [] # Fetch your objects
index = client.init_index('YourIndexName')
index.replace_all_objects(objects, {
'safe': True
})
client = Algolia::Search::Client.create('YourApplicationID', 'YourWriteAPIKey')
objects = [
# your new objects
]
index = client.init_index('YourIndexName')
index.replace_all_objects(objects, { safe: true })
# or: index.replace_all_objects!(objects)
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)
}
}
struct Contact: Encodable {
let objectID: ObjectID
let firstname: String
let lastname: String
}
let client = SearchClient(appID: "YourApplicationID", apiKey: "YourWriteAPIKey")
let index = client.index(withName: "YourIndexName")
let objects: [Contact]! = [/*Fetch your objects*/]
index.replaceAllObjects(with: objects, safe: true) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
Parameters
list
required
List of records.Python: Use an iterator instead of a list to prevent memory issues,
especially if you want to replace many records.
object
A mapping of request options to send along with the query.
boolean
default:false
If true, wait after each step before continuing.