Send data to Algolia
Algolia doesn’t search directly into your own data source. For data to be searchable, you need to send it to Algolia’s servers.
This happens right after retrieving your data from your data source and reformatting it. Once your data is ready, you can push it to Algolia using the batch
method.
Sending your data
caution
If the data you are sending is more than 1000 records at once, we recommend you to use our chunked batch solution instead.
Before sending anything to Algolia, you need to retrieve your data. You can do this in several ways, in our case we will pick it from the source code directly.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Ruby
- Scala
- Swift
using Algolia.Search.Clients;
using Algolia.Search.Http;
var client = new SearchClient(new SearchConfig("YOUR_APP_ID", "YOUR_API_KEY"));
var response = await client.BatchAsync(
"<YOUR_INDEX_NAME>",
new BatchWriteParams
{
Requests = new List<BatchRequest>
{
new BatchRequest
{
Action = Enum.Parse<Action>("AddObject"),
Body = new Dictionary<string, string> { { "key", "bar" }, { "foo", "1" } },
},
new BatchRequest
{
Action = Enum.Parse<Action>("AddObject"),
Body = new Dictionary<string, string> { { "key", "baz" }, { "foo", "2" } },
}
},
}
);
await client.WaitForTaskAsync("<YOUR_INDEX_NAME>", response.TaskID);
import 'package:algolia_client_search/algolia_client_search.dart';
final client = SearchClient(appId: 'YOUR_APP_ID', apiKey: 'YOUR_API_KEY');
final response = await client.batch(
indexName: "<YOUR_INDEX_NAME>",
batchWriteParams: BatchWriteParams(
requests: [
BatchRequest(
action: Action.fromJson("addObject"),
body: {
'key': "bar",
'foo': "1",
},
),
BatchRequest(
action: Action.fromJson("addObject"),
body: {
'key': "baz",
'foo': "2",
},
),
],
),
);
await client.waitTask('<YOUR_INDEX_NAME>', response.taskID);
import "github.com/algolia/algoliasearch-client-go/v4/algolia/search"
client, err := search.NewClient("YOUR_APP_ID", "YOUR_API_KEY")
response, err := client.Batch(client.NewApiBatchRequest(
"<YOUR_INDEX_NAME>",
search.NewEmptyBatchWriteParams().SetRequests(
[]search.BatchRequest{*search.NewEmptyBatchRequest().SetAction(search.Action("addObject")).SetBody(map[string]any{"key": "bar", "foo": "1"}), *search.NewEmptyBatchRequest().SetAction(search.Action("addObject")).SetBody(map[string]any{"key": "baz", "foo": "2"})}),
))
if err != nil {
// handle the eventual error
panic(err)
}
// use the model directly
print(response)
taskResponse, err := searchClient.WaitForTask("<YOUR_INDEX_NAME>", response.TaskID, nil, nil, nil)
if err != nil {
panic(err)
}
import com.algolia.api.SearchClient;
import com.algolia.model.search.*;
SearchClient client = new SearchClient("YOUR_APP_ID", "YOUR_API_KEY");
client.batch(
"<YOUR_INDEX_NAME>",
new BatchWriteParams()
.setRequests(
List.of(
new BatchRequest().setAction(Action.ADD_OBJECT).setBody(Map.of("key", "bar", "foo", "1")),
new BatchRequest().setAction(Action.ADD_OBJECT).setBody(Map.of("key", "baz", "foo", "2"))
)
)
);
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
import { searchClient } from '@algolia/client-search';
const client = searchClient('YOUR_APP_ID', 'YOUR_API_KEY');
const response = await client.batch({
indexName: '<YOUR_INDEX_NAME>',
batchWriteParams: {
requests: [
{ action: 'addObject', body: { key: 'bar', foo: '1' } },
{ action: 'addObject', body: { key: 'baz', foo: '2' } },
],
},
});
// use typed response
console.log(response);
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID: response.taskID });
import com.algolia.client.api.SearchClient
val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY")
var response = client.batch(
indexName = "<YOUR_INDEX_NAME>",
batchWriteParams = BatchWriteParams(
requests = listOf(
BatchRequest(
action = Action.entries.first { it.value == "addObject" },
body = buildJsonObject {
put(
"key",
JsonPrimitive("bar"),
)
put(
"foo",
JsonPrimitive("1"),
)
},
),
BatchRequest(
action = Action.entries.first { it.value == "addObject" },
body = buildJsonObject {
put(
"key",
JsonPrimitive("baz"),
)
put(
"foo",
JsonPrimitive("2"),
)
},
),
),
),
)
// Use the response
println(response)
client.waitTask("<YOUR_INDEX_NAME>", response.taskID)
use Algolia\AlgoliaSearch\Api\SearchClient;
$client = SearchClient::create('<YOUR_APP_ID>', '<YOUR_API_KEY>');
$response = $client->batch(
'<YOUR_INDEX_NAME>',
['requests' => [
['action' => 'addObject',
'body' => ['key' => 'bar',
'foo' => '1',
],
],
['action' => 'addObject',
'body' => ['key' => 'baz',
'foo' => '2',
],
],
],
],
);
// play with the response
var_dump($response);
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);
from algoliasearch.search.client import SearchClient
_client = SearchClient("YOUR_APP_ID", "YOUR_API_KEY")
response = await _client.batch(
index_name="<YOUR_INDEX_NAME>",
batch_write_params={
"requests": [
{
"action": "addObject",
"body": {
"key": "bar",
"foo": "1",
},
},
{
"action": "addObject",
"body": {
"key": "baz",
"foo": "2",
},
},
],
},
)
# use the class directly
print(response)
# print the JSON response
print(response.to_json())
await client.wait_for_task(index_name="<YOUR_INDEX_NAME>", task_id=response.task_id)
require 'algolia'
client = Algolia::SearchClient.create('YOUR_APP_ID', 'YOUR_API_KEY')
response = client.batch(
"<YOUR_INDEX_NAME>",
BatchWriteParams.new(
requests: [
BatchRequest.new(
action: 'addObject',
body: { key: "bar",
foo: "1" }
),
BatchRequest.new(action: 'addObject', body: { key: "baz", foo: "2" })
]
)
)
# use the class directly
puts response
# print the JSON response
puts response.to_json
client.wait_for_task("<YOUR_INDEX_NAME>", response.task_id)
import algoliasearch.api.SearchClient
val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY")
val response = client.batch(
indexName = "<YOUR_INDEX_NAME>",
batchWriteParams = BatchWriteParams(
requests = Seq(
BatchRequest(
action = Action.withName("addObject"),
body = JObject(List(JField("key", JString("bar")), JField("foo", JString("1"))))
),
BatchRequest(
action = Action.withName("addObject"),
body = JObject(List(JField("key", JString("baz")), JField("foo", JString("2"))))
)
)
)
)
// Use the response
val value = Await.result(response, Duration(100, "sec"))
client.waitTask("<YOUR_INDEX_NAME>", response.getTaskID())
import Search
let client = try SearchClient(appID: "YOUR_APP_ID", apiKey: "YOUR_API_KEY")
let response = try await client.batch(
indexName: "<YOUR_INDEX_NAME>",
batchWriteParams: BatchWriteParams(requests: [
BatchRequest(action: Action.addObject, body: ["key": "bar", "foo": "1"]),
BatchRequest(action: Action.addObject, body: ["key": "baz", "foo": "2"]),
])
)
try await client.waitForTask(with: response.taskID, in: "<YOUR_INDEX_NAME>")