Installation

Requirements:

  • Java 17 or later.
  • A JSON object mapping library to allow seamless integration of your application classes with the Elasticsearch API. The Java client has support for Jackson or a JSON-B library like Eclipse Yasson.

Releases are hosted on Maven Central. If you are looking for a SNAPSHOT version, the Elastic Maven Snapshot repository is available at https://snapshots.elastic.co/maven/.

dependencies {
    implementation 'co.elastic.clients:elasticsearch-java:9.5.0'
}
		

In the pom.xml of your project, add the following repository definition and dependencies:

<project>
  <dependencies>

    <dependency>
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>9.5.0</version>
    </dependency>

  </dependencies>
</project>
		

The Java API Client communicates with Elasticsearch through an HTTP client, and two implementations are available:

  • Rest5Client, the default and recommended implementation, based on the Apache HttpClient 5 library. It is included in the elasticsearch-java dependency, so no additional dependency is needed to use it.
  • RestClient, the legacy implementation, based on the deprecated Apache HttpClient 4 library. Since version 9.0 it is an optional dependency, meaning that it has to be added explicitly to the project.

Refer to the transport layer documentation for guidance on which implementation to choose.

To use the legacy RestClient, add the following dependency, using the same version as elasticsearch-java:

dependencies {
    implementation 'org.elasticsearch.client:elasticsearch-rest-client:9.5.0'
}
		
<project>
  <dependencies>

    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>elasticsearch-rest-client</artifactId>
      <version>9.5.0</version>
    </dependency>

  </dependencies>
</project>
		
Note

This is especially relevant when upgrading an application from version 8.x, where the legacy RestClient was the only available HTTP client and was included by default. Applications upgrading to 9.x that keep using the legacy RestClient must add this dependency to keep compiling. Refer to the 9.0.0 release notes for the full list of breaking changes.

It may happen that after setting up the dependencies, your application fails with ClassNotFoundException: jakarta.json.spi.JsonProvider.

If this happens, you have to explicitly add the jakarta.json:jakarta.json-api:2.1.3 dependency.

dependencies {
    ...
    implementation 'jakarta.json:jakarta.json-api:2.1.3'
}
		
<project>
  <dependencies>
    ...
    <dependency>
      <groupId>jakarta.json</groupId>
      <artifactId>jakarta.json-api</artifactId>
      <version>2.1.3</version>
    </dependency>

  </dependencies>
</project>
		

Why is this needed?

Some frameworks like Spring Boot or Helidon come with their Gradle and Maven plugins or their Maven BOM files to ease development and dependency management. These plugins and BOM define the versions to use for a number of well-known libraries.

One of these libraries can be jakarta.json:json-api that defines the standard Java JSON API. In version 1.x this library used the javax.json package, while in version 2.x it uses the jakarta.json package after the transition from JavaEE to JakartaEE.

The Java API Client depends on version 2.1.3 of this library, in order to use the newer and future-proof jakarta.json package. But some build plugins and BOMs override the Java API Client’s dependency to use version 1.x in the older javax.json namespace, resulting in ClassNotFoundException: jakarta.json.spi.JsonProvider.

Adding the correct version as top-level project dependency solves the problem.