ITZone

Installation instructions and differences between 2 versions 8.x.x and 7.x.x Elasticsearch

In this article, I will guide you in 2 parts:

  • Configure Elasticsearch
  • Create project with spring boot using spring data elasticsearch library

Tools and libraries used in the article:

  • Spring tool suite 4
  • Spring boot 2.7.3
  • Spring data elasticsearch 4.4.2
  • Maven 3
  • Java 8
  • Elasticseah 8.4.1
  • Elasticsearch 7.17.6
  • Postman
  • Elasticsearch head extension

1. Install Elasticsearch:

First, visit the link below to download the latest Elasticsearch:

Link: https://www.elastic.co/downloads/elasticsearch

If you want to download Elasticsearch lower version, visit the link below:

Link: https://www.elastic.co/downloads/past-releases

In this article I download Elasticsearch zip file. After downloading, extracting it, you will get a folder like this:

Directory structure:

Go to the “config” folder and open the “elasticsearch.yml” file:

Contents of config file:

There is really no need to edit any values ​​in the config file but there are 2 values ​​we need to take care of

  • cluster.name -> set a name for the cluster. Here I will delete the “#” character and change the name to “employee-cluster”
  • path.data -> set the path to save data. Here I will reset the path to “G:/elasticsearch-8.4.1/data”

By default, in the extracted Elasticsearch folder, there will be no “data” folder, so I will create a folder named “data” myself:

Next to start elasticsearch, I open the folder named “bin” and run the script file “elasticsearch.bat”:

After successful launch, visit the path “localhost:9200” to check the Elasticsearch status:

As you can see, elasticsearch is still not working, check the cmd screen and see some connection errors:

This error occurs on Elasticsearch version 8 and to fix this error, I will open the “elasticsearch.yml” file in the “config” folder to correct some information:

There are 4 values ​​that need to be changed, the default is “true” so I’ll change it to “false”:

  • xpack.security.enabled: true -> xpack.security.enabled: false
  • xpack.security.enrollment.enabled: true -> xpack.security.enrollment.enabled: false
  • xpack.security.http.ssl: enabled: true => xpack.security.http.ssl: enabled: false
  • xpack.security.transport.ssl: enabled: true => xpack.security.transport.ssl: enabled: false

After updating the 4 values ​​again, go to the folder “../bin” and launch the script file “elasticsearch.bat”:

After successful launch, continue to retry accessing the path “localhost:9200”:

We see that elasticsearch has successfully launched

Next I introduce you to an extension on chrome that supports checking data on elasticsearch called “elasticsearch head”:

Link: https://chrome.google.com/webstore/detail/multi-elasticsearch-head/cpmmilfkofbeimbmgiclohpodggeheim

After installing the above extension successfully, click the extension icon:

Select “Multi Elasticsearch Head” and this is what it looks like:

If you click on the “Structured Query” tab, you will see that there are currently no documents:

So basically the installation and configuration of Elasticsearch is done. Next I will create a project with Spring boot and Spring data elasticsearch.

2. Directory structure:

3. Contents of pom file:

4. Create class “Employee” in package “com.example.elasticsearch.model”:

In the class “Employee” there are 2 annotations, I will analyze each one:

  • Document : If you have ever worked with Hibernate, you will know that ” Table ” -> ” Document ” is similar to ” Table “, it will map this “Employee” class with the index name of ” employee”
  • @Id: Annotation is used to mark that this variable “id” is the primary key

And one note, with Elasticsearch version 8.xx, the “type” attribute has been removed. You can refer to this information on elastic homepage:

Link: https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html

Continuing, when I finished creating the “Employee” class, I started the project and went to the “Elasticsearch head” interface to check if the “document” was created:

Click on the icon circled in red to reset the state:

After successful reset, you will see the word “Green” change to “Yellow” and already have “document” with index name “employee”.

I click on the “Structured Query” tab and I see “document” with index name “employee” created but no data currently available:

5. Create an interface “EmployeeRepository” in the package “com.example.elasticsearch.repository”:

In the interface “EmployeeRepository” I have inherited “ElasticsearchRepository” and declared a method “findByName”, actually by default “ElasticsearchRepository” has supported some methods like “findAll()”; “findById()”;”findByName()”;…

It is necessary to declare the package containing the repositories in the startup class “ElasticSearchApplication”, using the annotation ” EnableElasticsearchRepositories “:

6. Create a class “EmployeeController” in the package “com.example.elasticsearch.controller”:

In the controller class I need to use the methods “findAll()”; “findById()”;”findByName()” is backed by “ElasticsearchRepository”, so I will use the ” AutoWired ” annotation to declare “EmployeeRepository”

I run the test with Postman:

  • Call api creates new employee with data body as a list:

Test results:

Error checking:

First, I will go to Elasticsearch head and check if the data has been successfully added:

You can see that the index name “employee” currently has 2 docs -> the data has been successfully added and in the “Results:” section, leaving the default as “Table” I will choose it again to “JSON” and click ” Search” to see all available data:

The result shows that the data is created successfully, so why give an error? actually this error is encountered in Elasticsearch 8.xx version because I said at the beginning of the article that with Elasticsearch 8.xx version the “type” attribute has been removed and only older versions have the attribute. “type”

This is the cause of the error, you see the error I circled in red above at the “DocWriteResponse” class, in this class in the constructor when assigning a value to the “type” property, it requires the “type” attribute not to be null:

And so when you see “type” null -> throw an error

Actually the data has been added successfully but how to not throw the error, I have 2 solutions:

  • Solution 1 (recommended): At elasticsearch version 7.xx or earlier -> there will be no errors.
  • 2nd solution: Wrap “try{}catch()”.

I would try both solutions above:

  • Second solution:

Result when calling api to create new employee:

Result when calling api delete employee by id:

Result when calling api to update employee by id information:

Result when extracting employee by id information:

=> The data is processed successfully but this solution is not effective because other errors may be ignored and if you want to zone which errors to pass or not then you need to get the error name and exclude out. You can use “{exeption}.getClass().getSimpleName()” to get the error name

I will use the 1st solution, using an older version of Elasticsearch, here I am using version 7.17.6 (the last version of 7.xx). The configuration is the same as I explained at the beginning of the article

Check version information:

Check the Elasticsearch head:

No documents yet

Start project:

Check the Elasticsearch head again:

No data yet

Update “EmployeeControler”, remove “try{}catch()”:

Test run:

Result when calling api to create new employee:

Result when calling api delete employee by id:

Result when calling api to update employee by id information:

Result when extracting employee by id information:

=> You see with Elasticsearch version 7.xx still has the “type” attribute and the api call process does not have any errors.

The article is quite long so I will stop here and in the next post I will create a Spring boot project with thymeleaf and spring data elasticsearch.

 

Share the news now