Skip to main content

Installation

warning

The amount of changes in this new version is significant. If you are upgrading for v4, you should thoroughly test your application before deploying to production.

To get started, add the algoliasearch-client-java dependency to your project, either with Maven:

xml
<repositories>
<repository>
<id>oss.sonatype.org-snapshot</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-client-java</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
xml
<repositories>
<repository>
<id>oss.sonatype.org-snapshot</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-client-java</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>

or Gradle:

groovy
repositories() {
maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
testImplementation 'com.algolia:algoliasearch-client-java:4.0.0-SNAPSHOT'
}
groovy
repositories() {
maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
testImplementation 'com.algolia:algoliasearch-client-java:4.0.0-SNAPSHOT'
}

Using the client

You can now import the Algolia API client in your project and play with it.

java
import com.algolia.model.search.*;
import java.util.*;
// Instantiate the client
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");
// Add a new record to your Algolia index
Map<String, String> body = new HashMap<>();
body.put("objectID", "id1");
body.put("title", "My Algolia Object");
client.saveObject("<YOUR_INDEX_NAME>", body);
// Fetch search results
SearchMethodParams searchMethodParams = new SearchMethodParams();
SearchQueries requests = new SearchQueries();
requests.setIndexName("<YOUR_INDEX_NAME>");
requests.setQuery("my aloglia ojbect");
searchMethodParams.setRequests(requests);
try {
SearchResponse result = client.search(
searchMethodParams
);
System.out.println(result);
} catch (AlgoliaRuntimeException e) {
e.printStackTrace();
}
java
import com.algolia.model.search.*;
import java.util.*;
// Instantiate the client
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");
// Add a new record to your Algolia index
Map<String, String> body = new HashMap<>();
body.put("objectID", "id1");
body.put("title", "My Algolia Object");
client.saveObject("<YOUR_INDEX_NAME>", body);
// Fetch search results
SearchMethodParams searchMethodParams = new SearchMethodParams();
SearchQueries requests = new SearchQueries();
requests.setIndexName("<YOUR_INDEX_NAME>");
requests.setQuery("my aloglia ojbect");
searchMethodParams.setRequests(requests);
try {
SearchResponse result = client.search(
searchMethodParams
);
System.out.println(result);
} catch (AlgoliaRuntimeException e) {
e.printStackTrace();
}

Advanced use cases

If you don't find a use case that suits your needs, please request it.

You can learn more on how to use Algolia in your project by reading our dedicated guides for advanced use cases.