Elasticsearch Java High Level REST Client

Elasticsearch is an open-source, highly scalable full-text search and analytics engine. Using this, you can easily store, search, and analyze a large amount of data in real time. Java REST client is the official client for Elasticsearch which comes in 2 flavors:

  1. Java Low-Level REST client : It allows communicating with an Elasticsearch cluster through HTTP and leaves requests marshalling & responses un-marshalling to users.
  2. Java High-Level REST client : It is based on low-level client and exposes API specific methods, taking care of requests marshalling and responses un-marshalling.

I will focus to talk about High-Level REST client. I hope you are clear with the basics of Elasticsearch if not, you can go through its documentation here.

let’s start !!

Serialization & Deserialization of request & response objects is handled by the client itself. Each API can be called either synchronously or asynchronously.

Let’s discuss how we can use High-level REST client in our Scala-SBT application.

Following is the dependency you need to add to build.sbt for using the client :

"org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "6.1.2"

Since High-level REST client depends on Elasticsearch core, so don’t forget to add Elasticsearch core dependency.

"org.elasticsearch" % "elasticsearch" % "6.1.2"


val client = new RestHighLevelClient(
RestClient.builder(new HttpHost(HOST, PORT, "http")))

Here, you can replace the HOST with IP address on which Elasticsearch is running. And, 9200 is the port to send REST requests to that node.

The Java High-Level REST Client supports the various APIs. Index, Update, Search, Get, Delete, Bulk are some of those APIs and there are many more.

CRUD & Search Operations

How to index a document in Elasticsearch?

val request = new IndexRequest(index_name, type_name, id)
request.source(jsonString, XContentType.JSON)

jsonString refers to the data you want to insert in Elasticsearch.
And, then you can execute the request with the help of client you have created before.


Update an existing document

val updateRequest = new UpdateRequest(index_name, type_name, id)val builder = XContentFactory.jsonBuilder
builder.field(fieldName, value)

Delete operation

val deleteRequest = new DeleteRequest(index_name, type_name, id)

Deleting the index is also a simple task. Following is the example for that :

val request = new DeleteIndexRequest(index_name)

Search documents

val searchRequest = new SearchRequest(index_name)

After that, SearchSourceBuilder needs to be created, adding to it the query you may want to execute.

val searchSourceBuilder = new SearchSourceBuilder

Lastly, executing the SearchRequest through REST client.


There are several other operations you can execute via High-Level REST client. Also, You can use Kibana , cerebro ..to search, view, and interact with data stored in Elasticsearch indices. For understanding Kibana, you can go through the following documentation.

with love ❤ ❤ ❤

A passionate Full Stack Software SpringBoot/Angular Developer 🚀 having an experience of building Web applications with Spring boot / Angular / Postgres / Elast