This page guides you through the installation process of the Java client, shows you how to instantiate the client, and how to perform basic Elasticsearch operations with it.
-
Java 8 or later.
-
A JSON object mapping library to allow seamless integration of your application classes with the Elasticsearch API. The examples below show usage with Jackson.
dependencies {
implementation 'co.elastic.clients:elasticsearch-java:{version}'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.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>{version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
</dependencies>
</project>Refer to the Installation page to learn more.
You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint.
include-tagged::{doc-tests-src}/getting_started/ConnectingTest.java[create-client]Your Elasticsearch endpoint can be found on the My deployment page of your deployment:
You can generate an API key on the Management page under Security.
For other connection options, refer to the Connecting section.
Time to use Elasticsearch! This section walks you through the basic, and most important, operations of Elasticsearch. For more operations and more advanced examples, refer to the [usage] page.
This is how you create the product index:
include-tagged::{doc-tests-src}/usage/IndexingTest.java[create-products-index]This is a simple way of indexing a document, here a Product application object:
include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-dsl]You can get documents by using the following code:
include-tagged::{doc-tests-src}/usage/ReadingTest.java[get-by-id]-
The get request, with the index name and identifier.
-
The target class, here
Product.
This is how you can create a single match query with the Java client:
include-tagged::{doc-tests-src}/usage/SearchingTest.java[search-getting-started]This is how you can update a document, for example to add a new field:
include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-update]include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-delete]include-tagged::{doc-tests-src}/usage/IndexingTest.java[delete-products-index]-
Learn more about the [api-conventions] of the Java client.

