Skip to content

Latest commit

 

History

History
168 lines (115 loc) · 4.54 KB

File metadata and controls

168 lines (115 loc) · 4.54 KB

Getting started

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

  • 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

Installation in a Gradle project by using Jackson

dependencies {
    implementation 'co.elastic.clients:elasticsearch-java:{version}'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
}

Installation in a Maven project by using Jackson

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.

Connecting

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:

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

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.

Creating an index

This is how you create the product index:

include-tagged::{doc-tests-src}/usage/IndexingTest.java[create-products-index]

Indexing documents

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]

Getting documents

You can get documents by using the following code:

include-tagged::{doc-tests-src}/usage/ReadingTest.java[get-by-id]
  1. The get request, with the index name and identifier.

  2. The target class, here Product.

Searching documents

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]

Updating documents

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]

Deleting documents

include-tagged::{doc-tests-src}/usage/IndexingTest.java[single-doc-delete]

Deleting an index

include-tagged::{doc-tests-src}/usage/IndexingTest.java[delete-products-index]

Further reading