Skip to content

Commit 38c0ddc

Browse files
authored
fix(java): use batch and class in playground and doc (#696)
1 parent c72d586 commit 38c0ddc

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/ClientOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public ClientOptions addAlgoliaAgentSegment(String value, String version) {
4646
return this;
4747
}
4848

49+
public ClientOptions addAlgoliaAgentSegment(String value) {
50+
this.algoliaAgentSegments.add(new AlgoliaAgent.Segment(value));
51+
return this;
52+
}
53+
4954
public List<StatefulHost> getHosts() {
5055
return this.hosts;
5156
}

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,31 @@
33
import com.algolia.api.SearchClient;
44
import com.algolia.exceptions.*;
55
import com.algolia.model.search.*;
6-
import com.algolia.utils.AlgoliaAgent;
76
import com.algolia.utils.ClientOptions;
87
import io.github.cdimascio.dotenv.Dotenv;
98
import java.util.*;
109
import java.util.concurrent.CompletableFuture;
1110
import java.util.concurrent.ExecutionException;
1211

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

1523
public static void main(String[] args) {
1624
Dotenv dotenv = Dotenv.configure().directory("../").load();
1725

1826
SearchClient client = new SearchClient(
1927
dotenv.get("ALGOLIA_APPLICATION_ID"),
20-
dotenv.get("ALGOLIA_SEARCH_KEY"),
21-
ClientOptions.build()
28+
dotenv.get("ALGOLIA_ADMIN_KEY"),
29+
ClientOptions
30+
.build()
2231
.addAlgoliaAgentSegment("test", "8.0.0")
2332
.addAlgoliaAgentSegment("JVM", "11.0.14")
2433
.addAlgoliaAgentSegment("no version")
@@ -28,15 +37,18 @@ public static void main(String[] args) {
2837
String query = dotenv.get("SEARCH_QUERY");
2938

3039
try {
31-
List<Map<String, Object>> records = Arrays.asList(Collections.singletonMap("name", "Tom Cruise"), Collections.singletonMap("name", "Scarlett Johansson"));
40+
List<Actor> records = Arrays.asList(new Actor("Tom Cruise"), new Actor("Scarlett Johansson"));
41+
42+
List<BatchOperation> batch = new ArrayList<>();
3243

33-
for (Map<String, Object> record : records) {
34-
client.saveObject(
35-
indexName,
36-
record
37-
);
44+
for (Actor record : records) {
45+
batch.add(new BatchOperation().setAction(Action.ADD_OBJECT).setBody(record));
3846
}
3947

48+
BatchResponse response = client.batch(indexName, new BatchWriteParams().setRequests(batch));
49+
50+
client.waitForTask(indexName, response.getTaskID());
51+
4052
SearchMethodParams searchMethodParams = new SearchMethodParams();
4153
List<SearchQuery> requests = new ArrayList<>();
4254
SearchForHits request = new SearchForHits();

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,35 @@ $client->waitForTask(<YOUR_INDEX_NAME>, $response['taskID']);
122122
<TabItem value="java">
123123

124124
```java
125+
class Actor {
126+
String name;
127+
128+
Actor(String name) {
129+
this.name = name;
130+
}
131+
}
132+
125133
// 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);
134+
List<Actor> records = Arrays.asList(
135+
new Actor("Tom Cruise"),
136+
new Actor("Scarlett Johansson")
137+
);
131138

132139
// Here we construct the request to be sent to Algolia with the `batch` method
133-
BatchWriteParams batchParams = new BatchWriteParams();
134140
List<BatchOperation> requests = new ArrayList<>();
135141

136-
for (Map<String, Object> record : records) {
142+
for (Actor record : records) {
137143
requests.add(new BatchOperation()
138144
.setAction(Action.ADD_OBJECT)
139-
// Accepts a free form `Map<String, Object>`.
145+
// Accepts any serializable class.
140146
.setBody(record)
141147
);
142148
}
143149

144-
batchParams.setRequests(requests);
145-
146-
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", batchParams);
150+
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));
147151

148152
// Wait for indexing to be finished
149-
await client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
153+
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
150154
```
151155

152156
</TabItem>

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)