Skip to content

Commit da45204

Browse files
shortcutsmillotp
andauthored
docs: update send data guide (#697)
Co-authored-by: Pierre Millot <[email protected]>
1 parent 9e4c728 commit da45204

File tree

5 files changed

+351
-24
lines changed

5 files changed

+351
-24
lines changed

specs/search/paths/objects/batch.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ post:
4242
$ref: '../../../common/responses/common.yml#/taskID'
4343
objectIDs:
4444
$ref: '../../../common/responses/common.yml#/objectIDs'
45+
required:
46+
- taskID
47+
- objectIDs
4548
'400':
4649
$ref: '../../../common/responses/BadRequest.yml'
4750
'402':

tests/CTS/methods/requests/search/batch.json

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
[
22
{
3+
"testName": "allows batch method with `addObject` action",
4+
"parameters": {
5+
"indexName": "theIndexName",
6+
"batchWriteParams": {
7+
"requests": [
8+
{
9+
"action": "addObject",
10+
"body": {
11+
"key": "value"
12+
}
13+
}
14+
]
15+
}
16+
},
17+
"request": {
18+
"path": "/1/indexes/theIndexName/batch",
19+
"method": "POST",
20+
"body": {
21+
"requests": [
22+
{
23+
"action": "addObject",
24+
"body": {
25+
"key": "value"
26+
}
27+
}
28+
]
29+
}
30+
}
31+
},
32+
{
33+
"testName": "allows batch method with `clear` action",
34+
"parameters": {
35+
"indexName": "theIndexName",
36+
"batchWriteParams": {
37+
"requests": [
38+
{
39+
"action": "clear",
40+
"body": {
41+
"key": "value"
42+
}
43+
}
44+
]
45+
}
46+
},
47+
"request": {
48+
"path": "/1/indexes/theIndexName/batch",
49+
"method": "POST",
50+
"body": {
51+
"requests": [
52+
{
53+
"action": "clear",
54+
"body": {
55+
"key": "value"
56+
}
57+
}
58+
]
59+
}
60+
}
61+
},
62+
{
63+
"testName": "allows batch method with `delete` action",
364
"parameters": {
465
"indexName": "theIndexName",
566
"batchWriteParams": {
@@ -27,5 +88,125 @@
2788
]
2889
}
2990
}
91+
},
92+
{
93+
"testName": "allows batch method with `deleteObject` action",
94+
"parameters": {
95+
"indexName": "theIndexName",
96+
"batchWriteParams": {
97+
"requests": [
98+
{
99+
"action": "deleteObject",
100+
"body": {
101+
"key": "value"
102+
}
103+
}
104+
]
105+
}
106+
},
107+
"request": {
108+
"path": "/1/indexes/theIndexName/batch",
109+
"method": "POST",
110+
"body": {
111+
"requests": [
112+
{
113+
"action": "deleteObject",
114+
"body": {
115+
"key": "value"
116+
}
117+
}
118+
]
119+
}
120+
}
121+
},
122+
{
123+
"testName": "allows batch method with `partialUpdateObject` action",
124+
"parameters": {
125+
"indexName": "theIndexName",
126+
"batchWriteParams": {
127+
"requests": [
128+
{
129+
"action": "partialUpdateObject",
130+
"body": {
131+
"key": "value"
132+
}
133+
}
134+
]
135+
}
136+
},
137+
"request": {
138+
"path": "/1/indexes/theIndexName/batch",
139+
"method": "POST",
140+
"body": {
141+
"requests": [
142+
{
143+
"action": "partialUpdateObject",
144+
"body": {
145+
"key": "value"
146+
}
147+
}
148+
]
149+
}
150+
}
151+
},
152+
{
153+
"testName": "allows batch method with `partialUpdateObjectNoCreate` action",
154+
"parameters": {
155+
"indexName": "theIndexName",
156+
"batchWriteParams": {
157+
"requests": [
158+
{
159+
"action": "partialUpdateObjectNoCreate",
160+
"body": {
161+
"key": "value"
162+
}
163+
}
164+
]
165+
}
166+
},
167+
"request": {
168+
"path": "/1/indexes/theIndexName/batch",
169+
"method": "POST",
170+
"body": {
171+
"requests": [
172+
{
173+
"action": "partialUpdateObjectNoCreate",
174+
"body": {
175+
"key": "value"
176+
}
177+
}
178+
]
179+
}
180+
}
181+
},
182+
{
183+
"testName": "allows batch method with `updateObject` action",
184+
"parameters": {
185+
"indexName": "theIndexName",
186+
"batchWriteParams": {
187+
"requests": [
188+
{
189+
"action": "updateObject",
190+
"body": {
191+
"key": "value"
192+
}
193+
}
194+
]
195+
}
196+
},
197+
"request": {
198+
"path": "/1/indexes/theIndexName/batch",
199+
"method": "POST",
200+
"body": {
201+
"requests": [
202+
{
203+
"action": "updateObject",
204+
"body": {
205+
"key": "value"
206+
}
207+
}
208+
]
209+
}
210+
}
30211
}
31212
]

