Spring Boot Elasticsearch

In my last blog, I talked about how we can use Spring-Data-Elasticsearch project to connect with Elasticsearch Engine and do all CRUD operations. However, I did also mention that this project is not updated to be compatible with latest version of elasticsearch engine. So, In this blog, I am going to cover how to interact with latest version of elasticsearch engine using Transport Client library. I am going to use Spring Boot as Client application and then add dependencies for other required libraries.
  • Jdk 1.8
  • Maven
  • Elasticsearch engine download 5.x or 6.x ( I will explain the steps how to download)
  • Eclipse or VSD as IDE
Setup Elasticsearch 
Step 1 - Go to elastic official website https://www.elastic.co/downloads/past-releases
Step 2 - Select Elasticsearch in drop down and then version as 5.5.0 and click on Download button.
Step 3 - It will give you options if you want to download as Zip, TAR, RPM. You can choose as per your convenience. I selected the Zip format as using it on windows. Step 4 - unzip the downloaded content and go to  bin  folder. There will be a file named elasticsearch.bat.
Step 5 - Run this file on windows OS through command prompt and it will start the elasticsearch engine for you. Once started, it will start listening to port 9200. So the url will be http://localhost:9200/ Also, 9300 port is exposed as cluster node. 9200 port is for REST communication and can be used by Java or any other language and 9300 is for Elasticsearch Cluster Node communication with each other as well Java can also connect to this cluster node using Transport protocol.
Step 6 - Test to verify if it started properly by launching the url using curl command. You can use PowerShell on windows.
curl http://localhost:9200/
Once the search engine starts up, let's try to test some of the REST apis it provides to interact with engine.
Launch -  http://localhost:9200/users/employee/1  using Postman or curl with POST method. Input should be in JSON format.
Response will show that it has created "users" as index name, "employee" as type and document has been created with id as "1".
Now, To view the content of the document, we can call another REST api end point  http://localhost:9200/users/employee/1 using GET method. This will result the document information as below:
Now, if we want to search a document by any particular field, you need to add _search as path in the REST API url. So launch        curl -XGET 'http://localhost:9200/users/employee/_search' 
This will search for all the documents for index "users" and type "employee". Now, if you want to search for a particular field, need to add Query match criteria as part of JSON.
 curl -XGET 'http://localhost:9200/users/employee/_search'
-H 'Content-Type: application/json' -d
' {"query": { "match": {"name" : "Rajesh" } }}' 
This will search for a document having field 'name' as 'Rajesh'. 
Now, we have seen how Elasticsearch REST APIs works to create, retrieve and other operations for the documents; let's try to understand how we can connect to elasticsearch engine from Applications code. Either, we can make call to these REST apis directly from the code or we can use Transport Client provided by Elasticsearch. Let's develop an Spring Boot application to showcase all the CRUD operations.
Develop Spring Boot Application
Maven Dependencies 
Other than spring boot jars, It requires elasticsearch, transport client and log4j jars.
Configuration -
As we will be using Transport client to connect to elasticsearch engine, we need to give url path for the cluster node of engine. so i have put properties in application.properties file for the host and port of the url.
Domain -
Create a domain class named User. JSON input will be mapped to this User object. This will be used to create user document associated with the index and type. 
Configuration -
Java configuration file has been created to create Transport client which connects to elasticsearch cluster node. It is also loading the value of host and port from environment configured through application.properties
Controller -
UserController is created to showcase below features -
1.     Create an Index named "users" and Type "employee". It will create a document storing user information. Id for a document can be passed as input in JSON or if it is not passed, elasticsearch will generate its own Id. Client has a method called prepareIndex() which builds the document object and store against the index and type. This method is a POST method call where User information will be passed as JSON.
2. View the user information based on "id" passed. Client has prepareGet() method to retrieve information based on index, type and id. It will return the User Information in JSON format. 
3. View the user information based on a field name. I have used matchQuery() here to search by "name" field and return User information. However, there are many different types of query() available with QueryBuilders class . e.g rangeQuery() to search a field value in particular range like age between 10 to 20 years. There is a wildcardQuery() to search a field with wildcard. termQuery() is also available. You can play with all of these based on your need.
4. Update the document by searching it with Id and replace a field value. Client has a method called update(). It accepts UpdateRequest as input which builds the update query.
5. Last method is to showcase delete a document of an index and type. Client does have prepareDelete() method which accepts the index, type and id to delete the document. 
Build Application
 Run mvn clean install   command to build the jar file.
Start Application
Run java -jar target/standalone-elasticsearch-0.0.1-SNAPSHOT.jar   command to start the Spring Boot application.
Test Application
Application will be running on http://localhost:8102   url. Now let's test couple of use cases we talked above.
1. Test Creating document.
     Launch  http://localhost:8102/rest/users/create as POST method either through curl or Postman
Input -
You will see the response showing "CREATED". 
2. To test if the document has been created, lets test the view functionality.
     launch -  http://localhost:8102/rest/users/view/1 with GET method
In response, you will see the User information for id with value "1".
3. You can view User information by name field as well by launching  http://localhost:8102/rest/users/view/name/Rajesh   This is passing "Rajesh" as "name" field value.
Similarly, update and delete features can be tested by launching  http://localhost:8102/rest/users/update/1  and  http://localhost:8102/rest/users/delete/1 
So these are all the features I have played around and sharing with you through this blog. As mentioned above, there are many more different types of queries you can explore with elasticsearch transport client.

No comments: