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.
Required credentials
To push data to Algolia, you need an Application ID and a valid API key with the right access level. You can find them and create new ones in the API keys page.
Setting up the API client
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Scala
// for the default version
import { algoliasearch } from 'algoliasearch';
// you can also import the lite version, with search only methods
// import { liteClient } from 'algoliasearch/lite';
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');
<?php
$client = Algolia\AlgoliaSearch\Api\SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>'
);
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");
val client = SearchClient(appId = "<YOUR_APP_ID>", apiKey = "<YOUR_API_KEY>")
var client = SearchClient(appId: '<YOUR_APP_ID>', apiKey: '<YOUR_API_KEY>');
import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
searchClient, _ := search.NewClient("<APPLICATION_ID>", "<API_KEY>")
Sending your data
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
- Scala
// The records retrieved by any of your data sources
const recordsFromDataSource = [
{ name: 'Tom Cruise' },
{ name: 'Scarlett Johansson' },
];
// Here we construct the request to be sent to Algolia with the `batch` method
const requests: BatchRequest[] = recordsFromDataSource.map((record) => {
return {
// `batch` allows you to do many Algolia operations, but here we want to index our record.
action: 'addObject',
body: record,
};
});
const { taskID } = await client.batch({
indexName: '<YOUR_INDEX_NAME>',
batchWriteParams: {
requests,
},
});
// Wait for indexing to be finished
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });
console.log('Ready to search!');
// The records retrieved by any of your data sources
$records = [
['name' => 'Tom Cruise'],
['name' => 'Scarlett Johansson']
];
// Here we construct the request to be sent to Algolia with the `batch` method
$requests = [];
foreach ($records as $record) {
$requests[] = [
'action' => 'addObject',
'body' => $record
];
}
$response = $client->batch(
'<YOUR_INDEX_NAME>',
[ 'requests' => $requests ]
);
// Wait for indexing to be finished
$client->waitForTask(<YOUR_INDEX_NAME>, $response['taskID']);
import com.algolia.model.search.*;
class Actor extends Hit {
public String name;
public Actor(String name) {
this.name = name;
}
}
// The records retrieved by any of your data sources
List<Actor> records = Arrays.asList(
new Actor("Tom Cruise"),
new Actor("Scarlett Johansson")
);
// Here we construct the request to be sent to Algolia with the `batch` method
List<BatchRequest> requests = new ArrayList<>();
for (Actor record : records) {
requests.add(new BatchRequest()
.setAction(Action.ADD_OBJECT)
// Accepts any serializable class.
.setBody(record)
);
}
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));
// Wait for indexing to be finished
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
import com.algolia.client.api.SearchClient
import com.algolia.client.extensions.waitTask
import com.algolia.client.model.search.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
@Serializable
data class Actor(val name: String)
// The records retrieved by any of your data sources
val records = listOf(Actor("Tom Cruise"), Actor("Scarlett Johansson"))
// Here we construct the request to be sent to Algolia with the `batch` method
val response = client.batch(
indexName = "<YOUR_INDEX_NAME>",
batchWriteParams = BatchWriteParams(
requests = records.map { actor ->
BatchRequest(
action = Action.AddObject,
body = Json.encodeToJsonElement(Actor.serializer(), actor).jsonObject,
)
},
)
)
// Wait for indexing to be finished
client.waitTask("<YOUR_INDEX_NAME>", response.taskID)
class Actor {
final String name;
Actor(this.name);
Map<String, Object> toJson() => { "name": name };
}
// The records retrieved by any of your data sources
var records = [Actor('Tom Cruise'), Actor('Scarlett Johansson')];
// Here we construct the request to be sent to Algolia with the `batch` method
var response = await client.batch(
indexName : '<YOUR_INDEX_NAME>',
batchWriteParams : BatchWriteParams(
requests: records.map((record) => BatchRequest(action: Action.addObject, body: record)).toList(),
)
);
// Wait for indexing to be finished
await client.waitTask('<YOUR_INDEX_NAME>', response.taskID);
import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
indexName := "<INDEX_NAME>"
appID := "<APPLICATION_ID>"
apiKey := "<API_KEY>"
searchClient, _ := search.NewClient(appID, apiKey)
actors := []map[string]interface{}{
{"name": "Tom Cruise"},
{"name": "Scarlett Johansson"},
}
requests := make([]search.BatchRequest, 0, len(actors))
for _, actor := range actors {
requests = append(
requests,
*search.NewBatchRequest(search.ACTION_ADD_OBJECT, actor),
)
}
response, err := searchClient.Batch(
searchClient.NewApiBatchRequest(
indexName,
search.NewBatchWriteParams(requests),
),
)
if err != nil {
panic(err)
}
taskResponse, err := searchClient.WaitForTask(
indexName,
response.TaskID,
nil,
nil,
nil,
)
if err != nil {
panic(err)
}