DEV Community

Cover image for Creating a Spring Boot Project with Elasticsearch
Jhonattan Cabral
Jhonattan Cabral

Posted on

Creating a Spring Boot Project with Elasticsearch

Hey, guys. In this article I will briefly explain how to create and configure a project using Spring Boot and Elasticsearch(ES). To start with, it is important to define the two technologies that we will use in the project.

Spring Boot

Spring Boot is one of the main java freamworks. The technology is always up to date and incorporating new features involving database, cloud, security and so on.

Elasticsearch

Developed on Apache Lucene, the non-relational database Elasticsearch has as its main advantage its speed and scalability in indexing many types of content, providing flexible uses for numerous scenarios. After indexing, users can run complex queries based on their data and use aggregations to retrieve complex summaries of the data. The database can be manipulated using the Rest API provided by the technology itself.

Generating the Project

Let's now attack what matters. First, we must go to the website https://start.spring.io/ to generate our project with its possible dependencies. The image below shows all the dependencies that we will use in our project, paying special attention to the ES dependency that is our focus.

image

Installing Elasticsearch

For our project we will need to install ES. However, to make it easier, let's use a Docker image. By following this tutorial you will have no problem installing the tool on your Ubuntu. After installing Docker, to install a working ES image, just run the following commands:

  • Pulling the image
    docker pull docker.elastic.co/elasticsearch/elasticsearch:7.14.0

  • Starting a single node cluster with Docker
    docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.14.0

For more details, see: Install Elasticsearch with Docker.

To initialize the image and verify that it is working correctly, just run:

image

Note that Elasticsearch is running locally at addresses 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300 /tcp.

Opening the project

Going back to our project, let's extract and open it using IntelliJ as shown in the image below:

image

Configuration file creation

Our first step here will be to create the configuration file to link our project to our already installed database, let's create a package called repository and inside it the file called ElasticSearchConfiguration. Shown below are the necessary settings for connect the database. Attention to the url and port information which are the same as shown before.

image

Model creation

Now let's create a model for our example project, here we'll create the model package and inside it we'll create a Person file that will define the structure of our model.

image

Repository creation

After the model, we are able to create the repository, here we are going to create the PersonRepository file inside the repository package defined before.

image

Controller creation

To finish our project, let's define our access interface with HTTP protocols. The following image defines the access methods for our project:

image

Operation

After the end of the project we can start it, but make sure the ES is active before (shown above). The images below show the project startup and some tests validating its basic operation.

image

image

image

image

More articles will be published below using this as a basis, so it is important to understand the basics of project creation well. see u soon 👋 👻

Cover image by Pixabay

Top comments (0)