Creating a Spring Boot Project with Elasticsearch
Tags: elasticsearch, webdev, java, springboot
Published: 2021-09-09 03:08:19

Hey, guys. In this article I will briefly explain how to create and configure a project using [Spring Boot](https://spring.io/projects/spring-boot) and  [Elasticsearch](https://www.elastic.co/)(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/](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](https://drive.google.com/uc?export=view&id=1OgQ63yB-NSeNgGFtd_bLXy8UbsHnydjV)

### 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](https://linuxhint.com/install_configure_docker_ubuntu/) 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker).

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

![image](https://drive.google.com/uc?export=view&id=1sCibfL8GAl5YCG86stweCaG-cFVpETdf)

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](https://drive.google.com/uc?export=view&id=1tXS_P4PKN3rR7WUa41U0Kv8SFou0FojM)

### 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](https://drive.google.com/uc?export=view&id=1FIV8JG4I8dPnLQO7sivAmp5vkqSvLgD2)

### 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](https://drive.google.com/uc?export=view&id=1j1YWBLXkc5nQ5vXJglvPk8tI-ouzw6p8)

### 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](https://drive.google.com/uc?export=view&id=1nWtxWN7thNWhFM4PKjeZjeVw2y_P8wJr)

### 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](https://drive.google.com/uc?export=view&id=1RRdSOlpq68SJEZaexoUG94uPYEvrLKqm)

### 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](https://drive.google.com/uc?export=view&id=1aFS575M66tmmddgQmYXf-zmapZ3THPM_)

![image](https://drive.google.com/uc?export=view&id=18phRHirlPlfODd6BfacFT5VZ8Ay2XCvm)

![image](https://drive.google.com/uc?export=view&id=11-bwfHBh78Eo4OgyWY-7fyyD_j62IM2Y)

![image](https://drive.google.com/uc?export=view&id=1stxkl-1xT31h4TbGkpFy24VeH63li_z1)

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 :wave: :ghost:



_Cover image by <a href="https://pixabay.com/pt/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=336377">Pixabay</a>_