The Java API clients let you interact with Algolia’s APIs from your Java backend.

Install the library

All API clients are part of the algoliasearch Java package.

If you use Maven, add the following to your pom.xml file:

1
2
3
4
5
<dependency>
  <groupId>com.algolia</groupId>
  <artifactId>algoliasearch</artifactId>
  <version>4.4.0</version>
</dependency>

If you use Gradle, add the following to your build.gradle file:

1
2
3
dependencies {
    implementation 'com.algolia:algoliasearch:4.4.0'
}

Test your installation

To test your installation, create a new Java project in your favorite IDE and run a short program that adds a record to a test index, searches your index, and prints the results.

  1. If you haven’t already, create an Algolia account.

  2. Copy the following code into your IDE. Replace ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with values from your account. Make sure to use an API key with addObject and search permissions.

    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
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    package com.algolia.example;
    
    import com.algolia.api.SearchClient;
    import com.algolia.model.search.SearchForHits;
    import com.algolia.model.search.SearchMethodParams;
    
    import java.util.HashMap;
    
    public class Main {
        public static void main(String[] args) {
            final var appID = "ALGOLIA_APPLICATION_ID";
            // API key with `addObject` and `search` ACL
            final var apiKey = "ALGOLIA_API_KEY";
            final var indexName = "test-index";
    
            try (var client = new SearchClient(appID, apiKey)) {
                // Create a new record
                var body = new HashMap<>();
                body.put("objectID", "object-1");
                body.put("name", "test record");
    
                // Add the record to an index
                var addResponse = client.saveObject(indexName, body);
    
                // Wait until indexing is done
                client.waitForTask(indexName, addResponse.getTaskID());
    
                // Search for 'test'
                var responses = client.search(
                    new SearchMethodParams().addRequests(
                        new SearchForHits()
                          .setIndexName(indexName)
                          .setQuery("test")
                    ), body.getClass());
                System.out.println(responses);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    In production, don’t include your credentials in your code. Use environment variables instead.

  3. Run the program.

If the program completed successfully, you’ll see the API response.

Next steps

You can view your new index in the Algolia dashboard.

Did you find this page helpful?
Java API clients v4