Skip to content

Commit

Permalink
fix(java): use batch and class in playground and doc (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jun 15, 2022
1 parent c72d586 commit 38c0ddc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public ClientOptions addAlgoliaAgentSegment(String value, String version) {
return this;
}

public ClientOptions addAlgoliaAgentSegment(String value) {
this.algoliaAgentSegments.add(new AlgoliaAgent.Segment(value));
return this;
}

public List<StatefulHost> getHosts() {
return this.hosts;
}
Expand Down
30 changes: 21 additions & 9 deletions playground/java/src/main/java/com/algolia/playground/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
import com.algolia.api.SearchClient;
import com.algolia.exceptions.*;
import com.algolia.model.search.*;
import com.algolia.utils.AlgoliaAgent;
import com.algolia.utils.ClientOptions;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

class Actor {

String name;

Actor(String name) {
this.name = name;
}
}

public class Search {

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

SearchClient client = new SearchClient(
dotenv.get("ALGOLIA_APPLICATION_ID"),
dotenv.get("ALGOLIA_SEARCH_KEY"),
ClientOptions.build()
dotenv.get("ALGOLIA_ADMIN_KEY"),
ClientOptions
.build()
.addAlgoliaAgentSegment("test", "8.0.0")
.addAlgoliaAgentSegment("JVM", "11.0.14")
.addAlgoliaAgentSegment("no version")
Expand All @@ -28,15 +37,18 @@ public static void main(String[] args) {
String query = dotenv.get("SEARCH_QUERY");

try {
List<Map<String, Object>> records = Arrays.asList(Collections.singletonMap("name", "Tom Cruise"), Collections.singletonMap("name", "Scarlett Johansson"));
List<Actor> records = Arrays.asList(new Actor("Tom Cruise"), new Actor("Scarlett Johansson"));

List<BatchOperation> batch = new ArrayList<>();

for (Map<String, Object> record : records) {
client.saveObject(
indexName,
record
);
for (Actor record : records) {
batch.add(new BatchOperation().setAction(Action.ADD_OBJECT).setBody(record));
}

BatchResponse response = client.batch(indexName, new BatchWriteParams().setRequests(batch));

client.waitForTask(indexName, response.getTaskID());

SearchMethodParams searchMethodParams = new SearchMethodParams();
List<SearchQuery> requests = new ArrayList<>();
SearchForHits request = new SearchForHits();
Expand Down
28 changes: 16 additions & 12 deletions website/docs/clients/guides/send-data-to-algolia.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,35 @@ $client->waitForTask(<YOUR_INDEX_NAME>, $response['taskID']);
<TabItem value="java">

```java
class Actor {
String name;

Actor(String name) {
this.name = name;
}
}

// The records retrieved by any of your data sources
Map<String, Object> record1 = new HashMap<>();
record1.put("name", "Tom Cruise");
Map<String, Object> record2 = new HashMap<>();
record2.put("name", "Scarlett Johansson");
List<Map<String, Object>> records = Arrays.asList(record1, record2);
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
BatchWriteParams batchParams = new BatchWriteParams();
List<BatchOperation> requests = new ArrayList<>();

for (Map<String, Object> record : records) {
for (Actor record : records) {
requests.add(new BatchOperation()
.setAction(Action.ADD_OBJECT)
// Accepts a free form `Map<String, Object>`.
// Accepts any serializable class.
.setBody(record)
);
}

batchParams.setRequests(requests);

BatchResponse response = client.batch("<YOUR_INDEX_NAME>", batchParams);
BatchResponse response = client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));

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

</TabItem>
Expand Down
25 changes: 15 additions & 10 deletions website/docs/clients/migration-guides/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,33 @@ $client->batch(
<TabItem value="java">

```java
class Actor {
String name;

Actor(String name) {
this.name = name;
}
}

// The records retrieved by any of your data sources
Map<String, Object> record1 = new HashMap<>();
record1.put("name", "Tom Cruise");
Map<String, Object> record2 = new HashMap<>();
record2.put("name", "Scarlett Johansson");
List<Map<String, Object>> records = Arrays.asList(record1, record2);
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
BatchWriteParams batchParams = new BatchWriteParams();
List<BatchOperation> requests = new ArrayList<>();

for (Map<String, Object> record : records) {
for (Actor record : records) {
requests.add(new BatchOperation()
.setAction(Action.ADD_OBJECT)
// Accepts a free form `Map<String, Object>`.
// Accepts any serializable class.
.setBody(record)
);
}

batchParams.setRequests(requests);
client.batch("<YOUR_INDEX_NAME>", new BatchWriteParams().setRequests(batch));

client.batch("<YOUR_INDEX_NAME>", batchParams);
```

</TabItem>
Expand Down

0 comments on commit 38c0ddc

Please sign in to comment.