You are viewing docs on Elastic's new documentation system, currently in technical preview. For all other Elastic docs, visit

Get started with the serverless Java client

Set up and use the Java client for Elasticsearch on serverless.

This page guides you through the installation process of the Java client for Elasticsearch on serverless, shows you how to initialize 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.


You can add the Java client to your Java project using either Gradle or Maven.

Using Gradle

You can install the Java client as a Gradle dependency:

dependencies {
    implementation 'co.elastic.clients:elasticsearch-java-serverless:1.0.0-20231031'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'

Using Maven

You can install the Java client as a Maven dependency, add the following to the pom.xml of your project:





Initialize the client

Initialize the client using your API key and Elasticsearch Endpoint:

// URL and API key
String serverUrl = "";
String apiKey = "VnVhQ2ZHY0JDZGJrU...";

// Create the low-level client
RestClient restClient = RestClient
    .setDefaultHeaders(new Header[]{
        new BasicHeader("Authorization", "ApiKey " + apiKey)

// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
    restClient, new JacksonJsonpMapper());

// And create the API client
ElasticsearchClient esClient = new ElasticsearchClient(transport);

To get API keys or the Elasticsearch Endpoint for a project, see Get started.

Using the API

After you initialized the client, you can start ingesting documents.

Creating an index and ingesting documents

The following is an example of indexing a document, here a Product application object in the products index:

Product product = new Product("bk-1", "City bike", 123.0);

IndexResponse response = esClient.index(i -> i
);"Indexed with version " + response.version());


Now that some data is available, you can search your documents using the search API:

String searchText = "bike";

SearchResponse<Product> response = -> s
    .query(q -> q
        .match(t -> t

A few things to note in the above example:

  • The search query is built using a hierarchy of lambda expressions that closely follows the Elasticsearch HTTP API. Lambda expressions allows you to be guided by your IDE's autocompletion, without having to import (or even know!) the actual classes representing a query.
  • The last parameter Product.class instructs the client to return results as Product application objects instead of raw JSON.


You can update your documents using the update API:

Product product = new Product("bk-1", "City bike", 123.0);

esClient.update(u -> u


You can also delete documents:

esClient.delete(d -> d.index("products").id("bk-1"));

Deleting an index

esClient.indices().delete(d -> d.index("products"));

On this page