Install the Java API Client
With Maven, add the following dependency to your pom.xml file:
1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-apache</artifactId>
<version>3.0.0</version>
</dependency>
Then run:
$
mvn compile
Supported platforms
The API client only supports Java 1.8 and above.
Source on GitHub
All our API clients are open source and available on Github.
Language-specific notes
The JVM has an infinite cache on successful DNS resolution. As our hostnames points to multiple IPs, the load could be not evenly spread among our machines, and you might also target a dead machine.
You should change this TTL by setting the property networkaddress.cache.ttl. For example to set the cache to 60 seconds:
1
java.security.Security.setProperty("networkaddress.cache.ttl", "60");
For debug purposes you can enable debug logging on the API client. It’s using java.util.logging.
To enable it, add the following line in your logging.properties file.
1
com.algolia.search.HttpTransport=FINEST
Philosophy
Builder
The v3 of the API client comes in two parts.
algoliasearch-corewhich contains all the methods, the POJOs, the transport layer and the retry strategy. This jar is agnostic of any HTTP Client implementation.algoliasearch-apachewhich is the default HTTP client implementation for the core library.
You can instantiate a DefaultSearchClient with the two dependencies like this:
1
2
3
4
SearchClient client =
DefaultSearchClient.create("YourApplicationID", "YourAdminAPIKey");
SearchIndex<Contact> index = client.InitIndex("your_index_name", Contact.class);
If you want to inject your own HttpClient you can inject it by constructor into all the clients classes of library.
The latter has to implement the HttpRequester interface. In that case you don’t need algoliasearch-apache anymore as a dependency.
1
2
3
4
5
6
7
SearchConfig config =
new SearchConfig.Builder("YourApplicationID", "YourAdminAPIKey")
.build();
HttpRequester myCustomRequester = new myCustomRequester();
SearchClient client = DefaultSearchClient.create(config, myCustomRequester);
All clients are safe to use as a singleton. We recommend reusing client instances as much as possible to avoid socket exhaustion.
All clients implement the Closeable interface. You should close them when you’re done using them.
POJO, JSON & Jackson2
The SearchIndex class is parametrized with a Java class. If you specify one, it lets you have type safe method results.
This parametrized Java class should follow the POJO convention:
- A constructor without parameters
- Getters & setters for every field you want to (de)serialize
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Contact {
private String name;
private int age;
public Contact() {}
public String getName() {
return name;
}
public Contact setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Contact setAge(int age) {
this.age = age;
return this;
}
}
All the serialization/deserialization process is done with Jackson2. You can find the default ObjectMapper com.algolia.search.Defaults.DEFAULT_OBJECT_MAPPER.
Async & CompletableFuture
All asynchronous methods are suffixed with the Async keyword and are in the same classes as the synchronous one. All asynchronous methods return a CompletableFuture.
You can also pass a custom ExecutorService to the Configuration builder. If you don’t provide one the library will be using the ForkJoinPool.commonPool.
Multithreading
The client is designed to be thread-safe. You can use SearchClient, AnalyticsClient, and InsightsClient in a multithreaded environment.
Error handling
The library can throw three type of runtime exception for each methods:
AlgoliaApiExceptionWhen Algolia APIs send back an HTTP error codeAlgoliaRetryExceptionWhen the retry strategy failed targeting all hostsAlgoliaRuntimeExceptionWhen an error occurred during serialization/deserialization or data processingIllegalArgumentExceptionWhen unvalid parameters are providedNullPointerExceptionWhen a null pointer is passed and not expected