Getting started

edit

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.

Requirements

edit
  • 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.

Installation

edit

Installation in a Gradle project by using Jackson

edit
dependencies {
    implementation 'co.elastic.clients:elasticsearch-java:8.8.2'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
}

Installation in a Maven project by using Jackson

edit

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>8.8.2</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.3</version>
    </dependency>

  </dependencies>
</project>

Refer to the Installation page to learn more.

Connecting

edit

You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint.

// URL and API key
String serverUrl = "https://localhost:9200";
String apiKey = "VnVhQ2ZHY0JDZGJrU...";

// Create the low-level client
RestClient restClient = RestClient
    .builder(HttpHost.create(serverUrl))
    .setDefaultHeaders(new Header[]{
        new BasicHeader("Authorization", "ApiKey " + apiKey)
    })
    .build();

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

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

Your Elasticsearch endpoint can be found on the My deployment page of your deployment:

Finding Elasticsearch endpoint

You can generate an API key on the Management page under Security.

Create API key

For other connection options, refer to the Connecting section.

Operations

edit

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 Using the Java API Client page.

Creating an index

edit

This is how you create the product index:

esClient.indices().create(c -> c
    .index("products")
);

Indexing documents

edit

This is a simple way of indexing a document, here a Product application object:

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

IndexResponse response = esClient.index(i -> i
    .index("products")
    .id(product.getSku())
    .document(product)
);

logger.info("Indexed with version " + response.version());

Getting documents

edit

You can get documents by using the following code:

GetResponse<Product> response = esClient.get(g -> g
    .index("products") 
    .id("bk-1"),
    Product.class      
);

if (response.found()) {
    Product product = response.source();
    logger.info("Product name " + product.getName());
} else {
    logger.info ("Product not found");
}

The get request, with the index name and identifier.

The target class, here Product.

Searching documents

edit

This is how you can create a single match query with the Java client:

String searchText = "bike";

SearchResponse<Product> response = esClient.search(s -> s
        .index("products")
        .query(q -> q
            .match(t -> t
                .field("name")
                .query(searchText)
            )
        ),
    Product.class
);

Updating documents

edit

This is how you can update a document, for example to add a new field:

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

esClient.update(u -> u
        .index("products")
        .id("bk-1")
        .upsert(product),
    Product.class
);

Deleting documents

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

Deleting an index

edit
esClient.indices().create(c -> c
    .index("products")
);

Further reading

edit