Elasticsearch Client for Java: Complete Guide
Elasticsearch is a powerful distributed search and analytics system based on Lucene. For interacting with it from Java applications, there is an official client that provides a convenient API for indexing, searching, and managing data. In this article, we will thoroughly cover the installation, configuration, and main capabilities of the Elasticsearch Java client, as well as provide practical code examples.
What is the Elasticsearch Java Client?
The Elasticsearch Java client is an official library designed for interacting with an Elasticsearch cluster from Java applications. It supports two main approaches:
- Low-level REST Client — a low-level client that works directly with HTTP requests. It gives full control but requires manual JSON formatting.
- High-level REST Client — a high-level wrapper that provides an object-oriented API for typical operations (indexing, search, update). Recommended for most projects.
Starting with Elasticsearch 7.0, it is recommended to use the High-level REST Client, as it is more convenient and secure.
Installation and Setup
To add the client to a Java (Maven) project, add the following dependency to the pom.xml file:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.17.0</version></dependency>If you are using Gradle, add the line:
implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.17.0'After adding the dependency, create a client instance:
import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.client.RestClient;import org.apache.http.HttpHost;
public class ElasticsearchClientExample { public static void main(String[] args) { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // Your code for working with Elasticsearch try { client.close(); } catch (IOException e) { e.printStackTrace(); } }}Basic Operations with the Client
Creating an Index
Before working with data, you need to create an index (analogous to a database in SQL). Example:
import org.elasticsearch.client.indices.CreateIndexRequest;import org.elasticsearch.client.indices.CreateIndexResponse;import org.elasticsearch.client.RequestOptions;
public void createIndex(RestHighLevelClient client, String indexName) throws IOException { CreateIndexRequest request = new CreateIndexRequest(indexName); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println("Index created: " + response.isAcknowledged());}Indexing a Document
Documents in Elasticsearch are JSON objects. To add a document, use IndexRequest:
import org.elasticsearch.action.index.IndexRequest;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.common.xcontent.XContentType;import java.util.HashMap;import java.util.Map;
public void indexDocument(RestHighLevelClient client, String indexName) throws IOException { Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("title", "Elasticsearch client for Java"); jsonMap.put("content", "Detailed description of working with Elasticsearch from Java."); jsonMap.put("views", 150);
IndexRequest request = new IndexRequest(indexName) .id("1") .source(jsonMap);
IndexResponse respon Also in library
Caching in C++: Redis, Memcached, and Built-in Solutions
Task Scheduler in C# (.NET): Quartz.NET and System.Threading.Timer
rustls for Rust: Complete Guide with Examples