Elasticsearch Client for Java: Complete Guide
Elasticsearch is a powerful distributed search and analytics system built on Apache Lucene. For integration with Java applications, Elastic provides the official Elasticsearch Java Client (formerly known as the High-Level REST Client). In this article, we will thoroughly examine how to use this client for performing basic operations: connection, document indexing, CRUD operations, and full-text search.
What is Elasticsearch Java Client?
Elasticsearch Java Client is a modern, type-safe API for interacting with Elasticsearch from Java. It is built on top of an asynchronous HTTP client and supports all Elasticsearch versions starting from 7.x. Key advantages:
- Full support for all Elasticsearch APIs (indexing, search, aggregations, cluster management).
- Asynchrony and reactivity — built-in support for CompletableFuture and reactive streams.
- Type-safe DSL queries — minimizing errors at compile time.
- Simple configuration — minimal number of dependencies.
Installation and Configuration
To add the client to a Maven project, include the following dependency in pom.xml:
<dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.15.0</version></dependency>For Gradle (build.gradle):
implementation 'co.elastic.clients:elasticsearch-java:8.15.0'You will also need Jackson for JSON serialization/deserialization:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.0</version></dependency>Creating a Client and Connecting
To create a client, you need to specify the Elasticsearch host and port. Example of basic configuration:
import co.elastic.clients.elasticsearch.ElasticsearchClient;import co.elastic.clients.json.jackson.JacksonJsonpMapper;import co.elastic.clients.transport.rest_client.RestClientTransport;import org.apache.http.Header;import org.apache.http.message.BasicHeader;import org.elasticsearch.client.RestClient;
public class ElasticsearchExample {
public static ElasticsearchClient createClient() { // Create the Low-Level REST client RestClient restClient = RestClient.builder( new HttpHost("localhost", 9200, "http") ).setDefaultHeaders(new Header[]{ new BasicHeader("Content-Type", "application/json") }).build();
// Transport with Jackson mapper RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// High-level client return new ElasticsearchClient(transport); }
public static void main(String[] args) throws IOException { ElasticsearchClient client = createClient(); System.out.println("Client successfully created!"); client.close(); }}Working with Indices
Before starting work, you need to create an index. Let's look at an example of creating an index with a mapping:
public static void createIndex(ElasticsearchClient client, String indexName) throws IOException { client.indices().create(c -> c .index(indexName) .mappings(m -> m .properties("title", p -> p.text(t -> t)) .properties("content", p -> p.text(t -> t)) .properties("price", p -> p.double_(d -> d)) .properties("createdAt", p -> p.date(d -> d)) Also in library
Internationalization (i18n) in Java: A Complete Guide with Examples
Sending SMS in Java: Overview of API Libraries (Twilio, Nexmo, SMS Gateways)
Working with XML in Kotlin: Libraries, Parsing, and Document Creation
CLI Frameworks for Java: Picocli, JCommander, and Spring Shell
Working with S3 and Cloud Storage in JavaScript (Node.js) — @aws-sdk/client-s3 Library