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:
- Java Low-Level REST client – It allows communicating with an Elasticsearch cluster through HTTP and leaves requests marshalling & responses un-marshalling to users.
- 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.
Our focus here will be to learn about High-Level REST client. I hope you are clear with the basics of Elasticsearch if not, you can go through its documentation here.
The Java High-Level REST client works on top of Java Low-Level REST client. It is forward compatible. It allows one to use API specific methods, that accept request objects as an argument and return response objects. 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"
The REST High-level client instance can be build as follows:
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
With the help of REST client, we can perform CRUD (Create, Read, Update, and Delete) and search operations against our indexes. Let’s just have a quick discussion on these features.
How to index a document in Elasticsearch?
To insert a document, first, we need to create an IndexRequest which requires index, type and document Id as arguments. After that, document source should be provided with the request in JSON and other supported formats. Example of the same is given here:
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
For updating the document, you need to prepare an UpdateRequest passing index, type, and id as arguments and then using a script or a partial document for updating. And, then executing the update request through the client.
val updateRequest = new UpdateRequest(index_name, type_name, id) val builder = XContentFactory.jsonBuilder builder.startObject builder.field(fieldName, value) builder.endObject updateRequest.doc(builder) client.update(updateRequest)
Deleting a document just requires two lines of code. First, create a DeleteRequest and then execute it via the REST client.
val deleteRequest = new DeleteRequest(index_name, type_name, id) client.delete(deleteRequest)
Deleting the index is also a simple task. Following is the example for that :
val request = new DeleteIndexRequest(index_name) client.indices().deleteIndex(request)
The SearchRequest is used for any operation that has to do with searching documents, aggregations, suggestions and also offers ways of requesting highlighting on the resulting documents.
First, create a SearchRequest passing the index name as the argument.
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 searchSourceBuilder.query(QueryBuilders.matchAllQuery()) searchRequest.source(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 to search, view, and interact with data stored in Elasticsearch indices. For understanding Kibana, you can go through the following documentation.
The complete demo code is available here. You can check README.md file for instructions to run the application.
Happy Blogging! 🙂