website/docs/clients/guides/send-data-to-algolia.mdx

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import TabItem from '@theme/TabItem';
77

88
Algolia doesn’t search directly into your own data source. For data to be searchable, you need to send it to Algolia’s servers.
99

10-
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 `saveObjects` method.
10+
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.
1111

1212
## Required credentials
1313

14-
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](https://www.algolia.com/account/api-keys/all?applicationId=QPBQ67WNIG).
14+
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](https://www.algolia.com/account/api-keys/all).
1515

1616
## Setting up the API client
1717

18-
> [Make sure to also read the Installation page](/docs/clients/installation).
18+
> [Make sure to also read the `installation` page](/docs/clients/installation).
1919
2020
<TabsLanguage>
2121
<TabItem value="javascript">
@@ -61,47 +61,92 @@ Before sending anything to Algolia, you need to retrieve your data. You can do t
6161
<TabItem value="javascript">
6262

6363
```js
64-
const records = [{ name: 'Tom Cruise' }, { name: 'Scarlett Johansson' }];
64+
// The records retrieved by any of your data sources
65+
const recordsFromDataSource = [
66+
{ name: 'Tom Cruise' },
67+
{ name: 'Scarlett Johansson' },
68+
];
6569

66-
for (const record of records) {
67-
client.saveObject({
68-
indexName: '<YOUR_INDEX_NAME>',
69-
// Accepts a free form `Record<string, any>` object.
70+
// Here we construct the request to be sent to Algolia with the `batch` method
71+
const requests: BatchOperation[] = recordsFromDataSource.map((record) => {
72+
return {
73+
// `batch` allows you to do many Algolia operations, but here we want to index our record.
74+
action: 'addObject',
7075
body: record,
71-
});
72-
}
76+
};
77+
});
78+
79+
const { taskID } = await client.batch({
80+
indexName: '<YOUR_INDEX_NAME>',
81+
batchWriteParams: {
82+
requests,
83+
},
84+
});
85+
86+
// Wait for indexing to be finished
87+
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });
88+
89+
console.log('Ready to search!');
7390
```
7491

7592
</TabItem>
7693
<TabItem value="php">
7794

7895
```php
96+
// The records retrieved by any of your data sources
7997
$records = [
80-
['name' => 'Tom Cruise'],
81-
['name' => 'Scarlett Johansson']
98+
['name' => 'Tom Cruise'],
99+
['name' => 'Scarlett Johansson']
82100
];
83101

102+
// Here we construct the request to be sent to Algolia with the `batch` method
103+
$requests = [];
104+
84105
foreach ($records as $record) {
85-
$client->saveObject(
86-
'<YOUR_INDEX_NAME>',
87-
$record
88-
);
106+
$requests += [
107+
'action' => 'addObject',
108+
'body' => $record
109+
];
89110
}
111+
112+
$response = $client->batch(
113+
'<YOUR_INDEX_NAME>',
114+
[ 'requests' => $requests ]
115+
);
116+
117+
// Wait for indexing to be finished
118+
$client->waitForTask(<YOUR_INDEX_NAME>, $response['taskID']);
90119
```
91120

92121
</TabItem>
93122
<TabItem value="java">
94123

95124
```java
96-
List<Map<String, Object>> records = Arrays.asList(Collections.singletonMap("name", "Tom Cruise"), Collections.singletonMap("name", "Scarlett Johansson"));
125+
// The records retrieved by any of your data sources
126+
Map<String, Object> record1 = new HashMap<>();
127+
record1.put("name", "Tom Cruise");
128+
Map<String, Object> record2 = new HashMap<>();
129+
record2.put("name", "Scarlett Johansson");
130+
List<Map<String, Object>> records = Arrays.asList(record1, record2);
131+
132+
// Here we construct the request to be sent to Algolia with the `batch` method
133+
BatchWriteParams batchParams = new BatchWriteParams();
134+
List<BatchOperation> requests = new ArrayList<>();
97135

98136
for (Map<String, Object> record : records) {
99-
client.saveObject(
100-
"<YOUR_INDEX_NAME>",
137+
requests.add(new BatchOperation()
138+
.setAction(Action.ADD_OBJECT)
101139
// Accepts a free form `Map<String, Object>`.
102-
record
140+
.setBody(record)
103141
);
104142
}
143+
144+
batchParams.setRequests(requests);
145+
146+
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", batchParams);
147+
148+
// Wait for indexing to be finished
149+
await client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
105150
```
106151

107152
</TabItem>

website/docs/clients/guides/wait-for-a-task-to-finish.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ SaveObjectResponse response = client.saveObject(
5757
await client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
5858

5959
// Poll the task status with your options
60-
client.waitForTask(
60+
await client.waitForTask(
6161
"<YOUR_INDEX_NAME>",
6262
response.getTaskID(),
6363
// Number of maximum retries to do

0 commit comments

Comments
 (0)