Skip to main content

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

Make sure to also read the installation page.

java
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");
java
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_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.

java
class Actor {
String name;
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<BatchOperation> requests = new ArrayList<>();
for (Actor record : records) {
requests.add(new BatchOperation()
.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());
java
class Actor {
String name;
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<BatchOperation> requests = new ArrayList<>();
for (Actor record : records) {
requests.add(new BatchOperation()
.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());