<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jhonattan Cabral</title>
    <description>The latest articles on DEV Community by Jhonattan Cabral (@jhon3).</description>
    <link>https://dev.to/jhon3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F336339%2F0cfa6ffa-a28e-44f5-b54b-e4eba6ad48f8.jpeg</url>
      <title>DEV Community: Jhonattan Cabral</title>
      <link>https://dev.to/jhon3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhon3"/>
    <language>en</language>
    <item>
      <title>Creating a Spring Boot Project with Elasticsearch</title>
      <dc:creator>Jhonattan Cabral</dc:creator>
      <pubDate>Thu, 09 Sep 2021 03:08:19 +0000</pubDate>
      <link>https://dev.to/jhon3/creating-a-spring-boot-project-with-elasticsearch-187c</link>
      <guid>https://dev.to/jhon3/creating-a-spring-boot-project-with-elasticsearch-187c</guid>
      <description>&lt;p&gt;Hey, guys. In this article I will briefly explain how to create and configure a project using &lt;a href="https://spring.io/projects/spring-boot" rel="noopener noreferrer"&gt;Spring Boot&lt;/a&gt; and  &lt;a href="https://www.elastic.co/" rel="noopener noreferrer"&gt;Elasticsearch&lt;/a&gt;(ES). To start with, it is important to define the two technologies that we will use in the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spring Boot
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Elasticsearch
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generating the Project
&lt;/h3&gt;

&lt;p&gt;Let's now attack what matters. First, we must go to the website &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;https://start.spring.io/&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1OgQ63yB-NSeNgGFtd_bLXy8UbsHnydjV" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1OgQ63yB-NSeNgGFtd_bLXy8UbsHnydjV" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Elasticsearch
&lt;/h3&gt;

&lt;p&gt;For our project we will need to install ES. However, to make it easier, let's use a Docker image. By following this &lt;a href="https://linuxhint.com/install_configure_docker_ubuntu/" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; 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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pulling the image&lt;br&gt;
&lt;code&gt;docker pull docker.elastic.co/elasticsearch/elasticsearch:7.14.0&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Starting a single node cluster with Docker&lt;br&gt;
&lt;code&gt;docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.14.0&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details, see: &lt;a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker" rel="noopener noreferrer"&gt;Install Elasticsearch with Docker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To initialize the image and verify that it is working correctly, just run:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1sCibfL8GAl5YCG86stweCaG-cFVpETdf" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1sCibfL8GAl5YCG86stweCaG-cFVpETdf" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that Elasticsearch is running locally at addresses &lt;code&gt;0.0.0.0:9200-&amp;gt;9200/tcp, :::9200-&amp;gt;9200/tcp, 0.0.0.0:9300-&amp;gt;9300/tcp, :::9300-&amp;gt;9300 /tcp.&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Opening the project
&lt;/h3&gt;

&lt;p&gt;Going back to our project, let's extract and open it using IntelliJ as shown in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1tXS_P4PKN3rR7WUa41U0Kv8SFou0FojM" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1tXS_P4PKN3rR7WUa41U0Kv8SFou0FojM" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration file creation
&lt;/h3&gt;

&lt;p&gt;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 &lt;strong&gt;repository&lt;/strong&gt; and inside it the file called &lt;strong&gt;ElasticSearchConfiguration&lt;/strong&gt;. Shown below are the necessary settings for connect the database. Attention to the url and port information which are the same as shown before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1FIV8JG4I8dPnLQO7sivAmp5vkqSvLgD2" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1FIV8JG4I8dPnLQO7sivAmp5vkqSvLgD2" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Model creation
&lt;/h3&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1j1YWBLXkc5nQ5vXJglvPk8tI-ouzw6p8" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1j1YWBLXkc5nQ5vXJglvPk8tI-ouzw6p8" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Repository creation
&lt;/h3&gt;

&lt;p&gt;After the model, we are able to create the repository, here we are going to create the &lt;strong&gt;PersonRepository&lt;/strong&gt; file inside the &lt;strong&gt;repository&lt;/strong&gt; package defined before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1nWtxWN7thNWhFM4PKjeZjeVw2y_P8wJr" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1nWtxWN7thNWhFM4PKjeZjeVw2y_P8wJr" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Controller creation
&lt;/h3&gt;

&lt;p&gt;To finish our project, let's define our access interface with HTTP protocols. The following image defines the access methods for our project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1RRdSOlpq68SJEZaexoUG94uPYEvrLKqm" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1RRdSOlpq68SJEZaexoUG94uPYEvrLKqm" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Operation
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1aFS575M66tmmddgQmYXf-zmapZ3THPM_" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1aFS575M66tmmddgQmYXf-zmapZ3THPM_" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D18phRHirlPlfODd6BfacFT5VZ8Ay2XCvm" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D18phRHirlPlfODd6BfacFT5VZ8Ay2XCvm" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D11-bwfHBh78Eo4OgyWY-7fyyD_j62IM2Y" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D11-bwfHBh78Eo4OgyWY-7fyyD_j62IM2Y" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1stxkl-1xT31h4TbGkpFy24VeH63li_z1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1stxkl-1xT31h4TbGkpFy24VeH63li_z1" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 👋 👻&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover image by &lt;a href="https://pixabay.com/pt/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=336377" rel="noopener noreferrer"&gt;Pixabay&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>elasticsearch</category>
      <category>webdev</category>
      <category>java</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
