Elastic Search with Spring Boot
In the first part of the elastic search series, I have explained about basics of elastic search and using it with Spring boot.
But there are many questions I have received regarding running an elastic search with a spring boot
- How to connect spring boot application with elastic search easily.
- How to check indexes and data of elastic search.
- How to publish data of SQL into elastic search.
So In this post, I will try to explain these things as well we will be creating indexes and publishing data using elasticTemplate, So let's start..
How to connect spring boot application with elastic search easily
There are two things while running elastic search one is we enable default security which is provided by elastic or we disable it but if it is enabled then it is quite complex to connect as well as manage elastic search so its better to disable it for our local development.
Command to run the elastic search by disabling security.
Now to test whether it is working fine or not we can use curl
curl localhost:9200
Now after this In the Spring boot application Just make changes in properties and config file
If we are using Elastic search 8+ then we need to pass headers also to make it compatible with 7.
That's all it will connect your spring boot app with a security disabled elastic search instance running in your local.
Security enabled example I have explained in the previous post.
How to check indexes and data of elastic search
To see indexes and data created on elastic search either we can use curl or the better way is we can Kibana, using kibana we can see the indexes properly and there are lot many things in Kibana
Command to run Kibana
We can access it from port http://localhost:5601
If the security is enabled then we will get an enrollment token and password while starting the elastic search so that we can use it.
How to publish data of SQL into elastic search
To publish data from SQL into the elastic search we can use Logstash (We can run this also on docker by pulling the latest image)
After this, there is a Plugin named JDBC input plugin which is used for copying records from SQL and putting them into elastic search.
It will pull the data whenever there is an update in SQL or after a regular interval, Here we need to keep in my mind the delete scenarios also. So we should do soft delete and first remove records from dependent sources and then from the main DB.
So now I think If we have tried till here we will be having a good understanding of Elastic Search.
Create and Search Index Using ElasticSearchTemplate
Now if we want to create index using ElasticSearchTemplate we can simply call different methods which are used for creating bulkIndexes as well as single indexes.
Now After creating the index next step is to search, There are multiple ways to search using elastic search — NativeQuery, StringQuery, and CriteriaQuery depending on how we construct the query
Now we can test our results using Postman.
In the next post, I will be creating APIs to search using multiple columns and other important concepts related to ELK.
Thanks for reading!!
Ref: