Skip to content

Commit 162334e

Browse files
committed
use class
1 parent ccb02a8 commit 162334e

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

playground/java/src/main/java/com/algolia/playground/Search.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
import java.util.concurrent.CompletableFuture;
1010
import java.util.concurrent.ExecutionException;
1111

12+
class Actor {
13+
String name;
14+
15+
Actor(String name) {
16+
this.name = name;
17+
}
18+
}
19+
1220
public class Search {
1321

1422
public static void main(String[] args) {
@@ -28,15 +36,24 @@ public static void main(String[] args) {
2836
String query = dotenv.get("SEARCH_QUERY");
2937

3038
try {
31-
Map<String, Object> obj1 = new HashMap<>();
32-
obj1.put("name", "Tom Cruise");
33-
Map<String, Object> obj2 = new HashMap<>();
34-
obj2.put("name", "Scarlett Johansson");
35-
List<Map<String, Object>> records = Arrays.asList(obj1, obj2);
36-
37-
for (Map<String, Object> record : records) {
38-
client.saveObject(indexName, record);
39-
}
39+
List<Actor> records = Arrays.asList(
40+
new Actor("Tom Cruise"),
41+
new Actor("Scarlett Johansson")
42+
);
43+
44+
45+
List<BatchOperation> batch = new ArrayList<>();
46+
47+
for (Actor record : records) {
48+
batch.add(new BatchOperation()
49+
.setAction(Action.ADD_OBJECT)
50+
.setBody(record)
51+
);
52+
}
53+
54+
BatchResponse response = client.batch(indexName, new BatchWriteParams().setRequests(batch));
55+
56+
client.waitForTask(indexName, response.getTaskID());
4057

4158
SearchMethodParams searchMethodParams = new SearchMethodParams();
4259
List<SearchQuery> requests = new ArrayList<>();

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,23 @@ class Actor {
131131
}
132132

133133
// The records retrieved by any of your data sources
134-
List<Map<String, Object>> records = Arrays.asList(
134+
List<Actor> records = Arrays.asList(
135135
new Actor("Tom Cruise"),
136136
new Actor("Scarlett Johansson")
137137
);
138138

139139
// Here we construct the request to be sent to Algolia with the `batch` method
140-
BatchWriteParams batchParams = new BatchWriteParams();
141140
List<BatchOperation> requests = new ArrayList<>();
142141

143-
for (Map<String, Object> record : records) {
142+
for (Actor record : records) {
144143
requests.add(new BatchOperation()
145144
.setAction(Action.ADD_OBJECT)
146145
// Accepts any serializable class.
147146
.setBody(record)
148147
);
149148
}
150149

151-
batchParams.setRequests(requests);
152-
153-
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", batchParams);
150+
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));
154151

155152
// Wait for indexing to be finished
156153
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());

website/docs/clients/migration-guides/index.mdx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,28 +281,33 @@ $client->batch(
281281
<TabItem value="java">
282282

283283
```java
284+
class Actor {
285+
String name;
286+
287+
Actor(String name) {
288+
this.name = name;
289+
}
290+
}
291+
284292
// The records retrieved by any of your data sources
285-
Map<String, Object> record1 = new HashMap<>();
286-
record1.put("name", "Tom Cruise");
287-
Map<String, Object> record2 = new HashMap<>();
288-
record2.put("name", "Scarlett Johansson");
289-
List<Map<String, Object>> records = Arrays.asList(record1, record2);
293+
List<Actor> records = Arrays.asList(
294+
new Actor("Tom Cruise"),
295+
new Actor("Scarlett Johansson")
296+
);
290297

291298
// Here we construct the request to be sent to Algolia with the `batch` method
292-
BatchWriteParams batchParams = new BatchWriteParams();
293299
List<BatchOperation> requests = new ArrayList<>();
294300

295-
for (Map<String, Object> record : records) {
301+
for (Actor record : records) {
296302
requests.add(new BatchOperation()
297303
.setAction(Action.ADD_OBJECT)
298-
// Accepts a free form `Map<String, Object>`.
304+
// Accepts any serializable class.
299305
.setBody(record)
300306
);
301307
}
302308

303-
batchParams.setRequests(requests);
309+
client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));
304310

305-
client.batch("<YOUR_INDEX_NAME>", batchParams);
306311
```
307312

308313
</TabItem>

0 commit comments

Comments
 (0)