From 38c0ddc0baad7d8efae9cb2f202232a5f06a105c Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 15 Jun 2022 18:03:41 +0200 Subject: [PATCH] fix(java): use batch and class in playground and doc (#696) --- .../java/com/algolia/utils/ClientOptions.java | 5 ++++ .../java/com/algolia/playground/Search.java | 30 +++++++++++++------ .../clients/guides/send-data-to-algolia.mdx | 28 +++++++++-------- .../docs/clients/migration-guides/index.mdx | 25 +++++++++------- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/ClientOptions.java b/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/ClientOptions.java index 9d8f7ad000..515a6b8ded 100644 --- a/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/ClientOptions.java +++ b/clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/ClientOptions.java @@ -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 getHosts() { return this.hosts; } diff --git a/playground/java/src/main/java/com/algolia/playground/Search.java b/playground/java/src/main/java/com/algolia/playground/Search.java index 3367991ccd..d90aee6810 100644 --- a/playground/java/src/main/java/com/algolia/playground/Search.java +++ b/playground/java/src/main/java/com/algolia/playground/Search.java @@ -3,13 +3,21 @@ 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) { @@ -17,8 +25,9 @@ public static void main(String[] args) { 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") @@ -28,15 +37,18 @@ public static void main(String[] args) { String query = dotenv.get("SEARCH_QUERY"); try { - List> records = Arrays.asList(Collections.singletonMap("name", "Tom Cruise"), Collections.singletonMap("name", "Scarlett Johansson")); + List records = Arrays.asList(new Actor("Tom Cruise"), new Actor("Scarlett Johansson")); + + List batch = new ArrayList<>(); - for (Map 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 requests = new ArrayList<>(); SearchForHits request = new SearchForHits(); diff --git a/website/docs/clients/guides/send-data-to-algolia.mdx b/website/docs/clients/guides/send-data-to-algolia.mdx index 1dead6ecd6..23373bd7d0 100644 --- a/website/docs/clients/guides/send-data-to-algolia.mdx +++ b/website/docs/clients/guides/send-data-to-algolia.mdx @@ -122,31 +122,35 @@ $client->waitForTask(, $response['taskID']); ```java +class Actor { + String name; + + Actor(String name) { + this.name = name; + } +} + // The records retrieved by any of your data sources -Map record1 = new HashMap<>(); -record1.put("name", "Tom Cruise"); -Map record2 = new HashMap<>(); -record2.put("name", "Scarlett Johansson"); -List> records = Arrays.asList(record1, record2); +List 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 requests = new ArrayList<>(); -for (Map record : records) { +for (Actor record : records) { requests.add(new BatchOperation() .setAction(Action.ADD_OBJECT) - // Accepts a free form `Map`. + // Accepts any serializable class. .setBody(record) ); } -batchParams.setRequests(requests); - -BatchResponse response = client.batch("", batchParams); +BatchResponse response = client.batch("", new BatchWriteParams().setRequests(batch)); // Wait for indexing to be finished -await client.waitForTask("", response.getTaskID()); +client.waitForTask("", response.getTaskID()); ``` diff --git a/website/docs/clients/migration-guides/index.mdx b/website/docs/clients/migration-guides/index.mdx index 6b1f67d1b0..18cfb46d04 100644 --- a/website/docs/clients/migration-guides/index.mdx +++ b/website/docs/clients/migration-guides/index.mdx @@ -281,28 +281,33 @@ $client->batch( ```java +class Actor { + String name; + + Actor(String name) { + this.name = name; + } +} + // The records retrieved by any of your data sources -Map record1 = new HashMap<>(); -record1.put("name", "Tom Cruise"); -Map record2 = new HashMap<>(); -record2.put("name", "Scarlett Johansson"); -List> records = Arrays.asList(record1, record2); +List 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 requests = new ArrayList<>(); -for (Map record : records) { +for (Actor record : records) { requests.add(new BatchOperation() .setAction(Action.ADD_OBJECT) - // Accepts a free form `Map`. + // Accepts any serializable class. .setBody(record) ); } -batchParams.setRequests(requests); +client.batch("", new BatchWriteParams().setRequests(batch)); -client.batch("", batchParams); ```