Customize your Java client
This is documentation for v3 of the Java API clients, which is not the latest version. To see the documentation for the latest version, see Java v4.
To change individual requests made with an API client, pass individual Request options. To change all requests, create a custom configuration. This lets you change timeouts, add HTTP headers, and so on.
Use a custom host
You can change the default hosts to which the API client connects:
1
2
3
4
5
6
7
8
SearchClient client =
DefaultSearchClient.create(
new SearchConfig.Builder("applicationID", "apiKey")
.setHosts(
Arrays.asList(
new StatefulHost("yourapplication.example.net", EnumSet.of(CallType.READ)),
new StatefulHost("yourapplication.example.net", EnumSet.of(CallType.WRITE))))
.build());
Changing the hosts can be useful if you want to proxy the search requests through another server, for example, to process the request or response, or to perform custom analytics.
Add HTTP headers to every request
Adding HTTP headers to your requests lets you set parameters such as a user identifier or an IP address. This can be useful for analytics, geographical search, or applying API key rate limits.
1
2
3
4
5
6
SearchConfig configuration =
new SearchConfig.Builder("YourApplicationID", "YourWriteAPIKey")
.addExtraHeaders("NAME-OF-HEADER", "value-of-header")
.build();
SearchClient client = DefaultSearchClient.create(configuration);
You can add these headers to your requests:
Header | Use case |
---|---|
X-Algolia-UserToken |
Use API key rate limits |
X-Algolia-UserToken |
The Analytics API uses the value of this header to distinguish between users. It takes priority over any value in X-Forwarded-For . Use the X-Algolia-UserToken header to forward the user’s identity without relying on IP addresses. |
X-Forwarded-For |
Use for analytics in backend implementations. If your server sends the user’s IP address with every search, analytics can distinguish between users. Otherwise, the analytics uses the server’s IP address and considers all your users as a single user. |
X-Forwarded-For |
Use for geolocation, when you perform searches from your backend. This ensures that the geolocation for a search uses your user’s IP address and not that of your server. |
1
2
3
4
5
6
7
8
9
10
SearchIndex<Result> index = client.initIndex("indexName", Result.class);
Query query = new Query("query string");
RequestOptions requestOptions =
new RequestOptions().addExtraHeader("X-Algolia-UserToken", "user123");
// Sync
SearchResult<Result> search = index.search(query, requestOptions);
// Async
CompletableFuture<SearchResult<Result>> search = index.searchAsync(query, requestOptions);
Make sure to use the same user token for your events (Insights API) and search requests (Search API).
- If you send the
authenticatedUserToken
with your events, send the same value with your search requests. - If you send the
userToken
with your events, send the same value with your search requests.
Change timeouts for all requests
Network connections and DNS resolution can be slow. That’s why the API clients come with default timeouts.
1
2
3
4
5
6
7
8
9
SearchConfig configuration =
new SearchConfig.Builder("YourApplicationID", "YourWriteAPIKey")
.setConnectTimeOut(2000) // connection timeout in milliseconds
.setReadTimeOut(5000) // read timeout in milliseconds
.setWriteTimeOut(30000) // write timeout in milliseconds
.setHostDownDelay(300000) // delay before retrying a host we know was down, in milliseconds
.build();
SearchClient client = DefaultSearchClient.create(configuration);
Don’t change the default timeouts without a good reason.
1
2
3
4
5
6
7
8
9
10
11
12
SearchIndex<Result> index = client.initIndex("indexName", Result.class);
Query query = new Query("query string");
RequestOptions requestOptions =
new RequestOptions()
// Set the timeout to 20 seconds
.setTimeout(20000);
// Sync
SearchResult<Result> search = index.search(query, requestOptions);
// Async
CompletableFuture<SearchResult<Result>> search = index.searchAsync(query, requestOptions);