### Quick start: save object and search Source: https://github.com/algolia/algoliasearch-client-java/blob/main/README.md Import the SearchClient, create a client with your app ID and API key, save a record, wait for indexing, and perform a search with typo tolerance. Replace placeholders like YOUR_APP_ID and YOUR_INDEX_NAME with your actual values. ```java import com.algolia.api.SearchClient; import com.algolia.model.search.*; SearchClient client = new SearchClient("YOUR_APP_ID", "YOUR_API_KEY"); // Add a new record to your Algolia index var response = client.saveObject("", Map.of("objectID", "test", "test", "val")); // Poll the task status to know when it has been indexed client.waitForTask("", response.getTaskID()); // Fetch search results, with typo tolerance client.search( new SearchMethodParams() .setRequests(List.of(new SearchForHits().setIndexName("").setQuery("").setHitsPerPage(50))), Hit.class ); ``` -------------------------------- ### Build and Test Commands Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Commands to build, test, and format the Java client. Run from repo root or client directory as indicated. ```bash # From repo root (api-clients-automation) yarn cli build clients java # Build Java client yarn cli cts generate java # Generate CTS tests yarn cli cts run java # Run CTS tests yarn cli playground java search # Interactive playground yarn cli format java clients/algoliasearch-client-java # From client directory cd clients/algoliasearch-client-java ./gradlew build # Build with Gradle ./gradlew test # Run tests ./gradlew spotlessApply # Apply formatting ``` -------------------------------- ### Initialize APIClient in v1 Source: https://github.com/algolia/algoliasearch-client-java/wiki/Migration-guide-from-1.x-to-2.x Old v1 initialization of the APIClient using the constructor directly. Replace with the v2 builder pattern. ```java APIClient client = new APIClient("YourApplicationID", "YourAPIKey"); ``` -------------------------------- ### Resource Cleanup with try-with-resources Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Alternative to try-finally: use try-with-resources to automatically close the client. Requires SearchClient to implement AutoCloseable. ```java // Or use try-with-resources try (SearchClient client = new SearchClient("APP_ID", "API_KEY")) { // use client } ``` -------------------------------- ### Initialize APIClient in v2 using ApacheAPIClientBuilder Source: https://github.com/algolia/algoliasearch-client-java/wiki/Migration-guide-from-1.x-to-2.x New v2 initialization using the builder pattern. Allows changing default values via the builder. ```java APIClient client = new ApacheAPIClientBuilder("YourApplicationID", "YourAPIKey").build(); ``` -------------------------------- ### Verify Git Remote Repository Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Run this command first to confirm you are in the correct repository. If the remote points to algoliasearch-client-java.git, stop and do not make changes. ```bash git remote -v ``` -------------------------------- ### Sync and async search calls Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Shows the synchronous client.search(params) and the asynchronous client.searchAsync(params) returning CompletableFuture. ```java // Sync version SearchResponse response = client.search(params); // Async version (returns CompletableFuture) CompletableFuture future = client.searchAsync(params); ``` -------------------------------- ### HttpRequester class skeleton with OkHttp and JsonSerializer Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Core HTTP implementation in internal/HttpRequester.java. Uses OkHttp interceptors for header injection, gzip compression, and logging. ```java // Core HTTP in internal/HttpRequester.java public final class HttpRequester implements Requester { private final OkHttpClient httpClient; private final JsonSerializer serializer; // Uses OkHttp interceptors for: // - Header injection (HeaderInterceptor) // - Gzip compression (GzipRequestInterceptor) // - Logging (LogInterceptor) } ``` -------------------------------- ### Resource Cleanup with try-finally Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Always close the SearchClient when done to release resources. Use try-finally to ensure close() is called even if an exception occurs. ```java // Always close the client when done SearchClient client = new SearchClient("APP_ID", "API_KEY"); try { // use client } finally { client.close(); } ``` -------------------------------- ### Check Generation Config Patterns Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Inspect generation.config.mjs to determine if a file is generated. Patterns without '!' are generated and must not be edited. ```javascript // In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) 'clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/api/**', // Generated 'clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/model/**', // Generated // Everything else is hand-written by default (note the '!' pattern for Java) ``` -------------------------------- ### Null Safety with Optional and Annotations Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Use Optional for nullable return values and @Nonnull/@Nullable annotations to document nullability. This helps avoid NullPointerExceptions. ```java // Use Optional for nullable returns Optional value = config.getOptionalParam(); // Check with @Nonnull/@Nullable annotations public void method(@Nonnull String required, @Nullable String optional) ``` -------------------------------- ### Maven dependency for algoliasearch Source: https://github.com/algolia/algoliasearch-client-java/blob/main/README.md Add the Algolia Java client to a Maven project. Place this inside the project's pom.xml dependencies section. ```xml com.algolia algoliasearch [4,) ``` -------------------------------- ### Gradle dependency for algoliasearch Source: https://github.com/algolia/algoliasearch-client-java/blob/main/README.md Add the Algolia Java client to a Gradle project. Place this in the dependencies block of your build.gradle file. ```groovy dependencies { implementation 'com.algolia:algoliasearch:4.+' } ``` -------------------------------- ### Exception hierarchy in com.algolia.exceptions Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Base AlgoliaException is a RuntimeException. Subclasses cover API errors (4xx/5xx), client-side errors, retries exhausted, and unexpected runtime errors. ```java // com.algolia.exceptions AlgoliaException // Base (RuntimeException) ├── AlgoliaApiException // API error (4xx, 5xx) ├── AlgoliaClientException // Client-side error ├── AlgoliaRetryException // All retries exhausted └── AlgoliaRuntimeException // Unexpected runtime error ``` -------------------------------- ### TypeReference for Generic Types Source: https://github.com/algolia/algoliasearch-client-java/blob/main/AGENTS.md Use TypeReference to deserialize generic types like List when calling search. This preserves generic type information at runtime. ```java // Use TypeReference for generic types List hits = client.search(params, new TypeReference>() {}); ```