Elasticsearch is a real-time distributed and open source full-text search and analytics engine. To integrate Elasticsearch to our application, we need to use an API. Elasticsearch gives us two ways, REST APIs, and Native clients. It’s easy to get confused about all the different ways to connect to Elasticsearch and why one of them should be preferred over the other. Available Elasticsearch clients are:
- Node client
- Transport Client
- HTTP client
We will be exploring Java HTTP REST client i.e. JEST. Jest can be used when you need a lightweight client in your application (regarding jar size or memory consumption). Jest provides an implementation of the Elasticsearch REST API. It is a Java HTTP Rest client for ElasticSearch. It’s API is similar to Elasticsearch API.
For developing the application using Jest, I have used scala as programing language and SBT as a build tool.
All of the interaction happening with Elasticsearch by using JestClient can be done using a factory. So let us start by creating a JestClient instance:
lazy val factory = new JestClientFactory factory.setHttpClientConfig(new HttpClientConfig.Builder(“http://localhost:9200 “).build()) factory.getObject
- JestClient is designed to be a singleton, we should not construct it for each request. That’s the reason we have created it as a lazy variable so that it is created only once when the application starts.
- Elasticsearch runs on port 9200. Elasticsearch should be up and running. You can download it from here.
For creating the above client, we need to add the following dependency in our build.sbt file:
"io.searchbox" % "jest" % "5.3.3"
For communicating with Elasticsearch, there are two ways to send queries. Either we can send a query as string or use builder classes of Elasticsearch, and for using builder classes of Elasticsearch we need to include the following dependency in our project:
"org.elasticsearch" % "elasticsearch" % "5.3.3"
For basic concepts of ElasticSearch, you can visit here
We will be covering the basic operations of modifying data in Elasticsearch
Let us create an index in Elasticsearch. To create an index just pass the associated
CreateIndex action to the client:
val index: Index = new Index.Builder(source)
Search queries can be either JSON String or created by Elasticsearch SourceBuilder. Jest works with default Elasticsearch queries.
//build query with QueryBuilder
val searchSourceBuilder: SearchSourceBuilder = new SearchSourceBuilder()
val query = searchSourceBuilder.query(QueryBuilders.matchAllQuery())
In the above snippet, we have created a matchAllQuery using SearchSourceBuilder class.
val search = new Search.Builder(query.toString).addIndex(index).addType(docType).build()
Now, we can search using SearchBuilder and execute it by using the client.
Documents in ElasticSearch can also be updated very easily using UpdateBuilder.
val updateQuery = new Update.Builder(“UpdatedDocument”).index(“index”).`type`(“docType”).build()
We can also delete the documents which are no longer needed by using DeleteBuilder.
val deleteRequest = new Delete.Builder(“”).index(“index”).build()
I hope this blog helped the readers in performing the basic operations in Elasticsearch. There are many more operations that can also be performed using JestClient.
We can also visualize the data using Kibana. Kibana is an open source analytics and visualization platform designed to work with Elasticsearch. You use Kibana to search, view, and interact with data stored in Elasticsearch indices. you can download Kibana from here.
Here is the link to the complete demo.