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
- JavaScript
- PHP
- Java
js
// for the default versionimport { 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>');
js
// for the default versionimport { 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
<?php$client = Algolia\AlgoliaSearch\Api\SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');
php
<?php$client = Algolia\AlgoliaSearch\Api\SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');
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.
- JavaScript
- PHP
- Java
js
// The records retrieved by any of your data sourcesconst recordsFromDataSource = [{ name: 'Tom Cruise' },{ name: 'Scarlett Johansson' },];// Here we construct the request to be sent to Algolia with the `batch` methodconst 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 finishedawait client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });console.log('Ready to search!');
js
// The records retrieved by any of your data sourcesconst recordsFromDataSource = [{ name: 'Tom Cruise' },{ name: 'Scarlett Johansson' },];// Here we construct the request to be sent to Algolia with the `batch` methodconst 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 finishedawait client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });console.log('Ready to search!');
php
// 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']);
php
// 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']);
java
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 sourcesList<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` methodList<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 finishedclient.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
java
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 sourcesList<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` methodList<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 finishedclient.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());