Java High-Level REST Client: Elasticsearch
Learn about using a Java high-level REST client with Elasticsearch to use API-specific methods that accept request objects as an argument and return response objects.
Join the DZone community and get the full member experience.Join For Free
Elasticsearch is an open-source, highly scalable full-text search and analytics engine. Using it, you can easily store, search, and analyze a large amount of data in real time. The Java REST client is the official client for Elasticsearch and comes in two flavors:
- Java low-level REST client: It allows communicating with an Elasticsearch cluster through HTTP and leaves requests marshaling and responses un-marshaling to users.
- Java high-level REST client: It is based on a low-level client and exposes API-specific methods, taking care of requests marshaling and responses un-marshaling.
Our focus here will be to learn about the 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 a 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 and deserialization of request and response objects is handled by the client itself. Each API can be called either synchronously or asynchronously.
Let's discuss how we can use a 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 the high-level REST client depends on the Elasticsearch core, don't forget to add an Elasticsearch core dependency.
"org.elasticsearch" % "elasticsearch" % "6.1.2"
The REST high-level client instance can be built as follows:
val client = new RestHighLevelClient( RestClient.builder(new HttpHost(HOST, PORT, "http")))
Here, you can replace HOST with the IP address on which Elasticsearch is running. 9200 is the port to send REST requests to for that node.
The Java high-level REST client supports various APIs — Index, Update, Search, Get, Delete, and Bulk are some of those APIs, and there are many more.
CRUD and Search Operations
With the help of a REST client, we can perform CRUD (Create, Read, Update, and Delete) and search operations against our indexes. Let's just have a quick review of these features.
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, the document source should be provided with the request in JSON and other supported formats. Here's an example:
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.)
Then, you can execute the request with the help of the client you created before.
Update an Existing Document
To update a document, you need to prepare an UpdateRequest passing index, type, and ID as arguments and then use a script or a partial document for updating. Then, execute 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. 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)
SearchRequest is used for any operation that has to do with searching documents, aggregations, and 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. Add to it the query you want to execute.
val searchSourceBuilder = new SearchSourceBuilder searchSourceBuilder.query(QueryBuilders.matchAllQuery()) searchRequest.source(searchSourceBuilder)
Lastly, execute the SearchRequest through the REST client.
There are several other operations you can execute via a high-level REST client. Also, You can use Kibana to search, view, and interact with data stored in Elasticsearch indices. To understand Kibana, you can go through the documentation.
The complete demo code is available here. You can check the README.md file for instructions to run the application.
Published at DZone with permission of Divya Dua, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.