What is Elastic Search? How to use it with Spring-boot
So In this post, we are going to discuss Elastic search and we will be developing one application using spring boot and elastic search which will be providing faster results for search as well as indexing data into elastic search.
What is Elastic Search?
Introduction
Elastic is basically a search engine, which is used for faster retrieval of any kind of data like products we are searching on any e-commerce website all those are powered by elastic search.
Technically It is a distributed, free and open search and analytic engine for all kinds of data. It supports multiple data types. It uses inverted indexes for searching the data.
Elastic search is the main part of the ELK stack. Elasticsearch is built on top of Lucene, it excels at full-text search
It is used by multiple businesses for seeing analytics of the data using different aggregate functions.
We can read more about it here.
Uses of Elastic Search
The main use of elastic search is searching because it is very fast almost near real-time. and it has a lot of features on top of searching, Some uses include-
- Application Search
- Log Management
- Monitoring
- Full-text Search
Elastic has a lot of REST Apis for doing operations like
- Store the documents in an index.
- Fetching the documents from the index
- Running analytics functions on the data
How to test Elastic Search
For testing elastic search we need to have a setup of elastic search, this setup we can create either by using some public cloud like(AWS) or we can run an instance in our local system using docker or by directly downloading and running it. Or we can use elastic as a service.
Now we will be developing one Spring boot application which will be using elastic search dependency.
This dependency provides an interface for doing indexing and search operations instead of using REST Apis directly.
Any data which we want to search is stored as a document in an index. In Spring Boot Document means a POJO class like (product, employee etc.)
Before putting the data into the indexes elastic search processes that data and removes the common word from that and put it into an index.
I am using Docker for running an elastic search in our demo application
Run Elastic Search using Docker
First, pull the image using command
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.3.2
Create a network for elastic search and kibana
docker network create elastic
Now run elastic search a password and token will be generated store it in a safe place.
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.3.2
Now copy the certificate from docker to your local
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
Now run the elastic search in your local and enter the password for elastic when prompted
curl --cacert http_ca.crt -u elastic https://localhost:9200
We have started elastic on port 9200, we can test it using https://localhost:9200 and enter username as elastic and your generated password.
Now connect running elastic with our spring boot application.
There are two ways to do operations on elastic search using spring boot.
- By using
ElasticsearchRestTemplate:
This we should use when we want to create more complex queries. - By Using Repositories: This is very simple and it has all the methods defined and internally it creates elastic-based queries.
Now in our demo application, we will be creating one Product application, In which we can index multiple products and then search those products.
For adding a Document(Product) we need to specify one index in which all the products will be added.
Now one index will be created in elastic search with the above details, but till now there will be no records in that index, Now we need to add records into the index and then search records from that index. continue…..
Now ProductRepo will have all the methods which ElasticsearchRepository has like save(), saveAll(), find() etc…
Now for Indexing(saving) the records into elastic and getting from elastic we will adding the code in service class.
For fetching the records we need to create finder methods which will do processing in runtime based on the property name. some examples are-
List<Product> findByName(String name);
List<Product> findByNameContaining(String name);
List<Product> findByCreatorAndName(String creator, String name);
Now for testing our basic features, we can use Postman and we can enable the trace logs for checking the queries which are generated internally,
In this post, we have discussed the basics of elastic search and developed a simple CRUD application using elastic search.
That's all for this post, In the next post we will be implementing similar operations using ElasticsearchRestTemplate
and will be developing one complete application for demonstrating autocomplete feature. which will give us more insight into how elastic search works internally, till then have a look at elastic search documentation as well.
Thanks for reading!!
Ref: