Spring Boot Elasticsearch 6

In this post, we will setup up a sample Spring boot Elasticsearch application. We will use latest version of Elasticsearch i.e. 6.1.x. To interact with the Elasticsearch search engine, we will use Elasticsearch Rest client. We are not using Spring Data ElasticSearch because that doesn’t support latest ElasticSearch version i.e 6.x.

Setting up the project

Maven Dependencies

We will use Maven build system for this project and here are the dependencies we used:

Make sure to use stable version for Spring Boot from the maven central.

Elasticsearch Configuration

Now, we will have to configure ElasticSearch in our application. Let’s d this in two parts. First, we will provide Elasticsearch address in our application.properties file:

We only provided the Elasticsearch cluster name and node name here and these are actually the default values. Now, it’s time to put these values to use in our Java config class.

With this configuration, we ensured that ElasticSearch is able to make a successful connection to its server using the Rest Client API.

Working the app

Let’s start putting the working components of the app now.

Model for the app

We will be just using a simple model as a User:

We will be making following functionalities and Database interactions in our app:

  • Get a book with ID
  • Insert a Book
  • Update a Book
  • Delete a Book

Defining the Controller

Let us move to making our Controller:

We just Autowired the DAO dependency and we will use this next.

Defining the APIs

For the functionalities we mentioned, we will now be making APIs and accessing the DAO dependency which will internally use Elasticsearch Rest Client API.

Get a book with ID

Let us get a book with ID:

Insert a Book

Now, let us insert a book now:

Update a book

We will be updating a book in this snippet:

Deleting a Book

Now that we have added sample data into the DB, let’s try to extract some part of it:

This is the ease Spring Data API offers us but it also has some downsides. We will elaborate this when we Have defined the ElasticsearchTemplate version as well. Let’s get started with that too.

Defining the DAO layer

Now, we will actually define the DAL queries which achieves these objectives.

Defining DAO

We will start by mentioning the dependencies we need:

Insert Query

We will start by inserting a Book into the ES:

Clearly, we need to convert the model data to a Map data structure before we insert it into the ES database.

Search Query

We will now search a book with an ID:

Here, this is to be noted that data is searched and got back as a Map data structure as well. This is something we need to handle, converting data to Map and forth to make DB transactions.

Deleting Data

We will now search a book with an ID:

I think deleting an object was having the easiest query. Let’s try this application now by running it.

Running the application

We can run this app simply by using a single command:

Once the app is running, we can try saving a new book by using this API. Before doing that, just confirm that the ES is running by using this API:

We will get this response:

elasticsearch-installation-confirmation
Now let’s inserting the data by using following API:

We use a POST request with this JSON:

We have the following response:

elasticsearch-data-insertion

Let’s try another request to get the book with above ID. We will use GET request on this API:

This is what we get back:
elasticsearch-data-get-request
Go on and try more APIs we defined.

Summary

In this lesson, we looked at how Elasticsearch can be queried using a Rest Client API it provides. Download the source code here and modify it.

By admin

Leave a Reply

%d bloggers like this: